So I have a div like:
<div class="uiGrid"> <div class="trigger"></div> </div>
And I want to know the position of trigger to uiGrid and have tried both these:
$('.trigger').offset('.uiGrid'); $('.trigger').position('.uiGrid');
but neither get it. Offset is relative to the document and position is relative to the parent and not the specified element.
How would I do this? Thanks
jQuery position() MethodThe 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.
Answer: Use the jQuery position() methodYou can easily find the position of an element relative to the offset parent using the jQuery position() method. It is only applicable for the visible elements. That means, you can get the position of elements with visibility: hidden; but not with display: none; .
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.
The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.
just do the subtraction your self...
var relativeY = $("elementA").offset().top - $("elementB").offset().top;
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