每一刻有多少老鼠上下移动

How much mouse goes up and down at every moment

本文关键字:上下 移动 多少 一刻      更新时间:2023-09-26

如何在浏览器中找到鼠标位置?

我知道e.pageX , e.pageY,但我想知道鼠标是向上还是向下,(每一刻)移动了多少?但我找不到一种方法来存储最后一个(pageX,pageY),并将其与新的(pageX、pageY)进行比较!

我想做的事:

我有一张跟在老鼠后面的照片。我想知道鼠标向上或向下移动了多少,这样我就可以更改图片大小(如果鼠标向上移动5 像素,我的图片宽度将更改+10 如果鼠标向下移动5&nnbsp;像素,则图片宽度将为-10 像素)。

您可以使用鼠标移动事件。以在鼠标移动时跟踪鼠标坐标。

但是,您应该注意,mousemove事件会占用大量的处理时间,因为光标移动的每个像素都会触发该事件,所以要小心

在这样的事件中触发一个非常简单快速的函数是个好主意。

我希望这就是你想要的

示例:

element.onmousemove = function () {
   // do something simple
};

更新:

对于跟踪,这里有一个样本函数

//初始化函数外的变量,使其保持其值

var x=0;
var y=0;
function trackMouse(e) {
   var evt = e || window.event;
   if (evt.clientX) { // grab the x-y pos if browser is IE
     var tempX = evt.clientX + document.body.scrollLeft;
     var tempY = evt.clientY + document.body.scrollTop;
   }
   else {  // grab the x-y pos.s if browser is else
     var tempX = evt.pageX;
     var tempY = evt.pageY;
   }  
   if (x > tempX){
      // the mouse is going left
   }else {
      // mouse is going right
   }
   x = tempX; // set new value
   if (y > tempY){
      // the mouse is going down
   }else {
      // mouse is going up
   }
   y = tempY;
}

这将帮助您检测鼠标移动的方向