【问题标题】:jquery disable click until animation is fully completejquery禁用点击直到动画完全完成
【发布时间】:2026-02-02 02:55:01
【问题描述】:

我想禁用点击功能,直到其中的每个代码;初始化并完成。

我阅读了这些文章并尝试了他们所说的但无法实现: event.preventDefault() vs. return false + Best way to remove an event handler in jQuery? + jQuery - Disable Click until all chained animations are complete

我为这个问题准备了简单的例子:

This is DEMO

html

<div class="ele">1</div>
<div class="ele">2</div>
<div class="ele">3</div>

jQuery

$('.ele').click(function() {

    if ( !$('.ele').is(':animated') ) {//this works too but;
                         //is:animate duration returns = 2000 but need to be 4000
        $('.ele').stop().animate({'top':'0px'},2000);
        $(this).stop().animate({'top':'100px'},4000);
    } 
    return false;
});
​

【问题讨论】:

    标签: javascript jquery animation click bind


    【解决方案1】:

    使用 on() 和 off() 开启/关闭点击功能:

    $('.ele').on('click', animateEle);
    
    function animateEle(e) {
        $('.ele').stop().animate({'top':'0px'},2000);
        $(e.target).off('click').stop().animate({'top':'100px'},4000, function() {
            $(e.target).on('click', animateEle);
        });
    }​
    

    DEMONSTRATION

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      var exec_a1 = true, exec_a2 = true;
      $('.ele').click(function() {
          if (exec_a1 && exec_a2) {
              exec_a1 = false;
              exec_a2 = false;
      
              $('.ele').stop().animate({'top': '0px'}, 2000, function() {
                  exec_a1 = true;
              });
              $(this).stop().animate({'top': '100px'}, 4000, function() {
                  exec_a2 = true;
              });
          }
          return false;
      });
      

      【讨论】: