//获得元素位置
function getAbsolutePosition(obj) {
position = new Object();
position.x = 0;
position.y = 0;
position.h = 0;
position.w = 0;
var tempobj = obj;
if (tempobj.offsetHeight + '' != 'undefined') { position.h = tempobj.offsetHeight; }
if (tempobj.offsetWidth + '' != 'undefined') { position.w = tempobj.offsetWidth; }
while (tempobj != null && tempobj != document.body) {
position.x += tempobj.offsetLeft + tempobj.clientLeft;
position.y += tempobj.offsetTop + tempobj.clientTop;
tempobj = tempobj.offsetParent
}
return position;
}
//使用
var ask = getAbsolutePosition(xxx);
alert(ask.x); //横坐标
alert(ask.y); //纵坐标
alert(ask.h); //高
alert(ask.w); //宽
JS获得HTML元素位置
最新推荐文章于 2021-06-07 15:51:59 发布