javascript 获取控件绝对位置
方法一:
function GetObjPos()
{
var obj = document.getElementById("id");
var t=obj.offsetTop;
var l=obj.offsetLeft;
while(obj=obj.offsetParent)
{
t+=obj.offsetTop;
l+=obj.offsetLeft;
}
document.getElementById("a").style.left = l;
document.getElementById("a").style.top = t + 20;
return true;
}
方法二:
function subABSPos(node) {
var x = y = 0;
do { x = node.offsetLeft; y = node.offsetTop; }
while (node = node.offsetParent);
return { 'x': x, 'y': y };
}
调用:var vx = subABSPos(obj).x;
function GetObjPos()
{
var obj = document.getElementById("id");
var t=obj.offsetTop;
var l=obj.offsetLeft;
while(obj=obj.offsetParent)
{
t+=obj.offsetTop;
l+=obj.offsetLeft;
}
document.getElementById("a").style.left = l;
document.getElementById("a").style.top = t + 20;
return true;
}
方法二:
function subABSPos(node) {
var x = y = 0;
do { x = node.offsetLeft; y = node.offsetTop; }
while (node = node.offsetParent);
return { 'x': x, 'y': y };
}
调用:var vx = subABSPos(obj).x;