e.screenY 给出错误的值(鼠标坐标)

e.screenY giving wrong value (mouse coordinates)

我在获取 Y 鼠标坐标时遇到问题。

function mouseDistance() {
    document.onmousemove = handleMouseMove;
    function handleMouseMove(e) {
        document.getElementById("tester").innerHTML = e.screenX + " " + e.screenY;
    }
}

function mainLoop() {
    mouseDistance();
    requestAnimationFrame(mainLoop);
}
requestAnimationFrame(mainLoop);

e.screenX 正常工作,但 e.screenY 不正常。在 html 正文中,在最顶部边缘,它不会低于 95px(不应该是 0 吗?)。

我有 'CSS reset'.

例子:左上角的鼠标会在x:0;是:95;

原因是screenY提供了鼠标指针在全局(屏幕)坐标中的垂直坐标(偏移量)。

您可能要使用的 属性 是 clientY,其中 returns 事件发生时应用程序客户区内的垂直坐标。

For example, clicking in the top-left corner of the client area will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically.