Jump to content
Search Community

NextJS ScrollTrigger going to ending when starting

PWSey test
Moderator Tag

Recommended Posts

Hi 
 

I was following the example below but when trying to implement on a nextjs project the animation on start is disappearing (going to the end of the animation) also it seems to be duplication i have no clue why, if anybody can enlighten me I would greatly appreciate it. When going from bottom to top animation works correctly.

Code sandbox link: https://codesandbox.io/p/sandbox/cool-gwen-gqvoqi
 

Example I followed:

See the Pen LYpaPQp by hexagoncircle (@hexagoncircle) on CodePen

Link to comment
Share on other sites

Hey, 

 

your codesandbox is unfortunatly read only :/ 

Therefore I can't test it in your setup. But it looks like you are missing some css classes. 
Search in the internet for a scss to css compiler (I usually use this one) and copy the result into your project.

 

I would also encourage you to read Greensock's articles regarding React. There are many helpful tips, which help enormously. 

 

If the problem persists, try using gsap.context(), which is explained in detail in the linked article.  

const comp = useRef(); // create a ref for the root level element (for scoping)

useEffect(() => {
    let ctx = gsap.context(() => {
      //your animation
    }, comp); // <- IMPORTANT! Scopes selector text

    return () => ctx.revert(); // cleanup
}, []);

 

Edited by alig01
Added additional option
  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hi @PWSey and welcome to the GreenSock forums!

 

On top of @alig01 great advice about using GSAP Context 🙌 there is another thing in your setup. You are pinning an element with a display flex set to it. That can spell trouble if you're not carefull. Is better to use and pin a regular element to wrap your elements with display flex.

 

From the ScrollTrigger Docs:

 

pinSpacing

Boolean | String - By default, padding will be added to the bottom (or right for horizontal: true) to push other elements down so that when the pinned element gets unpinned, the following content catches up perfectly. Otherwise, things may scroll UNDER the pinned element. You can tell ScrollTrigger not to add any padding by setting pinSpacing: false. If you'd rather it use margin instead of padding, you can set pinSpacing: "margin". Note: pinSpacing works in most cases, but it really depends on the way you set up your DOM and CSS. For example, if you pin something in a parent that has display: flex or position: absolute, the extra padding won't push other elements down/right so you may need to manually space things out. pinSpacing is just a convenience that works in most situations. Important: if the container is display: flex, pinSpacing is set to false by default because that's typically what is desired since padding works differently in that context.

 

This vide by @Carl should explain that better:

 

I think if you use context and wrap your hero in a non-flex element your setup should work as expected.

 

Happy Tweening!

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