2011-11-18 42 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

我正在使用它作为菜单。主题是使用按钮滚动内容。 –