jQuery: Getting px position of an element in Integ

2019-04-19 17:25发布

This code works in FF, but not in IE:

parseInt($('.scroller').css('left');

In FF it returns 0px;

In IE it returns NaN.

What's a good way to get a pixel position of an element?

<div class="holder">
    <div class="scroller">
    </div>
</div>

2条回答
甜甜的少女心
2楼-- · 2019-04-19 18:01

It would be however always relative to document, therefore position() (relative to parent, i.e. holder) would sometimes be rather accurate and sometimes none of them.

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-19 18:09

Use offset:

$('.scroller').offset().left;

offset() returns an object containing the properties left and top, which are the position values relative to the document in pixels.

If you want the position relative to the parent element, use position instead.

查看更多
登录 后发表回答