Jump to content
GreenSock

PointC

data attribute to Timeline variable name - need advice from Javascript wizards

Moderator Tag

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 everyone,

 

I can usually get things working fine, but quite often I seem to over complicate some things or take the wrong approach so I'm looking for some advice.

 

I've got 4 buttons that will each have their own independent SVG animation on hover. (For the purposes of the Codepen, it's just a simple box inside each button.) 

 

The four timelines are set up as variables. I'm using a data attribute (that matches the timeline variable name) on each button to choose the timeline to play. 

 

Everything works fine, but I'm just looking for advice on the best way to do this.

 

To convert the data-attribute to the variable name and allow it to play the timeline, I've found two ways

1: eval(varName) 2: window[varName]

Both methods seem to accomplish the task, but I've read a bit that says eval() is bad and poses problems, while other info says it's fine and poses no security problems if no data is coming from the user.

 

So.. which is better... or is there a completely different approach to this that I'm missing? 

 

Thanks.

 

 

See the Pen zGyNYJ by PointC (@PointC) on CodePen

Link to comment
Share on other sites

Eval is ok if it's your data, but it's slower and not commonly used. If you can access it with square brackets, go with that method. I would only use eval as a last resort.

Link to comment
Share on other sites

I think referencing the window like that is odd. If I had a collection of animations, I would add them to object or an array. Objects are nice because you can reference them by a name.

 

See the Pen dowNRP?editors=101 by osublake (@osublake) on CodePen

  • Like 3
Link to comment
Share on other sites

Hey Diaco and Blake  :-) ,

 

Thanks so much for the info and Codepens - I greatly appreciate it and both of those are so much better than what I wrote.

 

Uggggh - I have so much to learn.

 

Thanks again.  

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.
×