複製鏈接
請複製以下鏈接發送給好友

offset

(jQuery的函數)

鎖定
offset(), jQuery的函數。

offset內容簡介

獲取匹配元素在當前視口的相對偏移。
返回的對象包含兩個整形屬性:top 和 left。此方法只對可見元素有效。
Get the current offset of the first matched element relative to the viewport.
The returned object contains two Integer properties, top and left. The method works only with visible elements.
當用於返回偏移時:
該方法返回第一個匹配元素的偏移座標。它返回一個帶有兩個屬性(以像素為單位的 top 和 left 位置)的對象。
當用於設置偏移時:
該方法設置所有匹配元素的偏移座標。 [1] 

offset返回值類型

Object{top,left}

offset示例

獲取第二段的偏移
HTML 代碼:
Hello
2nd Paragraph
jQuery 代碼:
var p = $("p:last");
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );
結果:
Hello
left: 0, top: 35
參考資料