Mouse position in mouseover event

Is it possible to get the exact mouse position in the mouse event with the image? If I use a function that updates the position of the mouse on the mouse move event on the document, I may have problems with the delay and this kind of thing, and you won’t get the EXACT position

+5
source share
2 answers

If you are looking for simple JS to get the cursor position for the MouseOver event, here is a code example:

    <!DOCTYPE html>
    <html>
    <head>
    	<script>
    	
    	function getPos(e){
    		x=e.clientX;
    		y=e.clientY;
    		cursor="Your Mouse Position Is : " + x + " and " + y ;
    		document.getElementById("displayArea").innerHTML=cursor
    	}
    
    	function stopTracking(){
    		document.getElementById("displayArea").innerHTML="";
    	}
    
    	</script>
    </head>
    
    <body>
    	<div id="focusArea" onmousemove="getPos(event)" onmouseout="stopTracking()"><p>Mouse Over This Text And Get The Cursor Position!</p></div>
    	
    	<p id="displayArea"></p>
    </body>
    </html>
Run codeHide result
+12
source

The javascript () offset method used to track the position, and here I did the same as Mayur, adding a bit. [Jsfiddle.net/7P8Rr/] [1]

0
source

All Articles