如何获得相对于特定父级的偏移量? [英] How to get offset relative to a specific parent?

查看:85
本文介绍了如何获得相对于特定父级的偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到一个元素相对于特定父的偏移而不是直接的而不是文档。

I want to get the offset of an element relative to a specific parent not the direct one and not the document.

我看了在互联网上找到了偏移位置 JQuery方法。但似乎他们在我的情况下没有帮助我(相对于特定的父母)

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)

任何帮助!

推荐答案

你可以试试这个:

var offsetLeft = $('#myElement').position().left - $('#myElement').closest('#crazyAncestor').position().left;
var offsetTop = $('#myElement').position().top - $('#myElement').closest('#crazyAncestor').position().top;

这篇关于如何获得相对于特定父级的偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆