jQuery mousemove workaround

前端 未结 1 1385
既然无缘
既然无缘 2021-01-24 07:02

I\'m looking for a workaround for .mousemove() method on Chrome. It\'s a known issue.

Chrome keeps firing mousemove repeatedly even if the mouse is still.

I can\

相关标签:
1条回答
  • 2021-01-24 07:51

    Still got the Problem in Chrome Version 29.0.1547.66 m and searched for a "real" solution. Nothing found so far. Like you said, i start to write a function to check if the mouse are really moved or not.

    Specially, you can change some settings for your own needs.

    var my_mouseMovements = {
        readNewPos : true, //indicates if time since last check are elapsed
        oldPos : [0,0], //pos from the last check
        minX_Distance: 10, //only look at movements thats are big enough(px)
        minY_Distance: 10, //only look at movements thats are big enough(px)
        timeBetweenEachCheck : 100, //Just checking every x ms for movements
        saveNewPos : function(pos,callback){
        if(this.readNewPos === true)
        {
            this.readNewPos = false;
    
    
            var xDistance = pos[0] - this.oldPos[0];
            var yDistance = pos[1] - this.oldPos[1];
            //Just making the distances positive
            xDistance = xDistance < 0 ? -xDistance : xDistance;
            yDistance = yDistance < 0 ? -yDistance : yDistance;
    
            //Check if mouse moved more then expected distance
            if(xDistance >=  this.minX_Distance || yDistance >= this.minY_Distance)
            {
                console.log("Mouse has moved a lot since last check!");
                if(callback !== undefined)
                {
                    callback();
                }
            }
    
            this.oldPos = pos;
    
            var that = this;
            //reset readNewPos after a while
            setTimeout(function(){
                that.readNewPos = true;
            },this.timeBetweenEachCheck);
        }
        else //Just for debug right now
        {
            console.log("the last time was not far away");
        }
    }
    };
    
    jQuery('body').mousemove(function(e){
        my_mouseMovements.saveNewPos([e.pageX,e.pageY]);
    });
    
    0 讨论(0)
提交回复
热议问题