In the page js gets the cursor and mouse coordinates and the pixel coordinates of the cursor

  • 2020-03-27 00:07:20
  • OfStack

Recently for the website development page statistics, although also developed before, but the function is not very complete, so this time to use some good functions.

For example, this time, the page JS cursor/mouse coordinates, you may ask what is the use of baidu statistics there is a hot statistical graph, which clearly understand it.

Okay, serve the meat:
Function: get the pixel coordinates of the cursor
 
<html> 
<head> 
<script type="text/javascript"> 
function showPosition(e){ 
var x,y; 
var e = e||window.event; 

document.getElementById("x").value = e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft; 
document.getElementById("y").value = e.clientY+document.body.scrollTop+document.documentElement.scrollTop 

} 
</script> 
</head> 
<body> 
<div onmousemove="showPosition(event);" style="height:100%; width:100%;"> 
x:<input id="x" type="text" value="" /> 
y:<input id="y" type="text" value="" /> 
</div> 
</body> 
</html> 

Related articles: