jQuery获取元素相对于另一个元素的位置 [英] jQuery get position of element relative to another element

查看:98
本文介绍了jQuery获取元素相对于另一个元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个像这样的div:

So I have a div like:

<div class="uiGrid">

<div class="trigger"></div>

</div>

我想知道uiGrid的触发器的位置,并尝试了这两种方法:

And I want to know the position of trigger to uiGrid and have tried both these:

$('.trigger').offset('.uiGrid');

$('.trigger').position('.uiGrid');

,但都不明白.偏移量相对于文档,位置相对于父文档而不是指定元素.

but neither get it. Offset is relative to the document and position is relative to the parent and not the specified element.

我该怎么做? 谢谢

推荐答案

只要对自己进行减法即可.

just do the subtraction your self...

var relativeY = $("elementA").offset().top - $("elementB").offset().top;

这篇关于jQuery获取元素相对于另一个元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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