Jump to content
GreenSock

icraft-websites

Check if tween var has finished

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hey forums,

 

How do you check if a particular tween variable has played once through and has ended so you could either call tween.play or tween.reverse based on its current position?

Link to comment
Share on other sites

Hi,

 

Well the easiest way is using an onComplete and/or onReverseComplete callback in the instance.

 

Another option is check if the current time is equal to the instance's duration or check the instance's progress, 0 means at the beginning of the instance and 1 means that the instance has finished.

 

As you can see there are a few options and as usual a reduced codepen sample could really help us narrow down which option could be the best.

 

Rodrigo.

  • Like 1
Link to comment
Share on other sites

Yup, Rodrigo is right again. I'd probably go the progress() route.

 

tween = TweenLite.to("#redBox", 1, {x:550})


$("#continue").on("click", function() {
  console.log(tween.progress())
  if(tween.progress() === 1){
    tween.reverse();
  }
  
  if(tween.progress() === 0){
    tween.play();
  } 
})

http://codepen.io/GreenSock/pen/ropHG

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×