在jquery / javascript中我试图找到一个元素与位置相距多远:相对。 屏幕左侧和屏幕左侧有几个元素。 因为它的位置是:相对和浮动:左边使用:
$(this).css('left');每次给我'0px'。 那么我怎么才能发现元素离屏幕左侧有多远呢?
in jquery/javascript I'm trying to find how far left an element is with position:relative. There are several elements between this one and the left of the screen. since it is positioned:relative and float:left using:
$(this).css('left');gives me '0px' every time. So how do I found how far the element is from the left of the screen?
最满意答案
你可以使用jQuery的.offset()方法。
这是一个例子: http : //jsfiddle.net/PgbmV/
$(this).offset().left; http://api.jquery.com/offset/这将给出与文档相关的左侧位置。
如果您想要相对于其容器的位置,您将以类似的方式使用.position() 。
$(this).position().left; http://api.jquery.com/position/You can use jQuery's .offset() method.
Here's an example: http://jsfiddle.net/PgbmV/
$(this).offset().left; http://api.jquery.com/offset/This will give the left position relative to the document.
If you want the position relative to its container, you would use .position() in a similar manner.
$(this).position().left; http://api.jquery.com/position/更多推荐
发布评论