返回值:Object offset([coordinates])
概述
獲取匹配元素在目前視口的相對偏移。
返回的對象包含兩個整型屬性:top 和 left,以畫素計。此方法只對可見元素有效。
參數
coordinates{top,left} Object V1.4
必需。規定以畫素計的 top 和 left 座標。
可能的值:
- 值對,比如 {top:100,left:0}
- 帶有 top 和 left 屬性的對象
function(index,coords) function V1.4
規定返回被選元素新偏移座標的函式。
- index - 可選。接受選擇器的 index 位置
- oldvalue - 可選。接受選擇器的當前座標
示例
無參數描述:
獲取第二段的偏移
HTML 程式碼:
<p>Hello</p><p>2nd Paragraph</p>
jQuery 程式碼:
var p = $("p:last");
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );
結果:
<p>Hello</p><p>left: 0, top: 35</p>
參數coordinates 描述:
獲取第二段的偏移
HTML 程式碼:
<p>Hello</p><p>2nd Paragraph</p>
jQuery 程式碼:
$("p:last").offset({ top: 10, left: 30 });