How to tell .hover() to wait?

后端 未结 9 1004
梦谈多话
梦谈多话 2020-11-29 17:36

I have a drop down menu. Now when it\'s slided down to multiple levels, I\'d like it to add wait time for like 2 secs, before it disappears, so the user can get back in, whe

相关标签:
9条回答
  • 2020-11-29 18:28

    This will make the second function wait 2 seconds (2000 milliseconds) before executing:

    $('.icon').hover(function() {
        clearTimeout($(this).data('timeout'));
        $('li.icon > ul').slideDown('fast');
    }, function() {
        var t = setTimeout(function() {
            $('li.icon > ul').slideUp('fast');
        }, 2000);
        $(this).data('timeout', t);
    });
    

    It also clears the timeout when the user hovers back in to avoid crazy behavior.

    This is not a very elegant way of doing this, however. You should probably check out the hoverIntent plugin, which is designed to solve this particular problem.

    0 讨论(0)
  • 2020-11-29 18:28
    var timer;
    
    var delay = 200;
    
    $('#hoverelement').hover(function() {
    
        on mouse hover, start a timeout
    
        timer = setTimeout(function() {
    
           Do your stuff here 
    
        }, delay);
    
    }, function() {
    
       Do mouse leaving function stuff here    
    
        clearTimeout(timer);
    });
    

    //edit: instert code

    0 讨论(0)
  • 2020-11-29 18:31

    or you could simply use transition:all 2s ease-in-out. make sure that you add -webkit, -moz and -o for different browsers.

    0 讨论(0)
提交回复
热议问题