Jump to content
Search Community

ScrollTo plugin not working every time

Daniel Hult 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

Hi!
 

I use the ScrollTo plugin in a forEach loop with different sections. You can see the live site here (click on the plus icon); https://human-as.appex.dev/kontakt
Not sure why, but the scrollTo plugin only works sometimes. If you hard refresh the site and try, it will at one point not work.

I know this is not much to go by, but do you know why this could happen?

Link to comment
Share on other sites

28 minutes ago, Daniel Hult said:

Not sure why, but the scrollTo plugin only works sometimes. If you hard refresh the site and try, it will at one point not work.

I know this is not much to go by, but do you know why this could happen?

If you don't scroll the page at all (in the CodePen or the live site), it works as you intend. If the user scrolls before clicking the +, it won't scroll to it.

 

This is because you create the scrollTo tween in the timeline at the page's initialization. ScrollTo plugin has autoKill: true by default. So when the user scrolls, that tween is ignored. See the ScrollTo docs for more information.

 

You can either change autoKill to false or you can use a separate function to create the scroll tween when the button is clicked. I think the second approach is probably better in your case. 

 

You might not want to add the function to your timeline because of the timeline.progress(1).progress(0) line that you have. Instead, I'd probably use a .delayedCall() inside of the click listener.

 

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

  • Like 2
Link to comment
Share on other sites

When I don't have it part of the timeline though it doesn't work as smoothly as intended. I want the scroll to be a part of the timeline so that after they've been scrolled down I do some other animation in the end as well. 

To be honest Im not sure why I have timeline.progress(1).progress(0). I got help from one of you earlier today that suggested that I put it in my timeline so it starts correctly(?) 

Sorry to bother you with all these questions, but Im learning a lot from these forums so that's a big plus :D 

 

Link to comment
Share on other sites

3 minutes ago, Daniel Hult said:

When I don't have it part of the timeline though it doesn't work as smoothly as intended. I want the scroll to be a part of the timeline so that after they've been scrolled down I do some other animation in the end as well. 

 

You can change the timing of the delayed call to match where you want it in the timeline (your easiest option). Or you can add it to your timeline and then use some conditional logic inside of the function to prevent it from running the first time when you do timeline.progress(1).progress(0). Or you can set it up to work without the timeline.progress(1).progress(0) line. 

 

1 minute ago, Daniel Hult said:

To be honest Im not sure why I have timeline.progress(1).progress(0). I got help from one of you earlier today that suggested that I put it in my timeline so it starts correctly(?) 

 

Yes, it's a kind of hack to set things up correctly. You could set them manually but I'll leave that exercise to 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...