JavaScript怎么获得鼠标当前坐标?
1、屏幕位置
function getMousePos(event) {
var e = event || window.event;
return {'x':e.screenX,'y':screenY}
}
2、浏览器窗口
function getMousePos(event) {
var e = event || window.event;
return {'x':e.clientX,'y':clientY}
}
3、文档
function getMousePos(event) {
var e = event || window.event;
var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var x = e.pageX || e.clientX + scrollX;
var y = e.pageY || e.clientY + scrollY;
//alert('x: ' + x + '\ny: ' + y);
return { 'x': x, 'y': y };
}