2011-11-18 32 views
0

我試圖讓一個按鈕無法使用,同時動畫運行。我用按鈕編碼了一個簡單的滾動條,但如果用戶反覆點擊按鈕,動畫不會停止,因此滾動會一遍又一遍地重複。點擊時,我嘗試在元素上使用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")和工程進展順利

+0

綁定將採取兩個參數對嗎? – sathis

+1

可能是duplaicate http://stackoverflow.com/questions/2593195/how-do-i-rebind-the-click-event-after-unbindclick – DoubleYo

+0

謝謝。有用!我在if子句中添加了「.is(」:animated「)」,並修復了它。 –

回答

0

試試這個:

 
//replace this: $this.bind("click"); 

with this: 
$this.unbind('toggle').unbind('click'); 

希望它可以幫助

+0

不起作用。第一次點擊元素時,代碼有效,但按鈕不起作用。我認爲它被解除了束縛。 –

0

你的第二個綁定調用不給要調用的函數,所以這是一個問題

BTW,有你正在嘗試做的叫accordion什麼是偉大的插件,它在jquery ui庫中也是非常有用的。很酷的事情是,也確保只有一個標籤打開在任何時候(如你給的例子)

+0

我正在使用它作爲菜單。主題是使用按鈕滾動內容。 –