Jquery - 使用toggle(),但在动画完成之前禁用它

发布于 2024-09-15 16:19:49 字数 297 浏览 3 评论 0原文

我正在使用 jquery 切换函数,如下所示:

$(".class").toggle( 功能() { //要做的事情...动画等等 }, 功能(){ // 其他要做的事情... } );

现在,当我经常通过单击调用切换效果时,两个函数的动画不会等待另一个函数完成。所以我想禁用切换效果,直到每个函数中的内容完成。因此,当通过切换调用第一个函数时,第二个函数不应通过再次单击 .class 元素来调用。反过来

你知道我该怎么做吗?

提前致谢, 菲尔

i am using the jquery toggle function like this:

$(".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. And the other way round

Any idea how i could do that?

Thanks in advance,
Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2024-09-22 16:19:49

您可以在之前添加 .click() 处理程序绑定 执行此操作的切换开关,例如:

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

它的作用是检查您的选择器和 :animated 发现任何元素(它们仍在动画),它会返回 false 并阻止 .toggle() 处理程序甚至不会受到攻击。这是可行的,因为事件处理程序按照它们绑定的顺序执行,因此如果第一个返回false,第二个将不会运行。

You could add a .click() handler bound before the toggle that does this, for example:

$(".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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文