首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery中的循环嵌套动画函数

jQuery中的循环嵌套动画函数
EN

Stack Overflow用户
提问于 2016-04-21 12:57:38
回答 2查看 52关注 0票数 1

所以我想转换这段代码:

代码语言:javascript
复制
$('.spawn_worm').animate({
    left: '+=20px',
    top: '-=20px'
},100, "linear", function(){
    $('.spawn_worm').animate({
        left: '+=20px',
        top: '-=16px'
    },100, "linear", function(){
        $('.spawn_worm').animate({
            left: '+=20px',
            top: '-=12px'
        },100, "linear", function(){
            $('.spawn_worm').animate({
                left: '+=20px',
                top: '-=8px'
            },100, "linear", function(){
                $('.spawn_worm').animate({
                    left: '+=20px',
                    top: '-=4px'
                },100, "linear", function(){
                    $('.spawn_worm').animate({
                        left: '+=20px',
                        top: '+=0px'
                    },100, "linear", function(){
                        $('.spawn_worm').animate({
                            left: '+=20px',
                            top: '+=4px'
                        },100, "linear", function(){
                            $('.spawn_worm').animate({
                                left: '+=20px',
                                top: '+=8px'
                            },100, "linear", function(){
                                $('.spawn_worm').animate({
                                    left: '+=20px',
                                    top: '+=8px'
                                },100, "linear", function(){
                                    $('.spawn_worm').animate({
                                        left: '+=20px',
                                        top: '+=12px'
                                    },100, "linear", function(){
                                        $('.spawn_worm').animate({
                                            left: '+=20px',
                                            top: '+=16px'
                                        },100, "linear", function(){

                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

做一些不那么愚蠢的事。因为这需要我花一年的时间来完成,而且因为它有很多代码,所以我认为可以用循环来解决它。

我希望动画函数属性left总是相同的+=20px。属性top-20px中开始并增加到180px,然后再次减少到180px并在(window.width)/20循环后完成。

这是可能的吗?感谢并抱歉提出了新手问题(:

EN

回答 2

Stack Overflow用户

发布于 2016-04-21 13:16:00

你可以使用类似这样的东西。根据需要更改if条件。

代码语言:javascript
复制
var count = parseInt(window.innerWidth/20);
function animateElement(){
    if(count){
        $('.spawn_worm').animate({
            left: '+=20px',
            top: '-=20px'
        }, 100, animateElement);
        count--;
    }
}
animateElement();
票数 1
EN

Stack Overflow用户

发布于 2016-04-21 14:05:32

您可以使用像这样的循环

代码语言:javascript
复制
jQuery(function($) {
  $('button').click(function() {
    var top = 20,
      sign = -1;

    anim();

    function anim() {
      $('.spawn_worm').animate({
        left: '+=20px',
        top: (sign == 1 ? '+' : '-') + '=' + top + 'px'
      }, 100, "linear", function() {
        if (sign * top < 16) {
          top += 2 * sign;
          if (!top) {
            sign = 1;
          }
          anim();
        }
      });
    }
  });
});
代码语言:javascript
复制
body {
  height: 1000px;
  margin-top: 200px;
}
.ct {
  position: relative;
  height: 500px;
}
.spawn_worm {
  height: 50px;
  position: absolute;
  background-color: red;
  display: inline-block;
  width: 50px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Test</button>
<div class="ct">
  <div class="spawn_worm"></div>
</div>

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

https://stackoverflow.com/questions/36760016

复制
相关文章

相似问题

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