How to tell .hover() to wait?

后端 未结 9 1063
梦谈多话
梦谈多话 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.

提交回复
热议问题