jQuery获得页面元素的绝对/相对位置即绝对X,Y坐标

jQuery获得页面元素的绝对X,Y坐标,可以用offset()方法,下面有个不错的坐标大家可以参考下

获取页面某一元素的绝对X,Y坐标,可以用offset()方法:

复制代码 代码如下:


<span>var</span> X <span>=</span> $<span>(</span><span>'#DivID'</span><span>)</span>.<span>offset</span><span>(</span><span>)</span>.<span>top</span><span>;</span>
<span>var</span> Y <span>=</span> $<span>(</span><span>'#DivID'</span><span>)</span>.<span>offset</span><span>(</span><span>)</span>.<span>left</span><span>;</span>


获取相对(父元素)位置:

复制代码 代码如下:


<span>var</span> X <span>=</span> $<span>(</span><span>'#DivID'</span><span>)</span>.<span>position</span><span>(</span><span>)</span>.<span>top</span><span>;</span>
<span>var</span> Y <span>=</span> $<span>(</span><span>'#DivID'</span><span>)</span>.<span>position</span><span>(</span><span>)</span>.<span>left</span><span>;</span>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdyddj.html