2011-05-13 91 views
0

我不确定如何标题这一个。但我有一个mouseenter/mouseleave行为,分别向上和向下滑动菜单。除非用户多次快速地将鼠标移入和移出元素,否则它的效果非常好。然后,所有的动画被激发,菜单“bezerk”,并继续动画。mouseenter/mouseleave animation chain halt

我想要的是解除mouseenter事件,直到mouseleave事件完成。我尝试了几种不同的方法,包括stop()和stopPropogation(),但它没有奏效。

我得到了这段代码的工作,但它似乎并不“正确”给我。

function add_menu_listener(menu, affected){ 
    $j(menu).mouseenter(function(e){ 
     $j(menu).unbind('mouseenter'); 
     $j(menu + " > ul.links").slideDown(); 
     $j(affected).attr('src', "/images/down_arrow_menu.png"); 
    }).mouseleave(function(e){ 
     $j(menu + " > ul.links").slideUp(500, function(){ 
      add_menu_listener(menu, affected); 
     }); 
     $j(affected).attr('src', "/images/right_arrow_menu.png"); 
    }); 
    } 
+0

反正有你给我们带来的jsfiddle或jsbin链接? – 2011-05-13 17:21:29

+0

希望这个问题可以帮助你:) http://stackoverflow.com/questions/4283674/jquery-mouseenter-mouseleave-html-swap-issue – diEcho 2011-05-13 17:26:40

+0

@oscar这里的链接:http://jsfiddle.net/TBxgP/ – brycemcd 2011-05-13 17:30:47

回答

3

这里更新与.stop(true,true)小提琴加入到动画:http://jsfiddle.net/maniator/TBxgP/1/

代码:

function add_menu_listener(menu, affected){ 
    $j(menu).mouseenter(function(e){ 
     $j(menu + " > ul.links").stop(true,true).slideDown(); 
     $j(affected).attr('src', "/images/down_arrow_menu.png"); 
    }).mouseleave(function(e){ 
     $j(menu + " > ul.links").stop(true,true).slideUp(500); 
     $j(affected).attr('src', "/images/right_arrow_menu.png"); 
    }); 
    } 
+0

啊,这太好了。我正在将stop()应用于事件,而不是受影响的元素。谢谢! – brycemcd 2011-05-13 18:10:55

+0

@Bryce,没问题:-),并记得选择一个“接受答案”,当你可以^ _ ^ – Neal 2011-05-13 18:13:31

0

根据您的文章我想你已经看到了这一点,或者是这样的:

http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

顺便说一句,我建议使用hover()如果你打算使用两个mouseenter()mouseleave()

最终,如果您的解决方案能够正常工作,可读性和可接受性(我认为所有这些都适用于此),那很好。

+0

我之前做过这个,但并没有阻止这种行为。我更喜欢这个语法,但它仍然令人不快。 – brycemcd 2011-05-13 18:01:03