How to tell .hover() to wait?

后端 未结 9 1089
梦谈多话
梦谈多话 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:27

    The general idea is to use setTimeout, like so:

    $('.icon').hover(function() {
               $('li.icon > ul').slideDown('fast');
        }, function() { 
               setTimeout(function() {
                    $('li.icon > ul').slideUp('fast');
               }, 2000);
        });
    

    But this may do counterintuitive things if the user mouses out and then mouses in again quickly—this doesn't account for clearing the timeout when the user hovers over it again. That would require additional state.

提交回复
热议问题