Jump to content
Search Community

ScrollTrigger not working properly - Next.JS

Anirban_45 test
Moderator Tag

Recommended Posts

So i am trying to animate the cards at the end of the Page by ScrollTrigger. This page also contains a Initial loading animation of the Page getting revealed. My problem is that if i keep the Initial Page load animation then the ScrollTrigger dosen't work properly (As you can see in the demo) and thecards suddenly appears as soon as the end reaches without a proper animation. But if i remove the initial page load animation then the scrollTrigger animation works fine. Any solution why ? 

 

Demo link - Demo here

Link to comment
Share on other sites

I'm going to guess this is a combination of a few 'React' things. From the looks of things, when the page first loads, the cards in in the view port, which could cause issues with ScrollTriggers calculations.
 

Also:

 

Perhaps the problem is that React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: 

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

 

One of the React team members chimed in here if you'd like more background.

 

We strongly recommend reading the React article at

 

Happy tweening!

Link to comment
Share on other sites

24 minutes ago, GSAP Helper said:

I'm going to guess this is a combination of a few 'React' things. From the looks of things, when the page first loads, the cards in in the view port, which could cause issues with ScrollTriggers calculations.
 

Also:

 

Perhaps the problem is that React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: 

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

 

One of the React team members chimed in here if you'd like more background.

 

We strongly recommend reading the React article at

 

Happy tweening!

 

Thanks. but it's not working, i even tried to use a bool as state for when the animation get's finished (initial page load animation) and then use it to create the timeline for the cards but still it's not working and the issue keeps happening. Also in the real project where i am using this, it has React 17

Link to comment
Share on other sites

Hi,

 

I'm seeing the exact same behaviour on both cases with and without the wrapper component.

https://i.imgur.com/SJVeCo3.mp4

 

A few things I can point out though:

  • You can definitely run into issues if you start poking around with the body's overflow property.
  • In your wrapper component you are not using GSAP Context. I'd strongly recommend that you do that in order to get the best results.

What I could suggest it to use an onComplete callback in the wrapper timeline and call ScrollTrigger's refresh method there in order to update the ScrollTrigger instances on the page:

useEffect(() => {
  const ctx = gsap.context(() => {
    gsap.set('body', { overflow: 'hidden' });
    gsap.set(`.outer`, { yPercent: 100 });
    gsap.set('.inner', { yPercent: -100 });

    const tl = gsap.timeline({
      defaults: { duration: 3, ease: 'power3.inOut', overflow: 'hidden' },
      onComplete: () => {
        ScrollTrigger.refresh();
      },
    });

    tl.fromTo(
      ['.outer', '.inner'],
      {
        yPercent: (index) => {
          return index ? -100 : 100;
        },
      },
      { yPercent: 0 }
    ).set(['body', '.outer', '.inner'], {
      overflow: 'auto',
      height: 'auto',
    });
  });
  return () => ctx.revert();
};

Hopefully this helps.

Happy Tweening!

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