Jump to content
Search Community

ScrollTrigger .getVelocity returning highly variable results when driven by a scrollTo animation

Tblax test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi everyone,

 

I'm struggling to obtain consistent and predictable values from a ScrollTrigger instance .getVelocity() method, in order to compute the remaining time to any given element(s) within the scrolling parent element.
This scrollTrigger instance is driven by a scrollTotween,  as I need the scrolling to be programatically driven within the app I'm building.
For this reason and quite a few others, I would strongly prefer to keep a scrolling approach rather than using transforms, as scrolling serves a multitude of purposes throughout the rest of the app.

As you can see in the demo below, even though the scrollTo tween speed is constant with no easing whatsoever,  I'm getting highly "instable" values from the getVelocity instance method within the onUpdate callback (the delta between two values can amount to pretty crazy numbers!)
 

My guess is all of this is related to the browsers inherent native scrolling behavior but that's just, well, a guess.


How would you go about solving or working around this issue? All solutions are welcome and thank you in advance!

 

 

See the Pen VwNGvzm by Lorbakounet (@Lorbakounet) on CodePen

Link to comment
Share on other sites

  • Solution

Yeah, that's the nature of dynamically calculating velocity because you've always gotta choose how much "recording" to do time-wise and then compare the values. But I'm not sure why you're not just pre-calculating the velocity based on the linear tween's distance and duration: 

const velocity = ScrollTrigger.maxScroll(document.querySelector("#scroller")) / 10; // change / duration

This demo shows a general idea of how the values are calculated, although in ScrollTrigger the measurements are always at least 50ms apart:

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

 

In short, I think there's probably a much cleaner way to do what you're attempting without using getVelocity().

Link to comment
Share on other sites

Thanks @GreenSock It helps a lot.
As for the reason I do not precalculate, I guess it's because I rely on getVelocity() in other parts of my code where the sampling is good enough for those other purposes and I didn't even think about using something else, that's my obsessive tendencies I guess 🙂

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