There is no left assigned for the div, I use $('#').css('left');
to get the left value, but it shows auto.
How do I get the left value of the div.
Thanks Jean
jQuery position() Method The position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.
jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.
Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.
The offset() method tells you the actual position of the element relative to the document. See the example on the page that I linked to to see how they got the left and top values.
That is:
var left = $('Your_selector').offset().left;
The .css() command returns what the CSS setting is, so if you have 'auto' set, that is what you will see.
You can retrieve the computed position relative to the parent with $('#').position().left;
, 'top' also is available from position(), if you want the value relative to the document use $('#').offset().left
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With