Jump to content
Search Community

GSAP TweenMax - not able to access variables inside timelines

ellisj98 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

I am creating scroll based animations with GSAP. I am creating a timeline for a tween, then updating the progress of the timeline based on how far you have scrolled through a set container. I am able to create this effect by using CSS values such as x or y for example for CSS transform properties. I cannot access dynamic variables inside an each function for the elements I wish to animate and set the CSS properties to tween through these.

My code would be too long to copy and paste here - so will link code pens for my problem.

Here is my working version:

Using the x and y values to tween CSS transform properties works completely fine. This is the result I want at the end.

https://codepen.io/ellisclicky/pen/rqPYmw

Here is my version that I want to work, where I am using data attributes from an HTML element to determine what CSS property to animate:

I cannot access the variable and I can't figure out why - I have googled everything under the sun about this but can't find anything.

https://codepen.io/ellisclicky/pen/NOowMx

Any help is very appreciated.

  • Like 1
Link to comment
Share on other sites

Hi @ellisj98 :)

 

Welcome to the forum.

 

You have a missing hyphen in your data attribute for animation.

 

<div class="animation section__image scroll-animation" data animation="x" data-animation-start="600" data-animation-end="0" data-animation-duration="0.1"> 

 

I'm not entirely sure what you're trying to do, but if you just want to tween the progress of the timeline based on scroll, you could do it like this.

 

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

Hopefully that helps a bit. Happy tweening.

:)

 

  • Like 1
Link to comment
Share on other sites

Hi both,

 

Thanks for this. I have changed it to data-animation (was a mistake) but it still doesn't work. I am basically animating on scroll elements relative to their container/if they are in the viewport.

 

Regarding the data attribute being a string, this is what I thought too. However, I'm not sure how to fix this to use this as a variable in my tween for x in this instance. Can you elaborate further on the adding a "+" as a prefix, please?

 

Thank you

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