jQuery mousemove workaround

怎甘沉沦 提交于 2019-12-02 13:58:44

问题


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't use .hover() method because the area is the window's height and width...

I think about checking the mouse cursor coordinates and check if they changed, but I really don't know where to start.

I reported to Chromium project: http://code.google.com/p/chromium/issues/detail?id=170631


回答1:


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]);
});


来源:https://stackoverflow.com/questions/14363708/jquery-mousemove-workaround

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!