我試圖讓一個按鈕無法使用,同時動畫運行。我用按鈕編碼了一個簡單的滾動條,但如果用戶反覆點擊按鈕,動畫不會停止,因此滾動會一遍又一遍地重複。點擊時,我嘗試在元素上使用unbind
,並且在動畫完成後再次嘗試使用bind
,但它看起來不起作用。這裏是我的代碼:jquery解除綁定按鈕,而.animate()正在運行
$(".go_up").bind('click',function(){
var $this = $(this);
var $content_page = $this.siblings('.componentin').children('.item-page');
var $content_math = $content_page.height();
var $content_height = ($content_math)*(-1);
var $content_margin = $content_page.css("margin-top");
if (($content_margin) <= '0') {
$this.unbind("click");
$content_page.animate({ marginTop: "+=100px" }, "slow", function(){
$this.bind("click");
});
}
});
這裏是例子:www.erincfirtina.com/bar/
[固定]
$(".go_up").bind('click',function(){ var $this = $(this); var $content_page = $this.siblings('.componentin').children('.item-page'); var $content_math = $content_page.height(); var $content_height = ($content_math)*(-1); var $content_margin = $content_page.css("margin-top"); if ((($content_margin) <= '0') && (!$content_page.is(":animated"))){ $content_page.animate({marginTop: "+=100px"}, "slow"); } });
添加.is(":animated")
和工程進展順利
綁定將採取兩個參數對嗎? – sathis
可能是duplaicate http://stackoverflow.com/questions/2593195/how-do-i-rebind-the-click-event-after-unbindclick – DoubleYo
謝謝。有用!我在if子句中添加了「.is(」:animated「)」,並修復了它。 –