欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-07 17:59:10
获取页面某一元素的绝对x,y坐标,可以用offset()方法: . 代码如下:

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

. 代码如下:


<span style="color:#000066; font-weight:bold">var</span> x <span style="color:#339933">=</span> $<span style="color:#009900">(</span><span style="color:#3366cc">'#divid'</span><span style="color:#009900">)</span>.<span style="color:#660066">offset</span><span style="color:#009900">(</span><span style="color:#009900">)</span>.<span style="color:#660066">top</span><span style="color:#339933">;</span>
<span style="color:#000066; font-weight:bold">var</span> y <span style="color:#339933">=</span> $<span style="color:#009900">(</span><span style="color:#3366cc">'#divid'</span><span style="color:#009900">)</span>.<span style="color:#660066">offset</span><span style="color:#009900">(</span><span style="color:#009900">)</span>.<span style="color:#660066">left</span><span style="color:#339933">;</span>


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

. 代码如下:


<span style="color:#000066; font-weight:bold">var</span> x <span style="color:#339933">=</span> $<span style="color:#009900">(</span><span style="color:#3366cc">'#divid'</span><span style="color:#009900">)</span>.<span style="color:#660066">position</span><span style="color:#009900">(</span><span style="color:#009900">)</span>.<span style="color:#660066">top</span><span style="color:#339933">;</span>
<span style="color:#000066; font-weight:bold">var</span> y <span style="color:#339933">=</span> $<span style="color:#009900">(</span><span style="color:#3366cc">'#divid'</span><span style="color:#009900">)</span>.<span style="color:#660066">position</span><span style="color:#009900">(</span><span style="color:#009900">)</span>.<span style="color:#660066">left</span><span style="color:#339933">;</span>