2025-01-05 10:00:02

跟踪鼠标位置

我希望跟踪鼠标光标的位置,周期性地每t秒。因此,从本质上讲,当一个页面加载时,这个跟踪器应该开始,(比如说)每100毫秒,我应该得到posX和posY的新值,并将其打印到表单中。

我尝试了下面的代码-但值不会刷新-只有posX和posY的初始值显示在表单框中。有什么办法能让它运行起来吗?

<html>
<head>
<title> Track Mouse </title>
<script type="text/javascript">
function mouse_position()
{
    var e = window.event;

    var posX = e.clientX;
    var posY = e.clientY;

    document.Form1.posx.value = posX;
    document.Form1.posy.value = posY;

    var t = setTimeout(mouse_position,100);

}
</script>

</head>

<body onload="mouse_position()">
<form name="Form1">
POSX: <input type="text" name="posx"><br>
POSY: <input type="text" name="posy"><br>
</form>
</body>
</html>

当前回答

无论使用哪种浏览器,以下几行都能让我获取正确的鼠标位置。

事件。clientX - event.currentTarget.getBoundingClientRect().left 事件。clientY - event.currentTarget.getBoundingClientRect().top

其他回答

document.addEventListener('mousemove', (event) => { document.getElementById("line").style.top = event.clientY - 10 + 'px'; document.getElementById("lineY").style.left = event.clientX - 10 + 'px'; document.getElementById("pos").style.top = (event.clientY - 60) + 'px'; document.getElementById("pos").style.left = (event.clientX - 60) + 'px'; }); body { position: relative; height: auto; min-height: 100% !important; background-color: lightblue; } h1 { color: white; text-align: center; } p { font-family: verdana; font-size: 20px; } .abs { position: relative; } .lineY { display: flex; position: relative; left: 0px; background-color: black; width: 2px; height: 100vh; min-height: 100% } .line { display: flex; position: relative; background-color: black; min-height: 2px; width: 100%; } .circle { display: flex; position: absolute; left: 0px; top: 0px; } <div class='line' id="line"></div> <div class='lineY' id="lineY"></div> <svg height="100" width="100" id="pos" class="circle"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="transparent" /> </svg>

无论使用哪种浏览器,以下几行都能让我获取正确的鼠标位置。

事件。clientX - event.currentTarget.getBoundingClientRect().left 事件。clientY - event.currentTarget.getBoundingClientRect().top

这里有一个解决方案

document.onmousemove = showCoords;
function showCoords(event) {
var x = event.clientX;
var y = event.clientY;
var coords = "X coords: " + x + ", Y coords: " + y;
document.getElementById("box1").innerHTML = coords;
}

如果只是想直观地跟踪鼠标移动:

<!DOCTYPE html> <html> <head> <title></title> </head> <style type="text/css"> * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; overflow: hidden; } </style> <body> <canvas></canvas> <script type="text/javascript"> var canvas = document.querySelector('canvas'), ctx = canvas.getContext('2d'), beginPath = false; canvas.width = window.innerWidth; canvas.height = window.innerHeight; document.body.addEventListener('mousemove', function (event) { var x = event.clientX, y = event.clientY; if (beginPath) { ctx.lineTo(x, y); ctx.stroke(); } else { ctx.beginPath(); ctx.moveTo(x, y); beginPath = true; } }, false); </script> </body> </html>

最近,我们必须找到当前的x,y位置,以枚举光标所悬停的元素,而不依赖于z-index。我们最终只是附加了一个鼠标移动事件监听器到文档,例如,

功能findElements(e) ( var els =文档。clientX, clientY); 和els一起做酷的东西 游戏机。log (els); return; ) 文档。addEventListener (mousemove”、“findElements);