【问题标题】:Webkit and Safari fire mousemove even when mouse doesn't move即使鼠标不动,Webkit 和 Safari 也会触发 mousemove
【发布时间】:2010-04-10 09:44:22
【问题描述】:

我已经阅读了有关在 Safari/Webkit 中触发两次 mousemove 事件的问题,但我面临的问题是即使在鼠标移动时mousemove 也会触发.也就是说:当鼠标光标位于加载/刷新页面时事件附加到的上下文上方时,它已经触发。因为我将它附加到document(浏览器的整个视口),所以它会立即在 Safari 中触发。我尝试将它附加到html 元素、body 和包装器div。没有变化。

$(document).bind('mousemove', function() {
  alert('Mouse moved!');
  $(document).unbind('mousemove');
});

在其他浏览器中确实可以正常工作。有人看到我做错了吗?谢谢。

【问题讨论】:

标签: javascript jquery jquery-events mousemove


【解决方案1】:

我知道这是一个肮脏的 hack,但至少是这样:只需在第二次触发 mousemove 时执行回调函数。

var $document = $(document), i = 0;
$document.bind('mousemove', function() {
 if (++i > 1) {
  alert('Mouse moved!');
  $document.unbind('mousemove');
 };
});

演示:http://fiddle.jshell.net/Yz5Bd/show/light/

不管怎样,Safari 并不是唯一一个在页面加载时触发 mousemove 的浏览器——IE 也是如此。无论如何,使用其他浏览器的人不会注意到“延迟”,因为几乎不可能将鼠标只移动一个像素。

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多