Jump to content
Search Community

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

PointC test
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

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