I cannot fi...">

Javascript onmousemove get relative coordinates

function aim() { var mousex = ? } <div class="gameArea" onmousemove="aim();"> </div> 

I cannot find how to get mouse coordinates for onmousemove event in javascript.

+4
source share
3 answers
 var guide = null; function aim(event) { if(window.event) event = window.event; //grrr IE var mousex = event.clientX - guide.offsetLeft; alert('mouse x' + mousex) } function init() { guide = document.getElementById("guide") guide.onmousemove = aim; } <body onload="init();"> 

This is similar to working with both browsers. I can not do onmousemove = "aim ()"; in html because it does not skip the mousemove event object.

+9
source

The above example using guide.offsetLeft only works if the enclosing element is at (0,0). If not, you can use guide.getBoundingClientRect (). Left (and similar for vertical coordination)

+1
source

Pay a lot of attention to the following script. I believe that this is exactly what you are looking for.

Mouse capture

0
source

Source: https://habr.com/ru/post/1335077/


All Articles