首頁  >  CSS  > position()

返回值:Object{top,left} position()

V1.2 概述

獲取匹配元素相對父元素的偏移。

返回的對象包含兩個整型屬性:top 和 left。為精確計算結果,請在補白、邊框和填充屬性上使用畫素單位。此方法只對可見元素有效。

示例

描述:

獲取第一段的偏移

HTML 程式碼:

<p>Hello</p><p>2nd Paragraph</p>
jQuery 程式碼:

var p = $("p:first");
var position = p.position();
$("p:last").html( "left: " + position.left + ", top: " + position.top );
結果:

<p>Hello</p><p>left: 15, top: 15</p>