Jump to content
Search Community

How to reuse tweens?

hgl test
Moderator Tag

Go to solution Solved by OSUblake,

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

I'm experimenting with GSAP, and I have a few questions regarding the pen I created:

 

  1. How do I reuse tweens? In the demo, if I alternatively click toggle and yoyo (and clicking yoyo after the page is firstly loaded doesn't seem to work), the animation breaks easily.
  2. The two tweens I created actually cancel each other. Is it possible to define one as a reversed version of the other? Note that they have to modify a class.

 

Thanks in advance.

See the Pen eNvRJg by anon (@anon) on CodePen

Link to comment
Share on other sites

You could use 1 tween for everything by changing its reversed and repeat properties. I'm not sure how you wanted the yoyo to work, like does it always add or remove the class, and does it always go to the top? Here's how I did it. Notice that I positioned some stuff absolutely so I didn't have to deal with the changing of block elements.

 

See the Pen waJrQN by osublake (@osublake) on CodePen

  • Like 3
Link to comment
Share on other sites

OSUblake, thanks for the help.

 

Unfortunately, in my case, the before and after elements can't be absolutely positioned.

 

Inspired by your code, I give it another try, and this time I simply wrap tweens in functions, and return a new one whenever I need an animation, seems to be working. Not sure if I'm wasting memories?

 

See the Pen gpmzBm by anon (@anon) on CodePen

Link to comment
Share on other sites

  • Solution

What you did is fine. The simplest solution is usually the best.

 

Does creating a new tween every time use up some memory? Sure. Are you going notice this? Probably not unless you are creating hundreds of tweens/second to do something crazy like a particle effect.

 

With the way your code is setup now, you have basically given each tween/timeline its own responsibility, which means you can reuse them.

 

See the Pen dovKWv by osublake (@osublake) on CodePen

  • 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.
×
×
  • Create New...