2016-11-30 68 views
0

几个指定的HTML文章:防止事件(这)元素一样

<article id="someId" class="some_wrapper"> 
    <section class="info_wrapper"> 
     <section class="info"> 
      <p>Content</p> 
     </section> 
    </section> 
</article> 

Cobined一些基本的jQuery的,如:

$(".some_wrapper").mouseenter(function(){ 
    $(this).find(".info").fadeIn(500); 
}); 

$(".some_wrapper").mouseleave(function(){ 
    $(this).find(".info").fadeOut(500); 
}); 

问题:如果用户移动鼠标快速severel倍从.some_wrapper到另一个,事件处理程序多次触发并建立队列fadeIn()和​​的效果。即使老鼠已经站在集装箱外面,只要经常发生这种情况,这些情况就会发生。

如何防止和mouseleave()上的$(this)元素,其中.info可见。或者打破队列?

感谢。

回答

4

您需要触发淡入或拔出事件

$(".some_wrapper").mouseenter(function(){ 
    $(this).find(".info").stop().fadeOut(500); 
    $(this).find(".info").fadeIn(500); 
}); 

$(".some_wrapper").mouseleave(function(){ 
    $(this).find(".info").stop().fadeIn(500); 
    $(this).find(".info").fadeOut(500); 
}); 

这将停止所有先前触发的事件,并执行最新event.So repeatation不发生使用stop()函数。

+0

不错!那有多容易!非常感谢你。 –

0
$(".some_wrapper").mouseenter(function(){ 
    if($(this).find(".info").is(":visible") === true) { 
     $(this).find(".info").fadeIn(500); 
    } 
}); 

$(".some_wrapper").mouseleave(function(){ 
    if($(this).find(".info").is(":visible") === false) { 
     $(this).find(".info").fadeOut(500); 
    } 
}); 
+0

使用jquery .stop()函数可以更好地处理它,而无需添加更多检查 –