jQuery animations sequence

若如初见. 提交于 2019-11-28 02:26:42

Ok here is the thing. I am going to start with a quick intro but will try to lead you to as many reading links as possible. And I will try to keep it all short.

GSAP is a fantastic suite of tools that originally started back in the Flash glory days, and has since been brought into JavaScript world. There are 4 main products: TweenLite, TimelineLite, TimelineMax and TweenMax plus a few more.

I usually load up a cdnjs link to TweenMax in my projects because TweenMax contains all of the above.

There are many ways to do what you are trying to do in TweenMax, here is one of them:

var divs = document.querySelectorAll('div');
var duration = .4;
var timeline = new TimelineMax({ paused: true });
timeline.to(divs[0], duration, { y: 200, ease: Power4.easeOut });
timeline.addCallback(doSomething, duration, [], this);
timeline.to(divs[1], duration, { rotation: 180, ease: Power4.easeOut });
timeline.addCallback(doSomethingElse, duration * 2, [], this);
timeline.to(divs[2], duration, { opacity: 0, ease: Power4.easeOut });
timeline.addCallback(danceForMe, duration * 3, [], this);
timeline.to(divs[3], duration, { scale: 2, transformOrigin: '50% 0%', ease: Power4.easeOut });
timeline.addCallback(moveIt, duration * 4, [], this);
timeline.play();

function doSomething() { console.log('doSomething'); }
function doSomethingElse() { console.log('doSomethingElse'); }
function danceForMe() { console.log('danceForMe'); }
function moveIt() { console.log('moveIt'); }
html, body { margin: 0; padding: 0; }
div { float: left; width: 100px; height: 100px; }
div:nth-child(odd) { background-color: #cc0; }
div:nth-child(even) { background-color: #0cc; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.17.0/TweenMax.min.js"></script>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>

Above, I am using two out of the plethora of methods available i.e. the .to() and .addCallback() methods.

There is so much more you can do with this tool. You will be amazed. I encourage you to explore.

Further reading:

  • Getting Started with GSAP: Link.
  • Jump Start: GSAP JS: Link.
  • Video: Sequence JavaScript Animations Like a Pro with GSAP's TimelineLite: Link.
  • Timeline Tip: Understanding the Position Parameter: Link.
  • Simple GreenSock Tutorial - Your first steps with GSAP: Link.
  • GreenSock Cheat Sheet: Link.
  • Codepen demos: Link.

Hope you find this information useful.

So suggestion, you pass the array of animations as argument to your first animation and you pass it on.

function animationX(var anim){
   ...
   ...

   if(anim && anim.length > 0)
       anim.shift()(anim);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!