首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画函数之间的延迟/队列

动画函数之间的延迟/队列
EN

Stack Overflow用户
提问于 2015-09-04 13:23:25
回答 2查看 93关注 0票数 3

详细信息

我有一个内有动画的函数,当单击按钮时,它将.append一个divbody。然后div会使它的位置向下移动一点。当另一个被添加时,所有的div也会向下移动,这样它们就不会重叠。

现在我们遇到问题了!当垃圾邮件按钮创建div时,它们将重叠。

我们怎样才能阻止这一切的发生?

备注:

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

我试过的

在我看来,我想要创建一个队列,用于您单击该按钮多少次,并在x的时间已经过去(足够完成所有动画)之后按行释放下一个按钮。问题是我无法在jQuery中找到这样的方法。

  • 我已经研究过在.queue中使用jQuery,但是找不到一种方法可以帮助解决这个问题。
  • 我已经在函数上放置了一个setTimeout,但是这会导致单击延迟。

我觉得这是一件很简单的事情,但经过几个小时的搅乱,我自己找不到解决办法。

演示

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

代码语言:javascript
复制
$("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();
      });
    }
  });
});
代码语言:javascript
复制
div {
  width: 400px;
  height: 50px;
  background: rgba(0, 0, 0, .4);
  position: absolute;
  top: 0;
  right: 0;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>

JSFiddle演示

EN

回答 2

Stack Overflow用户

发布于 2015-09-04 13:34:01

试试这个解决方案..。防止动画,直到动画回调

代码语言:javascript
复制
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();
        
      });
    }
  });
});
代码语言:javascript
复制
div {
  width: 400px;
  height: 50px;
  background: rgba(0, 0, 0, .4);
  position: absolute;
  top: 0;
  right: 0;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>

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

看看这个小提琴

票数 3
EN

Stack Overflow用户

发布于 2015-09-04 14:09:58

向队列中添加动画:

代码语言:javascript
复制
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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32399213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档