jQuery: Getting px position of an element in Integer format

試著忘記壹切 提交于 2019-12-10 01:22:44

问题


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>

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4652546/jquery-getting-px-position-of-an-element-in-integer-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!