Gsap timeline position parameters

 Gsap timeline position parameters



In the following timeline code: 


let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:1})
    .to('.box2',{x:400, duration:1})
    .to('.box3',{x:400, duration:1})


normally when animation of box1 completed in 1s then box2 starts and after 1second of box2 animation of box3 starts

if the duration of box1 was 5 second then animation of box1 will take 5s to complete and then only animation of box2 starts. For more enhancement the position parameter was introduced

Some of the examples of position parameter are:

1.

let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:1})
    .to('.box2',{x:400, duration:1})
    .to('.box3',{x:400, duration:1},1)


On that box 3 there is "1" outside the {} which means that  start exactly at a time of 1
It doesn't care about other box animation, after 1s of starting animation from the beginning it will starts to move exactly at 1s.

Output of code:




2.

let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:1})
    .to('.box2',{x:400, duration:1},"+=2")
    .to('.box3',{x:400, duration:1})


"+=2" will start 2 second after previous tween ends.
That means there will be 2 second gap after animation of box1 ends

Output of code:



3.

let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:5})
    .to('.box2',{x:400, duration:1},"-=1")
    .to('.box3',{x:400, duration:1})


"-=1" will start 1 second before previous tween ends
That means when animation of box1 reach to 4 second the box2 will get started
if there was -=2 then 2 second before box1's animation ends....

Output of code:



4.

let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:5})
    .to('.box2',{x:400, duration:1})
    .to('.box3',{x:400, duration:1}, "<")


"<" start when previous tween begins
That means when box2 starts box3 also starts. 
But when duration of box2 is about 4 second then, box3 will reach to destination at first then box2 because they will start exactly same time but box2 will move slowly because of duration: 4

Output of code:



5.

let mytimeline = gsap.timeline()
mytimeline
    .to('.box1',{x:400, duration:5})
    .to('.box2',{x:400, duration:4})
    .to('.box3',{x:400, duration:1}, "<2")


"<2" start 2 second after previous tween begins
That means when box2 starts box3 will start after 2 second of starting of box2

Output of code:






HTML and CSS codes for the above program:

 
<div class="boxes">
  <div class="box box1"></div>
  <div class="box box2"></div>
  <div class="box box3"></div>
</div>




*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
    display: flex;
    align-items: center;
    height: 100vh;
    background-color: rgb(7, 7, 22);
}

.box{
    margin-left: 2rem;
    height: 50px;
    width: 50px;
    margin-bottom: 1rem;
}

.box1{
    background-color: red;
}
.box2{
    background-color: greenyellow;
}
.box3{
    background-color: aqua;
}



For more info check : Gsap position parameters






































Comments

Popular posts from this blog

CSS simple grid layout

GSAP Timelines