你好
我有一个固定位置的元素,无法检测其位置,并且必须使用纯 JavaScript 进行操作,不得使用任何框架(jQuery、MooTools 等)。
你好
我有一个固定位置的元素,无法检测其位置,并且必须使用纯 JavaScript 进行操作,不得使用任何框架(jQuery、MooTools 等)。
使用:
var boundingBox = node.getBoundingClientRect();
请查看结果,您将会得到这样一个对象:
top : 0,
right : 0,
bottom : 0,
left : 0,
width : 0,
height : 0
document.getElementById('id').offsetLeft // + window.scrollX
document.getElementById('id').offsetTop // + window.scrollY
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent)
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curleft,curtop];
}