jQuery 检查偏移量
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; }
因此,我们有一个浮动方块列表和每个浮动方块的弹出块,它们具有绝对位置。
JS:
jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){$(this).find("div.over").toggle()});
});
如果超过 - 显示,否则 - 隐藏。很好,但它必须更高级,我们应该捕获一个偏移量并为 .over 块提供一个类:
- 如果从右(浏览器窗口的一角)偏移小于 150px,然后为
.over
块添加“right”类。 - 如果从右侧偏移大于超过 150 像素 - 为
.over
块添加“left”类。
我们怎样才能做到这一点?
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; }
So, we have a list of floated squares and a popup blocks in each, which have absolute position.
JS:
jQuery(function($) {
$(".clients li").bind('mouseover mouseout',function(){$(this).find("div.over").toggle()});
});
If over - show, else - hide. Quite ok, but it must be more advanced, we should catch an offset and give a class to .over block:
- if offset from right (corner of browser window) less than 150px, then add class "right" for an
.over
block. - if offset from right more than 150px - add class "left" for an
.over
block.
How can we do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.offset()
返回一个对象,例如{ left: 200, top: 300 }
$(window).width()
返回窗口宽度显然,您可以从
.offset()
获得左偏移直线。建立条件所需的正确偏移量应计算如下:合计:
.offset()
returns an object like{ left: 200, top: 300 }
$(window).width()
returns the window widthClearly you get the left offset streight from
.offset()
. The right offset you need to make a condition with should be calculated as:All together: