摘要:
下文讲述js脚本获取html标签的坐标的方法分享,如下所示;
实现思路:
获取标签的相对父级的offsetTop和offsetLeft坐标值,
依次循环叠加,直到无父级元素为止,
返回出叠加的偏移值,即为绝对坐标
function getLocation(e){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t +=e.offsetTop;
l +=e.offsetLeft;
}
alert("元素的绝对坐标:top="+t+"/nleft="+l);
}
例:
获取按钮的绝对坐标
maomao365.com
js获取html元素的绝对坐标的示例分享
function getLocation(e){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t +=e.offsetTop;
l +=e.offsetLeft;
}
alert("元素的绝对坐标:top="+t+"/nleft="+l);
}
window.onload = function()
{
var b = document.getElementById("btn");
b.onclick = function(){getLocation(this);}
}
获取绝对坐标
javascript使用js获取元素的绝对坐标的示例分享