程序员人生 网站导航

JavaScript函数获取元素位置坐标的代码

栏目:jscript时间:2014-01-01 07:28:37

 这是一个获取元素位置坐标的JS函数,分享给大家,更多JavaScript,请访问网中文手册:http://www.wfuyu.com/a/manual/jscript/

function elementLeft(e){
var offset = e.offsetLeft;
if(e.offsetParent != null) offset += elementLeft(e.offsetParent);
return offset;
}

function elementTop(e){
var offset=e.offsetTop;
if(e.offsetParent != null) offset += elementTop(e.offsetParent);
return offset;
}

function elementPos(e){
return {left:elementLeft(e), top:elementTop(e)};
}

配合《获取鼠标坐标的JavaScript函数》使用,效果更佳。

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐