+ 4

How can I get the x and y position of the mouse ?

26th Feb 2017, 9:44 AM
Ninja Nudiste
Ninja Nudiste - avatar
2 Respuestas
+ 9
They are known as offsets, and you'll need to create a event handler for this: var mousey = document.getElementById('mouse'); mousey.onmousemove = function(e) { var x = e.pageX; var y = e.pageY; } First declare and initiate the mouse as above, then add the event handler. The function catches the event as e here, x and y offsets are declared. Use your imagination to create a function, that when a user interacts with an element it will output it's coordinates like. <p onmouseover="goMouse(event);">Lorem ipsum text goes here but make it long to experience further.</p> <p id="mousePos"></p> IN JAVASCRIPT function goMouse(e) { var m = document.getElementById("mousePos"); var x = e.pageX; var y = e.pageY; m.innerHTML = "X position: " + x + " and Y: " + y; }
26th Feb 2017, 12:35 PM
Mark Foxx
Mark Foxx - avatar
+ 2
var x = event.clientX; y = event.clientY; Where event is the javascript event... Click, mouseenter, mousemove...
26th Feb 2017, 12:19 PM
florin holban
florin holban - avatar