【问题标题】:How to progressively increase the number of animated DIVs如何逐步增加动画 DIV 的数量
【发布时间】:2014-02-01 21:59:19
【问题描述】:

我试图在按下按钮时多次为 div 框设置动画。右转,再右转,再往下一点,里面的文字就变了,再左转,再左转原处。这是我的代码:http://jsfiddle.net/LSegC/ 希望现在一切都好。

$(document).ready(function(){
  $("button").click(function(){
  var d=$("#t");
  var number=$("#number1").val();
  var speed=2000;

//  if(state==true){
        d.animate({left:'+=230px'}, speed);
        d.animate({left:'+=230px'}, speed);
        d.animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4, "swing", function(){
            $('#span').fadeOut(500, function() {
                $(this).text('a1').fadeIn(500);
            });
        });
        d.delay(1000).animate({left:'-=230px'}, speed);
        d.animate({left:'-=230px'}, speed);
        d.fadeOut();

//        }
  });
});

现在我想知道如何做的是,当上述步骤完成后,我想增加动画 div 的数量。所以下一次,我想让两个这样的 div 相互跟随动画,当它们完成后,它们中的三个应该去,每个显示自己的数字。有没有人知道如何逐步增加动画 DIV 的数量? -谢谢

【问题讨论】:

  • 我将假设您编码包装链接的原因是因为您收到一条错误消息,指出 JSFiddle 链接应附有代码。这意味着您在问题中引用的代码应该在这里,在这个问题中。这并不意味着将您的链接直接包装在代码标记中。
  • 嗯,我明白了!是的,按道理应该是这样的。已编辑!

标签: javascript jquery


【解决方案1】:

如果您切换到 #t 的类而不是 id,那么这很容易。试试这样的:

$(document).ready(function(){
  $("button").click(function() {

      var d = $(".t").fadeIn();
      var speed = +$("#number1").val();

      d.animate({left:'+=230px'}, speed);
      d.animate({left:'+=230px'}, speed);
      d.animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4, "swing", function() {
          $('.span', this).fadeOut(100, function() {
              $(this).text(function() {
                  return 'a' + $(this).text().replace('a', '');
              }).fadeIn(100);
          });
      });
      d.delay(1000).animate({left:'-=230px'}, speed);
      d.animate({left:'-=230px'}, speed);
      d.fadeOut().promise().done(function() {
          d.last().after(function() {

              var top = +$(this).css('top').replace('px', ''),
                  number = +$(this).data('number') + 1,
                  $clone = $(this).clone();

              $clone.data('number', number).css('top', top + 20);
              $clone.find('.span').text(number);

              return $clone;
          });

          d.find('.span').text(function() {
              return $(this).text().replace('a', '');
          });
      })
  });
});

演示:http://jsfiddle.net/LSegC/1/

【讨论】:

  • 嗯,看起来很有希望。我想让它不是线性增加,而是指数增加,但它不起作用。像 1 个数据包,2、4、8,...我试图把它放在一个循环中,但不工作!
猜你喜欢
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2014-10-18
  • 2016-06-28
  • 2013-11-05
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
相关资源
最近更新 更多