function getElementLeft(element) { var actualLeft = element.offsetLeft; var current = element.offsetParent; while (current!==null) { actualLeft += current.offsetLeft; current = current.offsetParent; } return actualLeft; }
取得元素的左偏移量;
function getElementTop(element) { var actualTop = element.offsetTop; var current = element.offsetParent; while (current!==null) { <span style="white-space:pre"> </span>actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; }
取得元素的上偏移量;
利用offsetParent屬性在Dom層次中逐級向上回溯,將每個層次偏移量合計。