execute Jquery when the mouse stops moving

前端 未结 2 1710
夕颜
夕颜 2021-02-08 06:10

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

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
       $(\'.fall\').each(funct         


        
相关标签:
2条回答
  • 2021-02-08 06:33

    You add a timeout that fires after one second of inactivity, and clear the timeout if the mouse moves within 1 second etc :

    var timer;
    $(document).on('mousemove', function(e){
       clearTimeout(timer);
    
       timer = setTimeout(function() {
           $('.fall').fadeOut('slow', function() {
               $(this).remove();
           });
       }, 1000);
    });
    

    FIDDLE

    EDIT:

    Here's how I'd do it

    FIDDLE

    0 讨论(0)
  • 2021-02-08 06:35

    is this what you require? jsFiddle

    lastTimeMouseMoved = new Date().getTime();
    var t = setTimeout(function() {
      var currentTime = new Date().getTime();
      if (currentTime - lastTimeMouseMoved > 1000) {
        $('.fall').fadeOut('slow');
        // $('.fall').remove();
      }
    }, 1000)
    
    0 讨论(0)
提交回复
热议问题