1document.addEventListener("mousemove", () => {
2 let mousex = event.clientX; // Gets Mouse X
3 let mousey = event.clientY; // Gets Mouse Y
4 console.log([mousex, mousey]); // Prints data
5});
6
1// Cursor coordinate functions
2var myX, myY, xyOn, myMouseX, myMouseY;
3xyOn = true;
4
5function getXYPosition(e) {
6 myMouseX = (e || event).clientX;
7 myMouseY = (e || event).clientY;
8 if (document.documentElement.scrollTop > 0) {
9 myMouseY = myMouseY + document.documentElement.scrollTop;
10 }
11 if (xyOn) {
12 alert("X is " + myMouseX + "\nY is " + myMouseY);
13 }
14}
15function toggleXY() {
16 xyOn = !xyOn;
17 document.getElementById('xyLink').blur();
18 return false;
19}
20
21document.onmouseup = getXYPosition;
1<p>Click in the div element below to get the x (horizontal) and y (vertical) coordinates of the mouse pointer, when it is clicked.</p>
2
3<div onclick="showCoords(event)"><p id="demo"></p></div>
4
5<p><strong>Tip:</strong> Try to click different places in the div.</p>
6
7<script>
8function showCoords(event) {
9 var cX = event.clientX;
10 var sX = event.screenX;
11 var cY = event.clientY;
12 var sY = event.screenY;
13 var coords1 = "client - X: " + cX + ", Y coords: " + cY;
14 var coords2 = "screen - X: " + sX + ", Y coords: " + sY;
15 document.getElementById("demo").innerHTML = coords1 + "<br>" + coords2;
16}
17</script>