简体   繁体   English

JavaScript mousemove事件触发

[英]Javascript mousemove event firing

Is there a way using mousemove event to capture every pixel that the cursor collides with? 有没有一种方法可以使用mousemove事件捕获光标与之碰撞的每个像素? so that the following condition would fire consistently. 因此以下情况会持续发生。

if(e.pageX == 100) 

I find that if the mouse moves fast then it skips pixels in between.. however if I move it slowly it will catch pixels one by one. 我发现,如果鼠标快速移动,则它会跳过它们之间的像素。但是,如果我缓慢移动,它将一一捕获像素。

Thanks 谢谢

Mousemove triggers on every mouse movement and targets the topmost and most nested element which is covered by mouse. Mousemove会在每次鼠标移动时触发,并以鼠标覆盖的最顶层和最嵌套的元素为目标。 Mousemove, mouseover and mouseout trigger when browser internal timing allows only . 当浏览器内部计时仅允许时,会触发mousemove,mouseover和mouseout That means if you move the mouse fast, intermediate DOM elements and parents will be skipped. 这意味着如果快速移动鼠标,则会跳过中间的DOM元素和父元素。

So you can move over an element without any mousemove/mouseover triggered on it. 因此,您可以在元素上移动而不会触发任何鼠标移动/鼠标悬停。

You also can move from a child through parent without any mouse event on the parent as browser skips intermediate elements 当浏览器跳过中间元素时,您也可以从一个子代移动到另一个父代,而在父代上没有任何鼠标事件

The only guarantee you have is that as far as mouseover was triggered, the mouseout will trigger too. 您唯一的保证是,就触发了鼠标悬停而言,鼠标悬停也会触发。

No. 没有。

Mouse cursor is checked and if the position has changed since last time a mousemove event is triggered, but only with the new detected position. 检查鼠标光标,并且自上次触发mousemove事件以来,位置是否已更改,但仅使用新检测到的位置。

What you can do is storing the last known mouse position and the compute a straight line between that and the current position to do your computations. 您可以做的是存储最后一个已知的鼠标位置,并在该位置和当前位置之间计算一条直线以进行计算。 This doesn't require much code... a simple approach is 这不需要太多代码...一种简单的方法是

// Calls `f(x, y)` for each point starting from
// `(x0, y0)` and ending in `(x1, y1)` (excluded)
// moving along an 8-connected straight path.
function processLine(x0, y0, x1, y1, f) {
    var ix = x0 < x1 ? 1 : -1, dx = Math.abs(x1 - x0);
    var iy = y0 < y1 ? 1 : -1, dy = Math.abs(y1 - y0);
    var m = Math.max(dx, dy), cx = m >> 1, cy = m >> 1;
    for (var i=0; i<m; i++) {
        f(x0, y0);
        if ((cx += dx) >= m) { cx -= m; x0 += ix; }
        if ((cy += dy) >= m) { cy -= m; y0 += iy; }
    }
}

and can be used as 可以用作

processLine(10, 20, 30, 35, function(x, y) {
    console.log("x=" + x + ", y=", y);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM