I want to get the offset of an element relative to a specific parent not the direct one and not the document.
I looked for that on the internet and found the offset and position JQuery methods. But seems they don't help me in my situation ( relative to a specific parent)
Any help!
I want to get the offset of an element relative to a specific parent not the direct one and not the document.
I looked for that on the internet and found the offset and position JQuery methods. But seems they don't help me in my situation ( relative to a specific parent)
Any help!
Share Improve this question asked Mar 22, 2011 at 10:02 HomamHomam 23.8k37 gold badges117 silver badges189 bronze badges 1 |2 Answers
Reset to default 20You can try this out:
var offsetLeft = $('#myElement').position().left - $('#myElement').closest('#crazyAncestor').position().left;
var offsetTop = $('#myElement').position().top - $('#myElement').closest('#crazyAncestor').position().top;
you need to subtract as OffSet implies the navigator window
var offset = GetOffSet( $("#a"), $("#b") );
function GetOffSet( elem, elemParent ) {
return elemParent.position().left - elem.parent().position().left;
}
$e.position().left - $e.parent().position().left
– Teneff Commented Mar 22, 2011 at 10:08