I would like to know how to get the distance/offset/position of an element relative to it's parent element.
Unlike jQuery's position()
functionality, which get's the position relative to the parent's offset position, I need to get the (assuming) stable position of an element as it's distance from the top of the containing/parent element.
example:
<div id="parent">
<div id="pos1">Has a position of 0px from top of containing parent el.</div>
<br style="height:20px;">
<br style="height:20px;">
<div id="pos2">Has a position of 40px from top of containing parent el.</div>
</div>
So, no matter what the position of the parent element to the document, the position of pos1
and pos2
would be reported the same, as they don't change relative to their parent element...
Is this possible?
This should do it:
$('#pos1').offset().top - $('#pos1').parent().offset().top - $('#pos1').parent().scrollTop()
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