当鼠标停止移动时执行Jquery [英] execute Jquery when the mouse stops moving

查看:116
本文介绍了当鼠标停止移动时执行Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跟踪光标的快速脚本:

I have a quick script that has a trail follow the cursor:

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
       $('.fall').each(function(){
           if ($(this).css("opacity") == 0){
               $(this).remove();
           };
       });
       t = (e.pageY - 10).toString() + 'px';
       l = (e.pageX - 10).toString() + 'px';
       $('.fall').css("margin_left",l);
       $('.fall').css("margin_top",t);
       var doit = '<div class="fall" style="position:fixed;margin-left:' + l + ';margin-top:' + t + ';">+</div>'
       $('body').prepend(doit);
      $('#status2').html(e.pageX +', '+ e.pageY);

       $('.fall').animate({
           marginTop: '+=50px',
           opacity: 0
       },1000);       
   }); 
});

现在我想删除 animate 当鼠标不移动时,部分并具有以下内容:

Now I would like to remove the animate part and have something like the following when the mouse is not moving:

$('.fall').each(function(){
    $(this).fadeOut('slow');
    $(this).remove()
});

当鼠标不移动时,我只是无法弄清楚如何执行此操作一秒。有什么想法吗?

I just can't figure out how to execute this when the mouse is not moving for more than like a second. Any ideas?

谢谢,这里是一个jsfiddle

推荐答案

这是你需要的吗? jsFiddle

lastTimeMouseMoved = new Date().getTime();
var t = setTimeout(function() {
  var currentTime = new Date().getTime();
  if (currentTime - lastTimeMouseMoved > 1000) {
    $('.fall').fadeOut('slow');
    // $('.fall').remove();
  }
}, 1000)

这篇关于当鼠标停止移动时执行Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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