jQuery:以整数格式获取元素的px位置 [英] jQuery: Getting px position of an element in Integer format
本文介绍了jQuery:以整数格式获取元素的px位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码可用于FF,但不适用于IE:
This code works in FF, but not in IE:
parseInt($('.scroller').css('left');
在FF中返回0px;
In FF it returns 0px;
在IE中,它返回NaN
.
获取元素的像素位置的好方法是什么?
What's a good way to get a pixel position of an element?
<div class="holder">
<div class="scroller">
</div>
</div>
推荐答案
使用 offset
:
$('.scroller').offset().left;
offset()
返回一个包含属性left
和top
的对象,这两个属性是相对于文档的位置值(以像素为单位).
offset()
returns an object containing the properties left
and top
, which are the position values relative to the document in pixels.
如果要相对于父元素定位,请改用 position
.
If you want the position relative to the parent element, use position
instead.
这篇关于jQuery:以整数格式获取元素的px位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文