2014-02-20 27 views
0
之間
+0

是很難看的源代碼? – Populus

+0

取決於'this'是什麼。 –

+0

並幽默你(也因爲我想知道)這裏是源代碼https://github.com/jquery/jquery/blob/master/src/offset.js – Populus

回答

3

jQuery offset()

獲取所述第一元件的當前座標,或設置的每個元件的座標,在所述一組匹配的元素,相對於文檔的。

HTMLElement.offsetTop

的offsetTop返回電流元件相對於offsetParent節點的頂部的距離。

jQuery position()

獲取匹配的元素,相對於偏移父的第一個元素的當前座標。

Getting the position of the element to the page

function getOffset(el) { 
    var _x = 0; 
    var _y = 0; 
    while(el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) { 
     _x += el.offsetLeft - el.scrollLeft; 
     _y += el.offsetTop - el.scrollTop; 
     el = el.offsetParent; 
    } 
    return { top: _y, left: _x }; 
}