Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse position on mouseover event

Is it possible to get the exact mouse position in a mouseouver event of an image? If I use a function that updates the mouse position on a document mouse move event, I can have problems with delay and this kind of thing and wouldn't get the EXACT position.

like image 399
user2013107 Avatar asked Jan 27 '13 08:01

user2013107


People also ask

How do I get mouse position from event?

To get the mouse position relative to an element in React, set an onMouseMove event handler on the element, then calculate the local X and Y position using properties of the MouseEvent object passed to the event handler. Now the resulting X and Y coordinates will be relative to the element.

How do I find my mouse cursor coordinates?

In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

What is the difference between Mouseenter () and mouseover () event?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

Which event is used when mouse moves over the element?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.


2 Answers

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

    <!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>
like image 61
Mayur Manani Avatar answered Oct 15 '22 18:10

Mayur Manani


The javascript method offset() used for tracking the position, and here I did the same as Mayur says, just little bit added.

See jsfiddle

like image 1
Rajib Ganguly Avatar answered Oct 15 '22 18:10

Rajib Ganguly