gpt4 book ai didi

jQuery 检查偏移量

转载 作者:行者123 更新时间:2023-12-01 04:30:50 25 4
gpt4 key购买 nike

HTML:

<ul class="clients">
<li>
<div class="over left">Description</div>
<div class="inner">Image</div>
</li>
</ul>

CSS:

.clients { margin-right: -20px; }
.clients li {
float: left;
width: 128px;
height: 120px;
margin: 0 20px 20px 0;
border: 1px solid #c2c2c2;
}
.clients .over {
display: none;
position: absolute;
width: 250px;
font-size: 11px;
line-height: 16px;
background: #ecf5fb;
margin: 3px 0 0 3px;
padding: 18px;
z-index: 25;
}
.clients .right { margin: 3px 0 0 -161px; }
.clients .inner { width: 128px; height: 120px; overflow: hidden; }

因此,我们有一个 float 方 block 列表,每个方 block 中都有一个弹出 block ,它们具有绝对位置。

JS:

jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){$(this).find("div.over").toggle()});
});

如果超过 - 显示,否则 - 隐藏。很好,但它必须更高级,我们应该捕获一个偏移量并为 .over block 提供一个类:

  • 如果距右侧(浏览器窗口一角)的偏移小于小于 150 像素,则为 .over block 添加“right”类
  • 如果从右侧偏移超过超过 150 像素 - 为 .over block 添加“left”类

我们怎样才能做到这一点?

最佳答案

.offset() 返回一个类似 { left: 200, top: 300 }

的对象

$(window).width() 返回窗口宽度

显然,您可以从 .offset() 获得左偏移直线。您需要创建条件的正确偏移量应计算为:

offsetRight=$(window).width()-$(this).width()-$(this).offset().left;

一起:

jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){
$over=$("div.over",this);
$over.toggle();
//didn't get if it's the li's offset you need or the actual .over, replace $(this) with $over in next lines if you need that
offset=$(this).offset();
offsetRight=$(window).width()-$(this).width()-offset.left;
if (offsetRight<150){ $over.removeClass('left').addClass('right'); }
else { $over.removeClass('right').addClass('left'); }
});
});

关于jQuery 检查偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2667205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com