获取鼠标在屏幕上的位置
代码
1 function mousePosition(ev){
2 if(ev.pageX || ev.pageY){
3 return {x:ev.pageX, y:ev.pageY};
4 }
5 return {
6 x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
7 y:ev.clientY + document.body.scrollTop - document.body.clientTop
8 };
9 }
使用方法:
document.onmousemove = mouseMove; function mouseMove(ev){ ev = ev || window.event; //定义一个ev对象 var mousePos = mousePosition(ev); document.getElementById('mouseXPosition').value = mousePos.x; document.getElementById('mouseYPosition').value = mousePos.y; }