Tween Control on pressing buttons

Tween Control on pressing buttons



Tween’s have a number of methods for controlling playback.

In order to control a tween you need have way to reference it. Below we set up a variable to reference our tween.


let mytween = gsap.to(".box1", {
    y:200,  
    paused:"true",
    duration:6

})

document.querySelector(".startBtn").onclick = function(){
    mytween.play();
}

document.querySelector(".pauseBtn").onclick = function(){
    mytween.pause();
}
document.querySelector(".restartBtn").onclick = function(){
    mytween.restart();
}
document.querySelector(".reverseBtn").onclick = function(){
    mytween.reverse();
}

we have created our animating object along with 4 buttons with html, and on clicking each button, the tween will behave. To do this we must set variable for our tween.


We have set;  Pause = "True" at beginning because if we dont keep then our tween will start moving from the beginning but at first, we want to keep at rest and move only when we click other buttons

I mean to prevent a tween from playing automatically you can set its paused special property to true.


HTML code

<div class="main">
  <div class="box1"></div>
  <div class="box1"></div>
  <div class="box1"></div>
  <div class="box1"></div>
  <button class="startBtn">Start</button>
  <button class="pauseBtn">Pause</button>
  <button class="reverseBtn">Reverse</button>
  <button class="restartBtn">Restart</button>
</div>


Comments

Popular posts from this blog

CSS simple grid layout

Gsap timeline position parameters

GSAP Timelines