有没有办法循环播放分步动画?

huangapple go评论71阅读模式
英文:

Is there a way to loop a stepped animation?

问题

我已经为标志设计创建了一个分步动画,但似乎无法使其循环。我尝试在最后一步中重新调用该函数,使用setInterval,但我对JQuery还不是100%确定语法。有人有什么建议吗?

这是我现有的代码:

function move(step1, step2, step3, step4, step5) {
    $('.pupil').animate({
        left: '+=' + step1,
    }, 800, function() {
        $(this).animate({
            top: '+=' + step2,
        }, 800, function() {
            $(this).animate({
                left: '+=' + step3,
            }, 800, function() {
                $(this).animate({
                    top: '+=' + step4,
                }, 800, function() {
                    $(this).animate({
                        left: '+=' + step5,
                    }, 800);
                });
            });
        });
    });
}

$(function() {
    move(2, 2, -4, -2, 2);
});

这是代码的链接,以查看HTML和CSS:
https://codepen.io/kaitruss/pen/mdzxeZV

当我尝试循环动画时,大多数情况下它只会停止工作。我尝试添加第六步,其中调用了函数以重新启动它,但它完全停止工作。使用setInterval并调用函数既没有破坏它,也没有使其循环。

英文:

I've created a stepped animation for a logo design, but I can't seem to get it to loop. I've tried recalling the function in the final step, using setInterval, but I'm pretty new to JQuery so I'm not 100% sure on the syntax. Does anyone have any suggestions?

Here is my existing code:

function move(step1, step2, step3, step4, step5) {
        $('.pupil').animate({
            left: '+=' + step1,
        }, 800, function() {
            $(this).animate({
                top: '+=' + step2,
           }, 800, function() {
                $(this).animate({
                    left: '+=' + step3,
                }, 800, function(){
                 $(this).animate({
                      top: '+=' + step4,
                   }, 800, function() {
                    $(this).animate({
                     left: '+=' + step5,
                      }, 800,);
                 
                })
            });
        });   
    });
    }
    $(function() {
       move(2, 2, -4, -2, 2);
    });

And a link to the codepen to see the html and css:
https://codepen.io/kaitruss/pen/mdzxeZV

When I tried to loop the animation, most of the time it would just stop working. I attempted to add a sixth step where the function was called in order to restart it, but it just stopped working altogether. Using setInterval and calling the function didn't break it, but it also didn't loop.

答案1

得分: 0

我已清理了一下javascript代码,现在它可以运行了。以下是代码pen链接:

https://codepen.io/Crist-bal-D-az-lvarez/pen/BaqVqrr

只编辑了javascript:

function move($element, movements, time) {
  if (movements.length > 0) {
    $element.animate(movements[0], time, function() {
      movements.shift();
      move($element, movements, time);
    });
  }
}

function animateEye() {
    move($('.pupil'), [
       { left: '+=2' },
       { top: '+=2' },
       { left: '+=-4' },
       { top: '+=-2' },
       { left: '+=2' }
    ], 800);
}

var interval = setInterval(animateEye, 800 * 5 + 2000); // wait 2 seconds after the five animation cycles

animateEye();

// clearInterval(interval); // stop the infinite loop animation
英文:

I have cleaned a little the javascript code and now it is working, here is the code pen:

https://codepen.io/Crist-bal-D-az-lvarez/pen/BaqVqrr

Only edited the javascript

function move($element, movements, time) {
  if (movements.length > 0) {
    $element.animate(movements[0], time, function() {
      movements.shift();
      move($element, movements, time);
    });
  }
}

function animateEye() {
    move($('.pupil'), [
       { left: '+=2' },
       { top: '+=2' },
       { left: '+=-4' }, 
       { top: '+=-2' }, 
       { left: '+=2' }
    ], 800);
}

var interval = setInterval(animateEye, 800 * 5 + 2000); // wait 2 seconds after the five animation cycles

animateEye();

// clearInterval(interval); // stop the infinite loop animation

Here is snippet version

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function move($element, movements, time) {
  if (movements.length &gt; 0) {
    $element.animate(movements[0], time, function() {
      movements.shift();
      move($element, movements, time);
    });
  }
}

function animateEye() {
    move($(&#39;.pupil&#39;), [
       { left: &#39;+=2&#39; },
       { top: &#39;+=2&#39; },
       { left: &#39;+=-4&#39; }, 
       { top: &#39;+=-2&#39; }, 
       { left: &#39;+=2&#39; }
    ], 800);
}

var interval = setInterval(animateEye, 800 * 5 + 2000); // wait 2 seconds after the five animation cycles

animateEye();

// clearInterval(interval); // stop the infinite loop animation

<!-- language: lang-css -->

.text{
  color:#222345;
  font-size:100px;
  font-family: &#39;Cedarville Cursive&#39;, cursive;
}
.logo{
  position:relative;
  height: 150px;
  width:100px;
  overflow:hidden; 
}
.K{
 position:absolute;
 z-index:5;
 top: 0px;
 left: 0px;
}
.R{
 position:absolute;
 top: 25px;
 left: 24px;
}
.iris{
  position:absolute;
  width:22px;
  height:20px;
  background-color:#0DAB76;
  border-radius:30px;
  top:76px;
  left:4px;
}
.pupil{
  position:absolute;
  width:4px;
  height:12px;
  background-color:#272727;
  border-radius:15px;
  top:81px;
  left:13.5px;
}
.lid{
  position:absolute;
  width:15px;
  height:1px;
  top:75px;
  left:5px;
  border-radius:15px;
  z-index:6;
  transform:rotate(-7deg);  
}

<!-- language: lang-html -->

&lt;head&gt;&lt;link href=&quot;https://fonts.googleapis.com/css2?family=Cedarville+Cursive&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;logo&quot;&gt;
   &lt;div class=&quot;text K&quot;&gt; K &lt;/div&gt;
   &lt;div class=&quot;text R&quot;&gt; R &lt;/div&gt;
   &lt;div class=&quot;lid&quot;&gt;&lt;/div&gt;
   &lt;div class=&quot;iris&quot;&gt;&lt;/div&gt;
   &lt;div class=&quot;pupil&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 12:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定