Hello everyone,
I'm trying to create a set of animations with GSAP (for example scale from 0, fade and scale from 0, etc.) and I wanted to make that by applying the name of the animation in a data-gsap attribute of an html element, this is applied on scroll.
I can't, with any configuration to make each animated element be at its original position when scrolling up (onLeave, onEnterBack, onLeaveBack for say), so I would like the animation to happen only once when scrolling down, I hope that I explained clearly.
What I did:
I create a function called "fromScale" where:
1. I take the array of all elements that have data-gsap="fromScale"
2. I create a forEach in which I take all possible data-gsap-* attributes that may have been assigned to the element in the markup (e.g. data-gsap-start, data-gsap-end, data-gsap-ease) and in case have not been indicated, I assign them some default values.
3. I create a variable containing the gsap-from with the animation
4. I create a standalone ScrollTrigger which in the parameters resumes the gsap-from animation just created
5. I call the "fromScale" function
(in my various attemps also with timeline...see the commented part in the CodePen)
In the example here, I have a 100vh high gray section with a simple paragraph in the centre, then follows a background image with a box positioned with a translate -40% at its top, then I have another 100vh high gray section with another paragraph.
Things I need to fix:
1. My main problem is that I am unable, neither with the timelines nor with other methods such as forEach to make the toggle Actions or the reverse or the onEnter, onLeave, onEnterBack, onLeaveBack methods work, with pin or without pin , it just doesn't work, the animations always start.?
2. I also can't figure out how to set start and end values that can fit, dynamically, for each element regardless of its position. I just can't get it (that's why I created the data-gsap attributes). Another huge problem when you want to create animations to be called in an entire project.
I've been trying different approaches for days and this means that I probably haven't grasped well some basic concept about scrolltrigger,? or there's a problem in my logic (or syntax?) even if I watched the video lessons I have at my disposal and the docs and I don't understand (also because the more I search the more I realize that the same thing can be done in a thousand ways..so then I get lost...)
I hope someone can help me. Thank you all in advance.♥️