Jump to content
Search Community

Multiple scroll triggers getting created

i_dont_understand_gsap test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi,

 

I am trying to add scrolltrigger to the follow Id (which i made sure occurs only once in the code), but the are multiple start and stop triggers getting created.

 

I am not sure what is that i am doing wrong here

 

useLayoutEffect(() => {
    gsap.registerPlugin(ScrollTrigger);
    const tl = gsap.timeline({
      scrollTrigger: {
        trigger: '#custom-page-footer',
        start: 'top center',
        end: '+=100',
        scrub: true,
        markers: true,
        toggleActions: 'play reverse play reverse',
      }
    })
    tl.to('.nav-cta-join-the-fast-lane', {
      opacity: 0,
      duration: 0.1
    })
    tl.to('.nav-gushwork-logo', {
      opacity: 0,
      duration: 0.1
    })
  })

 

Screenshot 2023-06-01 at 7.41.10 PM.png

 

Later on the page...

 

Screenshot2023-06-01at8_40_30PM.thumb.png.32d710247cdf0bd9162f261b6776f863.png

See the Pen endpage.js,pages by edit (@edit) on CodePen

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

  • Solution

Hi @i_dont_understand_gsap and welcome to the GreenSock forums!

 

I see that you're not using GSAP Context in your code and that you're not doing proper cleanup in your effect hook. Since React 18 is super important to cleanup in your effect hook as explained here:

Here you can learn more about GSAP Context:

https://greensock.com/docs/v3/GSAP/gsap.context()

 

Finally take a look at the resources in this page to get a better grasp about using GSAP in your React projects:

 

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

1 hour ago, GSAP Helper said:

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

 

Hi,

 

GSAP ScrollTrigger NextJS Starter (forked) - StackBlitz

 

If you see this stackblitz, the trigger markers are way beyond the "EndPage" component. 

 

I know this is not the exact problem, but it is part of the problem.

Link to comment
Share on other sites

11 minutes ago, i_dont_understand_gsap said:

 

Hi,

 

GSAP ScrollTrigger NextJS Starter (forked) - StackBlitz

 

If you see this stackblitz, the trigger markers are way beyond the "EndPage" component. 

 

I know this is not the exact problem, but it is part of the problem.

Would it be possible to move that to codepen?  That stackblitz wants to run some cookies or soemthing and is getting blocked for me

  • Like 1
Link to comment
Share on other sites

Hi,

 

There are a few issues in your example:

  1. In React child components are rendered before their parents, so your endpage component gets rendered before that.
  2. In your endpage component you have this:
    const tl = gsap.timeline({
      scrollTrigger: {
        trigger: '#custom-page-footer',
        start: 'top center',
        end: '+=100',
        scrub: true,
        markers: true,
        toggleActions: 'play reverse play reverse',
      },
    });
    tl.to('.nav-cta-join-the-fast-lane', {
      opacity: 0,
      duration: 0.1,
    });
    tl.to('.nav-gushwork-logo', {
      opacity: 0,
      duration: 0.1,
    });

    But none of those three selectors are present in the HTML so GSAP is not doing anything. Because of that the start and end markers are set to the top of the viewport. Without those elements those positions will not be fixed.

In order to solve the rendering order issue, use refreshPriority in your ScrollTrigger configs. From the ScrollTrigger docs:

number - it's VERY unlikely that you'd need to define a refreshPriority as long as you create your ScrollTriggers in the order they'd happen on the page (top-to-bottom or left-to-right)...which we strongly recommend doing. Otherwise, use refreshPriority to influence the order in which ScrollTriggers get refreshed to ensure that the pinning distance gets added to the start/end values of subsequent ScrollTriggers further down the page (that's why order matters). See the sort() method for details. A ScrollTrigger with refreshPriority: 1 will get refreshed earlier than one with refreshPriority: 0 (the default). You're welcome to use negative numbers too, and you can assign the same number to multiple ScrollTriggers.

 

So the ScrollTrigger instance in your endpage component should have a higher number than the one in the index component.

 

Hopefully this helps.

Happy Tweening!

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