Jump to content
Search Community

Restarting Timeline with ScrollTrigger

hamsa@thoughtput.in test
Moderator Tag

Recommended Posts

Hi folks!

 

Been using ScrollTrigger since last week, and amazed at the power and flexibility it yields! Working with it super fun! 😄

 

I've run into a particular issue while trying to implement a conditional show/hide of two objects within the same timeline. As demo'ed in the CodePen, I have two bars that grows together while scrolling, with button toggles to show/hide only one of them at a time. Ideally, when I toggle, I want to restart the timeline so that the visible bar animates to the height per the current scroll position. I've tried to use the Timeline Restart function, which even though does restart the animation correctly, seems to go ahead and kill the ScrollTrigger (or rather override it partially); it does not listen to the scroll position anymore and just goes ahead and completes the animation on its own. I tried including ScrollTrigger.refresh() but that doesn't change anything either. Is there a workaround to this, or am I doing something wrong?

 

Thanks in advance!

See the Pen OJMYjep by ashwinuae (@ashwinuae) on CodePen

Link to comment
Share on other sites

Hey Hamsa and welcome to the GreenSock forums. Thanks for supporting GreenSock by being a Club GreenSock member!

 

The issue here is logical: When a user clicks the button you're telling the timeline to both play all the way through (with the .restart()) and be controlled by the scroll position. Those two things are not compatible. 

 

As such, you'll have to use some method to disable the ScrollTrigger while the animation is replaying and also animate the progress of the timeline to the progress of the timeline when the button is clicked. I'd set it up along the lines of this:

See the Pen VweOyEw?editors=0010 by GreenSock (@GreenSock) on CodePen

  • Like 2
  • Thanks 1
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...