Jump to content
Search Community

Scrolltrigger issues - Scrolltrigger + pin + snap not working as expected.

Akram Faiz

Recommended Posts

Posted

I need to create a react component that has scrolltrigger with pin which pins the media that comes behind it. The component will have multiple sections which have h1, p, span and a media background that has height of 150vh which displays different media (image or video) based on the index of array and its media type the component when comes into viewport the media div should have position fixed (using pin). The sections are not snapping properly. I am getting jitterring, bouncing effect and sometimes to for bouncing with a pause.

 


      const nextSection = sectionRefs.current[idx + 1];
      const prevSection = sectionRefs.current[idx - 1];

      if (nextSection) {
        triggers.push(
          ScrollTrigger.create({
            trigger: section,
            start: 'bottom bottom-=50px',
            end: 'bottom top',
            onEnter: (self) => {
              if (self.direction === 1 && !isSnapping) {
                isSnapping = true;
                gsap.to(window, {
                  scrollTo: {
                    y: nextSection,
                    autoKill: false,
                  },
                  duration: 0.6,
                  ease: 'power2.inOut',
                  onComplete: () => {
                    isSnapping = false;
                  },
                  onInterrupt: () => {
                    isSnapping = false;
                  },
                });
              }
            },
          })
        );
      }

      if (prevSection) {
        triggers.push(
          ScrollTrigger.create({
            trigger: section,
            start: 'top-=50 top',
            end: 'top top',
            // markers: true,
            onLeaveBack: (self) => {
              if (self.direction === -1 && !isSnapping) {
                isSnapping = true;
                gsap.to(window, {
                  scrollTo: {
                    y: prevSection,
                    autoKill: false,
                  },
                  duration: 0.6,
                  ease: 'power2.inOut',
                  onComplete: () => {
                    isSnapping = false;
                  },
                  onInterrupt: () => {
                    isSnapping = false;
                  },
                });
              }
            },
          })
        );
      }


its not working as expected.
I need smooth transition between section with a snap effect.
Please guide me.
 

GSAP Helper
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

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

 

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

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import GSAP as shown in the Install Helper in our Learning Center : 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

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

Posted

Here in the codepin, 

I want the sections to snap when I am scrolling and they should snap only when I reach top 5% and bottom 95% remaing area it should have normal scroll.
when I am in the section the elements in the content should animate from y: 50 to y: 0 with stagger effect of 0:3. once I scroll further and reach 95% it should snap.

See the Pen xbwgdBR by Akram-Faiz-the-styleful (@Akram-Faiz-the-styleful) on CodePen.


 

Posted

Hi,

 

In order to do that you need to use the custom snap config option as shown in the docs:

https://gsap.com/docs/v3/Plugins/ScrollTrigger/#snap

 

This demo shows a simple implementation of this:

See the Pen empgQyB by GreenSock (@GreenSock) on CodePen.

 

Basically you have to use the value in order to either trigger a function or return a function like in the demo. You need to find the values you need in order to run some conditional logic to accommodate what you're looking for.

 

Hopefully this helps

Happy Tweening!

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