Jump to content
Search Community

GSAP scroll trigger not working

khoaoaoao test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

  • Solution

That's because you set the start and end to exactly the same thing and you've got it playing onEnter and reversing onLeave, thus it immediately plays and then reverses at exactly the same time, so it doesn't go anywhere. I assume you wanted to set the end to something later, like when the bottom of the element hits the top of the viewport(?)

 

Are you trying to do this?: 

See the Pen QWJoNBM?editors=0010 by GreenSock (@GreenSock) on CodePen

 

If so, there's really no need for you to create that loop at all since you're animating all those elements at the same time, so you can just use a single tween: 

See the Pen poQYyZp?editors=0010 by GreenSock (@GreenSock) on CodePen

 

I hope that helps. 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Hi,

 

There are several errors in your useEffectLayout and GSAP Context:

  useLayoutEffect(() => {
    let ctx = gsap.context(
      gsap.from(".Services__func", {
        scrollTrigger: {
          trigger: ".Services__funcContainer",
          toggleActions: "play reverse restart reverse",
          start: "top center",
          end: "bottom center",
          markers: true
        },
        opacity: 0,
        yPercent: 10,
        duration: 0.5,
        stagger: 0.02
      }),
      scrollSelector
    );
    return ctx.revert();
  }, []);

The gsap.context() method takes a callback or function as first parameter, right now you are just passing a gsap.to() instance, that is not going to work. Also the cleanup phase of the useEffect and useLayoutEffect hooks also takes a callback, right now you are immediately invoking ctx.revert() a few milliseconds after creating the GSAP Context instance.

 

GSAP Context:

// Bad
const ctx = gsap.context(gsap.to(...), scope);

// Good
const ctx = gsap.context(() => {
  gsap.to(...);
}, scope);

 

React useEfefct/useLayoutEffect

// Bad
useLayoutEffect(() => {
  const ctx = gsap.context(() => {}, scope);
  return ctx.revert();
}, []);

// Good
useLayoutEffect(() => {
  const ctx = gsap.context(() => {}, scope);
  return () => ctx.revert();
}, []);

This seems to work in the way you intend:

useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    gsap.from(".Services__func", {
      yPercent: 10,
      opacity: 0,
      duration: 0.5,
      stagger: 0.05,
      scrollTrigger: {
        trigger: scrollSelector.current,
        start: "top center",
        end: "bottom center",
        toggleActions: "play reverse restart reverse",
        markers: true,
      },
    });
  }, scrollSelector);
  return () => ctx.revert();
}, []);

Here is a fork of your sandbox:

https://stackblitz.com/edit/vitejs-vite-gnrf5o

 

Finally I strongly recommend you to check the resources in this page:

 

Hopefully this helps.

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