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.
Asked
Active
Viewed 6.3k times
13
-
I think your going to have to elaborate, and some example code might also be nice. Also are we talking about Java or JavaScript/Web? – MadProgrammer Jan 27 '13 at 08:22
-
JavaScript, this is what i'm talking about – user2013107 Jan 27 '13 at 08:33
2 Answers
23
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>

anatoly techtonik
- 19,847
- 9
- 124
- 140

Mayur Manani
- 795
- 4
- 12
-
-
1
-
cannot get lower value than 8px, and maximum size can be picked more than 100% width... – Marek Bernád Feb 16 '19 at 10:10
1
The javascript method offset()
used for tracking the position, and here I did the same as Mayur says, just little bit added.

Incepter
- 2,711
- 15
- 33

Rajib Ganguly
- 11
- 1