您的位置:首页 > Web前端 > JavaScript

js获取鼠标当前所在页面位置

2012-08-23 09:10 183 查看
//此方法对于嵌套在一个页面A中的B页面,获取B页面的位置在IE9和其他浏览器(包括IE其他系列浏览器)下有些不同,IE9是根据浏览器来定位的,FF及其他则是根据当前页面也就是嵌套的页面来定位的(真正兼容还待改进)

    function getEvent() //同时兼容ie和ff的写法
{
if (document.all)
return window.event;
func = getEvent.caller;
while (func != null) {
var arg0 = func.arguments[0];
if (arg0) {
if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
return arg0;
}
}
func = func.caller;
}
return null;
}
var __is_ff = (navigator.userAgent.indexOf("Firefox") != -1); //Firefox
function getMouseLocation() {
var e = getEvent();
var mouseX = 0;
var mouseY = 0;
if (__is_ff) {
mouseX = e.layerX + document.body.scrollLeft;
mouseY = e.layerY + document.body.scrollLeft;
}
else {
mouseX = e.x + document.body.scrollLeft;
mouseY = e.y + document.body.scrollTop;
}
return { x: mouseX, y: mouseY };
}
//调用的方法,弹出当前所在页面的位置
function show() {
var test = getMouseLocation();
alert(test.x + ":" + test.y);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: