+ 4
How can I get the x and y position of the mouse ?
2 Antworten
+ 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;
}
+ 2
var x = event.clientX;
y = event.clientY;
Where event is the javascript event... Click, mouseenter, mousemove...