Jump to content
Search Community

Recommended Posts

Posted

Hi all,

 

I must be getting scrollTrigger's behaviour wrong, but I can't figure out what I am missing since I believe to stick closely to the documentation.

 

The content of my webpage features four sections tagged by the class .sectionContainer, each with a height equal to exactly 1 viewport. Once the user finishes scrolling, I want the page to snap to the closest position that shows one full .sectionContainer. In other words, I don't want the page to rest in between two sections.

However, when I scroll through the attached example, GSAP often snaps to positions that lie in between two sections, even though I believe my snap points are calculated correctly. Also the onSnapComplete callback shows that the scroll progress at the snap point is different from the actual snap point.

F.e. totalHeight of 3578px, sectionHeight of 832px and the 50px padding at the top of .contentContainer imply a first snap point of 0.0139742873113471 by the mapping function sections.map((_, i) => offSet * (i + 1) + i * sectionCoverage). Similarly, for the #secondSectionContainer, the snap point is 0.26048071548351, which seems conceptually correct to me, but the code's behaviour disagrees and snaps to a point within #firstSectionContainer.

Can someone please point me to what I am missing?

See the Pen JojEdwK by Oliver-K-the-bashful (@Oliver-K-the-bashful) on CodePen.

Posted

Hi @okost welcome to the forum! 

 

Personally I always like to test my theory by hard coding values that I know are correct to then convert that to some fancy function that does all the calculations for me. 

 

I've now used a more simplified calculation to get you mostly there, but your math seems a lot more complicated, then I find it is even more important to first get the value you want and then try converting that to a function and compare that function to the values you expect.

 

When giving the snap a single value it will snap to increments of that value, so 0 is always part of that setup. If you want to be more explicit you can use an array and define the points you want to snap to. (seems like you already got the hang of that, my bad!) 

 

Your setup is even more complicated, because there is a spacer at the top of your section, so the span logic should always be +1 spacer and the first snap point should only be the distance of that spacer. Now that I'm trying to visualize that in my had I feel the urge to get out pen and paper and try and conceptualize that frist before creating fancy functions, because this can get really complex fast! I think to make it simper you might even want to remove the space from the first and last slide, so that 0 and 1 are already in position for your snap points, then you have to calculate how many sections you have and how many spacers you have and calculate what progress value is for each of those and then fro each loop combine the number of sections it must skip and how many spacers that includes.

 

As always I which I'd payed more attention in math class! Hope it helps and happy tweening! 

 

See the Pen MYWJaaW?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen.

Posted

Hi @mvaneijgen,

 

Apologies for my late reply - I had hoped to figure out a solution before responding to help other users.

However, I ultimately had to conclude that the plugin wasn't made for what I intended and changed the layout to full-screen sections.

That allowed me to use the snapTo parameter more accurately.

In case others come across this topic in the future, GSAP's scrollTrigger is really made to trigger animations, not to direct users to certain objects on your website. Even though this might be a nice idea for future plugins. To my uneducated ear, it sounds fairly straightforward to have the scroll bar align the top of a DOM element with the top of the screen upon being triggered by a scroll point.

 

 

Many thanks for your response either way.

 

 

Best,

Oliver

 

Posted
On 3/7/2025 at 4:15 AM, okost said:

In case others come across this topic in the future, GSAP's scrollTrigger is really made to trigger animations, not to direct users to certain objects on your website.

That doesn't sound correct to me - ScrollTrigger can certainly fire off a scrollTo tween to wherever you want. It also has built-in snapping capabilities. The demo you provided doesn't load ScrollToPlugin and it doesn't even fire your onStart because you misconfigured it. I'm struggling to understand exactly what you're attempting to do, but I bet this helper function would be useful: 

 

https://gsap.com/docs/v3/HelperFunctions/helpers/getScrollLookup

 

If you need some help, please just clarify (in as simple terms possible) what the issue is and how we can reproduce it in the provided demo. 

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