alexb148 Posted September 7, 2020 Share Posted September 7, 2020 I'm building a site using lots of ScrollTrigger animations. There are some places where all I want to do is simply use a trigger to fire off a function when the user gets to a point on the page. Is this possible? I almost always use timelines (to keep everything consistent, and so that I can easily kill them if I need) so my instinct is to do something like this: var tl = gsap.timeline({ scrollTrigger: { trigger: '#myTrigger', start: 'top 80%', toggleActions: 'play null play null' } }); tl.add(function () { console.log('do something'); }); But this doesn't do anything. I've noticed that if I add something else to the timeline, e.g. tl.fromTo( '#myElement', 0.1, { opacity: 0 }, { opacity: 1 } ); Then my function does get called, but only once - it doesn't seem to respect the toggleActions. Is there a simply way to just call a function using a ScrollTrigger? Thanks. Link to comment Share on other sites More sharing options...
ZachSaucier Posted September 7, 2020 Share Posted September 7, 2020 Hey alexb148. ScrollTriggers callbacks are perfect for this sort of thing. For example: ScrollTrigger.create({ trigger: elem, onEnter: myEnterFunc, onLeave: myLeaveFunc, onEnterBack: myEnterFunc, onLeaveBack: myLeaveFunc }); 7 Link to comment Share on other sites More sharing options...
alexb148 Posted September 7, 2020 Author Share Posted September 7, 2020 Thank you, I hadn't spotted those options in the docs - yes this does the job. Thanks for the rapid reply! Link to comment Share on other sites More sharing options...
TBWA-NY Posted February 15, 2022 Share Posted February 15, 2022 Hello! Is there a way to pass an argument to a ScrollTrigger callback? Nevermind.. I see you have to wrap it in another function. onEnter: function(){ logRed("onEnter"); }, or onEnter: () => logRed("onEnter"), Thanks, Travis 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now