2013-04-25 79 views
-1
$('.showmenu').bind("mouseenter", function (e) { 
       $('ul.secondul').show(); 
}); 
$('.showmenu').bind("mouseleave", function (e) { 
       $('ul.secondul').hide(); 
}); 

是否有办法让第二个函数不会立即启动,但在1秒的延迟之后?因此,当用户不小心将鼠标移出下拉菜单选项卡时,该选项卡将不会立即关闭,并让用户有机会将鼠标移回。因为它是一个类,你可以有更多的.showmenu项目使用jquery设置延迟或超时

$('.showmenu') 
.bind("mouseenter", function (e) { 
    if($(this).data("timer")) 
     clearTimeout($(this).data("timer")); 
    $('ul.secondul').show(); 
}) 
.bind("mouseleave", function (e) { 
    $(this).data("timer", setTimeout(function(){ 
     $('ul.secondul').hide(); 
    }, 1000)); 
}); 

使用$(this).data

+0

使用悬停:http://api.jquery.com/hover/ – 2013-04-25 18:50:10

+0

http://stackoverflow.com/questions/2939980/jquery-how-to-sleep-or-delay的重复和所以很多其他。 – Colleen 2013-04-25 18:51:21

+0

@Colleen你错了。在这种情况下,Delay()不起作用。 – Jerry 2013-04-25 18:52:21

回答

1

你可以做到这一点。

1

你可以做这样的事情 -

vat timeout; 
$('.showmenu').bind("mouseenter", function (e) { 
       $('ul.secondul').show(); 
       clearTimeout(timeout); 
}); 
$('.showmenu').bind("mouseleave", function (e) { 
      timeout = setTimeout(function(){ 
      $('ul.secondul').hide(); 
      },1000); 
}); 
0

我相信jQuery的悬停是你想要的内容:

$('.showmenu').hover(
    function() { 
     $('ul.secondul').show(); 
    }, 
    function() { 
     $('ul.secondul').hide(); 
    } 
); 
+0

这里没有任何延迟。 – showdev 2013-04-25 18:54:33

+0

因为它解决了所述问题,所以不需要这么做:“因此,当用户意外地将鼠标移出下拉菜单选项卡时,该选项卡将不会立即关闭,并让用户有机会将鼠标移回其上“。试试看。 – 2013-04-25 19:14:36

+0

当用户“将鼠标移出”时,我看不到您的代码如何使该选项卡“不能立即关闭”。 http://jsfiddle.net/7VcwF/1/由于OP没有提供HTML,也许我误解了“下拉菜单选项卡”的含义。 – showdev 2013-04-25 19:34:25

1

如果使用fadeIn()和​​(或其他动画),您可以用delay()这样连锁吧:

$('.showmenu').hover(

    function() { 
    $('ul.secondul').stop(true,true).fadeIn(200); 
    }, 

    function() { 
    $('ul.secondul').stop(true, true).delay(1000).fadeOut(200); 
    } 

); 

小提琴:http://jsfiddle.net/7VcwF/