如何防止 JQuery 排队(链接)

发布于 2024-09-30 23:56:57 字数 367 浏览 0 评论 0原文

场景:您可以单击一个按钮,一些内容将向左移动(.animate)。 当您快速单击按钮约 3 次时,JQuery ques 3 .animate 将按顺序排列。

我不想那样!我将阻止这种排队,因此该按钮仅在动画完成时才起作用。 谢谢你的每一个云!

伪代码:

IF #Content 不是动画(静止不动)

THEN On Click Button

Animate Contet CSS "left" + 900px

ELSE 您现在无法单击该按钮。但是如果你点击按钮,那么它就不会出现另一个动画。

Scenario: You can click a Button and some Content will move (.animate) left.
When you click the button fast for about 3 times then JQuery ques 3 .animate left's in the order.

I dont want that! I will prevent that queing so the button will only work when the animation is done.
Thank you for every clou!

Pseudo Code:

IF #Content is NOT animated (stands still)

THEN On Click Button

Animate Contet CSS "left" + 900px

ELSE You can't click the button now. But ff you click the butto,n then it wont que another animation.

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

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

发布评论

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

评论(2

春风十里 2024-10-07 23:56:57

您可以使用 :animated 选择器 来运行基于元素的动画状态。

if( $('#Content').is(':not(:animated)') ) {
    // run your animation
}

这也使用 :not() 选择器,以及 .is() 方法 返回布尔值。

或者,您可以在动画开始时向按钮添加一个类,并使用该类作为标志来确定动画是否应该运行。

该类还可以设置按钮的样式,以提供不应再次单击该按钮的反馈。

$('#myButton').click(function() {
    $button = $(this);
    if( !$button.hasClass( 'disabled' ) ) {
        $button.addClass( 'disabled' );
        $('#Content').animate({left: '+=900'}, function() { $button.removeClass('disabled') });
    }
});

You can use the :animated selector to run code based on the animated state of an element.

if( $('#Content').is(':not(:animated)') ) {
    // run your animation
}

This also uses the :not() selector, and the .is() method to return a boolean.

Or you could add a class to the button when the animation starts, and use that class as a flag to determine if the animation should run.

The class could also style the button to give feedback that it shouldn't be clicked again.

$('#myButton').click(function() {
    $button = $(this);
    if( !$button.hasClass( 'disabled' ) ) {
        $button.addClass( 'disabled' );
        $('#Content').animate({left: '+=900'}, function() { $button.removeClass('disabled') });
    }
});
甲如呢乙后呢 2024-10-07 23:56:57

您始终可以在按下时取消绑定 click 事件。然后将回调传递给animate,一旦动画播放,它将重新绑定click已完成。

You could always unbind the click event when it is pressed. Then pass a callback to animate that will re-bind click once the animation is complete.

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