Jump to content
Search Community

Seamless loop direction change on scroll is not working infinitely

codechirag test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I was  looking for marquee on scroll direction change example  and  thanks to this wonderful  community I got it,  I  saw  many threads for similar thing and one   of them worked for me but I have little difference in my  example, I have used  helper function to make infinite  and also  for two  opposite  direction simultaneously.

 

In my snippet the directions  are working  fine but it stops after sometimes and also at some points I found initially both were  going in one direction. I request corrections from experts. Thanks a lot.

See the Pen jOJRywp by EmpChirag (@EmpChirag) on CodePen

Link to comment
Share on other sites

thanks alot @mvaneijgen, your solution is correct but when I use it in my react project it is jerking  when I  add repeat: -1 and also  when I just make a codeblitz  snippet only for this section it worked fine. please look at this  loom video , I will be very much thankful to you if you can correct me.

 

here  I am  sharing my project fork in stackblitz. you can find that effect at the very bottom of  the page.

 

Link to comment
Share on other sites

  • Solution

Hi,

 

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

Here are a couple of demos of the horizontal loop with the useGSAP hook:

https://stackblitz.com/edit/vitejs-vite-cljwjs?file=src%2FApp.jsx

 

This changes the loop direction based on the scroll direction:

https://stackblitz.com/edit/vitejs-vite-auctqy?file=src%2FApp.jsx

 

Hopefully this helps.

Happy Tweening!

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