如何检测mousemove何时停止 [英] How to detect when mousemove has stopped

查看:122
本文介绍了如何检测mousemove何时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mousemove 完成时,如何侦测eventListener?

  document.AddEventListener('mousemove',startInteractionTimer,false); 

function startInteractionTimer(){
clearInterval(touchInterval);
touchInterval = setInterval(noAction,6000);

我想要启动函数 startInteractionTimer 在鼠标移动结束后立即生效,我想赶上这一点。在上面的代码示例中,如果鼠标移动,它就会启动。



感谢

编辑:,好吧,我回答了我自己的问题和上面的脚本 - ^就好了。

您可以总是为它做一个自定义事件:

pre $ $ $ $ $ $ $ $ (document).on('mousemove',function(event){
if(timeout!== undefined){
window.clearTimeout(timeout);
}
timeout = window.setTimeout(function(){
//触发event.target上的新事件,以便它可以适当地冒泡
$(event.target).trigger('mousemoveend');
},100);
});
}(jQuery));

现在您可以执行此操作:

<$ ('mousemoveend',function(){
...
}); pre> $('#my-el')。

编辑:

此外,为了与其他jQuery事件保持一致:

 (function($){
$ .fn。 mousemoveend = function(cb){
return this.on('mousemoveend',cb);
});
}(jQuery));

现在您可以:

  $( '#我-EL')mousemoveend(FN)。 


How is it possible to detect with an eventListener when mousemove has finished?

document.AddEventListener('mousemove', startInteractionTimer, false);

function startInteractionTimer(){
  clearInterval(touchInterval);
  touchInterval = setInterval(noAction, 6000);
}

I want to start the function startInteractionTimer immediately after the mousemove has ended and I would like to catch that. On the code example above, it is starting if the mouse is moved.

Thanks

Edit: Alright, I answered my own question and the script above --^ is just fine.

解决方案

You could always make a custom event for it:

(function ($) {
    var timeout;
    $(document).on('mousemove', function (event) {
        if (timeout !== undefined) {
            window.clearTimeout(timeout);
        }
        timeout = window.setTimeout(function () {
            // trigger the new event on event.target, so that it can bubble appropriately
            $(event.target).trigger('mousemoveend');
        }, 100);
    });
}(jQuery));

Now you can just do this:

$('#my-el').on('mousemoveend', function () {
    ...
});

Edit:

Also, for consistency with other jQuery events:

(function ($) {
    $.fn.mousemoveend = function (cb) {
        return this.on('mousemoveend', cb);
    });
}(jQuery));

Now you can:

$('#my-el').mousemoveend(fn);

这篇关于如何检测mousemove何时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆