2013-11-27 86 views

回答

3

您必須在您的jQuery動畫中使用stop()來模擬CSS動畫。

它具有以下參數,.stop([clearQueue ] [, jumpToEnd ]),和你想要的是清除隊列中,而不是直接到終點,所以這將是stop(true, false)

$('.box1').on('mouseenter mouseleave', function(e) { 
    $('.box2').stop(true, false).animate({opacity: e.type=='mouseenter'? 1 : 0}, 1500) 
}); 

FIDDLE

或不透明度

$('.box1').on('mouseenter mouseleave', function() { 
    $('.box2').stop(true, false).fadeToggle(1500) 
}); 

FIDDLE

+0

@thenewseattle - 這是一個事件處理程序,觸發對的mouseenter和鼠標離開事件,並在進出等等看的方法上來就jQuery的網站,如果你想褪色的元素瞭解更多關於他們如何工作的信息,讓它像CSS動畫一樣工作的相關部分就是stop()方法。 – adeneo

+0

**你是個天才。**非常感謝你! – thenewseattle

+0

謝謝,不客氣! – adeneo

0

這裏你去。

http://jsfiddle.net/defmetalhead/Q5cRU/2/

$(".box1").on({ 
     mouseover: function() { 
      $('.box2').stop().fadeIn('slow'); 
     }, 
     mouseleave: function() { 
      $('.box2').stop().fadeOut('slow'); 
     } 
    }); 
+0

如果將鼠標移開並移開,該動畫會繼續。我想這就是他想要避免的。 –

+0

沒有指定。此外,你只需使用.stop() –

+0

所以,簡單的修復。 IT尚不明確。你只是提到停止() –