How to get offset relative to a specific parent?

后端 未结 2 493
广开言路
广开言路 2021-02-13 12:23

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 of

相关标签:
2条回答
  • 2021-02-13 12:59

    You can try this out:

    var offsetLeft = $('#myElement').position().left - $('#myElement').closest('#crazyAncestor').position().left;
    var offsetTop = $('#myElement').position().top - $('#myElement').closest('#crazyAncestor').position().top;
    
    0 讨论(0)
  • 2021-02-13 13:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题