简体   繁体   English

jQuery-使用toggle()但将其禁用,直到完成动画

[英]Jquery - using toggle() but disable it until the animations are done

i am using the jquery toggle function like this: 我正在使用像这样的jQuery切换功能:

$(".class").toggle( function() { //things to do... animations and so on }, function(){ // other things to do... } );

Now when I call the toggle effect by clicking very often the animations of the two functions don't wait for the other to finish. 现在,当我通过频繁单击来调用切换效果时,这两个功能的动画不再等待另一个完成。 So I would like to disable the toggle effects undtil the stuff in each function is finished. 因此,我想禁用切换效果,直到每个函数中的内容完成为止。 So when the first function is called by the toggle the second one should not be called by another click on the .class element. 因此,当第一个函数由切换按钮调用时,第二个函数不应通过再次单击.class元素来调用。 And the other way round 反过来

Any idea how i could do that? 知道我该怎么做吗?

Thanks in advance, Phil 预先感谢,菲尔

You could add a .click() handler bound before the toggle that does this, for example: 您可以执行此操作的切换开关之前添加.click()处理程序绑定,例如:

$(".class").click(function() {
  if($(".selector:animated").length) 
    return false;
}).toggle(function() { 
  //things to do... animations and so on 
}, function(){
  // other things to do... 
});

What this does is if the check for your selector and :animated found any elements (they're still animating), it'd return false and prevent the .toggle() handler from even getting hit. 它的作用是,如果对选择器和:animated的检查找到任何元素(它们仍在设置动画),它将return false并防止.toggle()处理程序被点击。 This works because event handlers are executed in the order they were bound , so if the first one returns false , the second won't run. 之所以可行,是因为事件处理程序按照绑定的顺序执行,因此如果第一个返回false ,则第二个将不会运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM