首页 >web前端 >js教程 >多浏览器兼容的获取元素和鼠标的位置的js代码_javascript技巧

多浏览器兼容的获取元素和鼠标的位置的js代码_javascript技巧

WBOY
WBOY原创
2016-05-16 18:39:131036浏览
复制代码代码如下:

//获取元素的位置
function getLeft(obj) {
if (obj == null)
返回 null;
var mendingObj = obj;
var mendingLeft = mendingObj.offsetLeft;
while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") {
mendingLeft = mendingLeft mendingObj.offsetParent.offsetLeft;
mendingObj = mendingObj.offsetParent;
}

返回修补左;
};
function getTop(obj) {
if (obj == null)
返回 null;
var mendingObj = obj;
var mendingTop = mendingObj.offsetTop;
while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") {
mendingTop = mendingTop mendingObj.offsetParent.offsetTop;
mendingObj = mendingObj.offsetParent;
}
返回修补顶部;
};
//获取鼠标的位置
function getMousePosition(event) {
varposition = {
MouseX: 0,
MouseY: 0
}
if (event.pageX != 未定义) {
position.MouseX = event.pageX;
position.MouseY = event.pageY;
}
else {
var target = EventUtil.getTarget(event);
position.MouseX = event.offsetX getLeft(target);
position.MouseY = event.offsetY getTop(target);

}
返回位置;
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn