【问题标题】:Delay/Queue between function with animations带有动画的函数之间的延迟/队列
【发布时间】:2015-09-04 13:23:25
【问题描述】:

详情

我有一个里面有动画的函数,当点击按钮时,它会.append 一个divbodydiv 然后将动画其位置向下移动一点。当添加另一个时,所有divs 也会向下移动,因此它们不会重叠。

现在我们遇到了问题!当向按钮发送垃圾邮件以创建 divs 时,它们会重叠。

我们怎样才能阻止这种情况发生?

注意事项:

  • 所以只有当最后一个函数完成所有动画后,下一个调用才能开始
  • 需要记住并运行函数多少次单击按钮
  • 我的插件中的函数有一些需要记住的选项(例如弹出窗口的背景颜色)。

我尝试了什么

我看到它的方式是,我想创建一个队列,用于记录您单击按钮的次数,并在 x 时间过去后释放下一行(足以完成所有动画)。问题是我找不到在 jQuery 中执行此操作的方法。

  • 我已经研究过在 jQuery 中使用 .queue 但没有找到方法 这可以帮助解决这个问题。
  • 我在函数中添加了setTimeout,但这会导致点击延迟

我觉得这是一件非常简单的事情,但我自己搞砸了几个小时后找不到解决方案。

演示

我为这个问题创建了一个演示,我的真实版本是一个插件,因此它有自己的功能。这只是一个非常基本的版本。

$("button").click(function() {
  $("body").append("<div></div>");

  $($("div").get().reverse()).each(function(index) {
    $(this).animate({
      top: "+=120px"
    }, {
      duration: 600,
      queue: false
    });

    if (index >= 2) {
      // Fade out and then remove the item
      $(this).fadeOut("fast", function() {
        $(this).remove();
      });
    }
  });
});
div {
  width: 400px;
  height: 50px;
  background: rgba(0, 0, 0, .4);
  position: absolute;
  top: 0;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>

JSFiddle Demo

【问题讨论】:

  • 您是要在队列中添加动画还是在 previos 完成之前不允许新动画?
  • @LazarevAlexandr 我想记住该函数已被调用,并且仅在之前运行的函数完成后才启动下一个函数(连同其所有动画)。
  • 所以实际上它是一个队列:)

标签: javascript jquery animation


【解决方案1】:

试试这个解决方案....阻止动画直到动画回调

animated = false;
$("button").click(function() {
  if(animated)
     return false;
  else
    animated = true;
  $("body").append("<div></div>");

  $($("div").get().reverse()).each(function(index) {
    $(this).animate({
      top: "+=120px",
      duration: 600,
      queue: false
    },function(){ animated = false; });

    if (index >= 2) {
      // Fade out and then remove the item
      $(this).fadeOut("fast", function() {
        $(this).remove();
        
      });
    }
  });
});
div {
  width: 400px;
  height: 50px;
  background: rgba(0, 0, 0, .4);
  position: absolute;
  top: 0;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>

这可能是管理动画队列的起点。

看看这个fiddle

【讨论】:

  • 它更接近,但这还没有计算您完成的点击次数。我需要记住您单击按钮 x 次并按顺序运行该功能。
  • 在我的插件中,该函数有选项,所以我也需要记住这一点。
【解决方案2】:

将动画添加到队列中:

var freeToAnimate = true;
var animationQueue = [];
var i = 0;
$("button").click(function () {   
    var queueLength = animationQueue.length;    
    animationQueue.push(i);
    i++;  
    if(queueLength == 0){        
        animateDivs(i-1);  
    } else {   
        return false;                  
    }
});

function animateDivs(animationIndex) {     
      if(freeToAnimate == true) {
          freeToAnimate = false;
          var divsAmount = $("div").length;
          $("body").append("<div></div>");   
          $($("div").get().reverse()).each(function(index, el) {          
              if(index >= 2) {
                  // Fade out and then remove the item
                  $(this).fadeOut( "fast", function() {
                      $(this).remove();       
                  });
              }       

              $(this).animate({
                  top: "+=120px"               
              }, 1200, function() {
                  var indx = animationQueue.indexOf(animationIndex);
                  animationQueue.splice(indx, 1);               
                  if(index == divsAmount-1 || divsAmount == 0) {                    
                      freeToAnimate = true;  
                      if(animationQueue.length != 0) {
                          animateDivs(animationIndex+1);
                      }                
                  }                 
              });         
         });       
      }          
}      

https://jsfiddle.net/4zwmm20e/12/

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2022-08-13
    • 2018-12-21
    相关资源
    最近更新 更多