This article describes the method of obtaining the current mouse coordinates by javascript. Share it for your reference. The specific implementation method is as follows:
For JavaScript to obtain the current mouse coordinates, you must have some understanding of the coordinate positions of different browsers. The specific code is as follows:
Copy the code as follows: <html>
<head>
<title>javascript gets the current mouse coordinates</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript">
function mousePosition(ev){
if(ev.pageX || ev.pageY){//firefox, chrome and other browsers
return {x:ev.pageX,y:ev.pageY};
}
return {// IE browser
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mousePosition(ev);
document.getElementById('x').innerHTML = mousePos.x;
document.getElementById('y').innerHTML = mousePos.y;
}
document.onmousemove = mouseMove;
</script>
<style type="text/css">
h3{color:blue;}
p{line-height:30px;height:30px;font-size:14px;width:500px;}
span{color:orange;font-weight:bold;}
</style>
</head>
<body>
<h3>Your mouse has been tracked</h3>
<p> X-axis coordinates: <span id="x"></span></p>
<p> Y-axis coordinates: <span id="y"></span></p>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.