jquery javascript find how far left an element is with position:relative
in jquery/javascri开发者_如何学Cpt 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?
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/
精彩评论