2009-10-06 79 views
3

我有一個切換淡入淡出效果,但是當您多次單擊按鈕時,它會淡入淡出多次。 (所以,如果我快速點擊20倍的淡入淡出效果,它會淡入淡出多次)我將如何阻止這一點,並使其只能在動畫完成時切換淡出效果?我試圖獲得的電流值,使一個if語句,但它不工作:*(停止jQuery功能多次作用

感謝

jQuery.fn.fadeToggle = function(speed, easing, callback) { 
    return this.animate({opacity: 'toggle'}, speed, easing, callback); 
}; 



$(document).ready(function() { 
    $('.open').click(function() { 
    $('#fadeMe').next().fadeToggle('slow'); 
    }); 
}); 

回答

6

您需要檢查,如果該元素是不是:動畫,如果是這樣調用.animate :

<body> 
    <button class="open">open</button><br> 
    <div id="fadeMe">fade me!</div> 
    <style>#fadeMe { 
    width:20em; 
    height:10em; 
    background:black; 
    color:#fff; 
    }</style> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
    <script> 
    jQuery.fn.fadeToggle = function(speed, easing, callback) { 
     return this.animate({opacity: 'toggle'}, speed, easing, callback); 
    }; 



    $(document).ready(function() { 
     $('.open').click(function() { 
     var el = $('#fadeMe').next(); 
     if (!el.is(':animated')) { 
      $(el).fadeToggle('slow'); 
     } 
     }); 
    }); 
    </script> 
    </body> 

演示:http://jsbin.com/irare

+0

更新的解決方案。 – 2009-10-06 00:55:53

+0

Sweet it works :) – PHPNooblet 2009-10-06 01:02:45

+0

這回答了這個問題......但是看下面的stop()答案......這就是大多數人在這種情況下想要的。 – ndp 2009-10-06 02:45:38

3

使用.stop()這將停止所有動畫元素上

$('#fadeMe').next().stop().fadeToggle('slow');