最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get offset relative to a specific parent? - Stack Overflow

programmeradmin3浏览0评论

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
  • 3 you can always subtract the two positions like: $e.position().left - $e.parent().position().left – Teneff Commented Mar 22, 2011 at 10:08
Add a comment  | 

2 Answers 2

Reset to default 20

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;

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)

  1. 暂无评论