【问题标题】:jQuery mousemove workaroundjQuery mousemove 解决方法
【发布时间】:2013-09-26 21:28:51
【问题描述】:

我正在寻找 Chrome 上 .mousemove() 方法的解决方法。这是一个已知问题。

即使鼠标静止,Chrome 也会反复触发 mousemove。

我不能使用 .hover() 方法,因为该区域是窗口的高度和宽度...

我想检查一下鼠标光标坐标并检查它们是否改变了,但我真的不知道从哪里开始。

我向 Chromium 项目报告: http://code.google.com/p/chromium/issues/detail?id=170631

【问题讨论】:

  • 你能再解释一下吗?或包括您的代码?有一些变通方法,但要给出最佳答案,我们需要更多背景信息。
  • Chrome 在其他浏览器运行良好时遇到了一些类似的问题。尝试运行此链接底部的代码来跟踪你的鼠标,看看它在不移动时是否在移动,JQuery .mouseoverPS。我知道这不是答案,我不知道如何在问题下发布 cmets:-(
  • 托尼:给你! jsfiddle.net/Q7HhT
  • 只是澄清一下,因为我的法语很弱......你希望菜单在鼠标移动时显示,在不移动时隐藏,对吗?顺便说一句,我正在使用 Chrome,并且您的 mousemove 功能按预期工作。
  • 是的,你说得对。预期的行为: > 光标没有在#main > #menu hide 上移动,这还不错。但是如果你保持光标不动,在某些时候——至少在我的工作环境中——菜单会在某个时候再次显示。

标签: google-chrome mousemove


【解决方案1】:

Chrome 版本 29.0.1547.66 m 中仍然存在问题,并寻找“真正的”解决方案。到目前为止没有发现任何东西。 就像你说的,我开始写一个函数来检查鼠标是否真的移动了。

特别是,您可以根据自己的需要更改一些设置。

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

【讨论】:

    猜你喜欢
    • 2012-10-22
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2012-01-09
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多