Docop Posted September 24 Share Posted September 24 First of all, I'm not familiar with using CodePen and since there are multiple components involved, I hope you understand that I'll be sharing a CodeSandbox link instead. I'm making an simple animation in React project with GSAP and Locomotive. but I'm experiencing a problem where the start and end markers are not fixed and move along with the scroll. I think I've done GSAP and Locomotive settings well. The strange thing is that when I scroll in the regular mode of codesandbox, the marker is fixed and works well. But when I click the 'toggle responsive preview' button in codesandbox and refresh, then try scrolling, the marker doesn't stay fixed and moves with the scroll. And in a real local environment outside of codesandbox, the marker also doesn't stay fixed and exhibits the same behavior, moving with the scroll. May I know what's the problem?? CodeSandBox Link: https://codesandbox.io/s/locomotive-gsap-xsq7lf?file=/src/App.js Locomotive setting Code: useEffect(() => { const locoScroll = new LocomotiveScroll({ el: document.querySelector(".container"), smooth: true }); locoScroll.on("scroll", ScrollTrigger.update); ScrollTrigger.scrollerProxy(".container", { scrollTop(value) { return arguments.length ? locoScroll.scrollTo(value, 0, 0) : locoScroll.scroll.instance.scroll.y; }, getBoundingClientRect() { return { top: 0, left: 0, width: window.innerWidth, height: window.innerHeight }; }, pinType: document.querySelector(".container").style.transform ? "transform" : "fixed" }); ScrollTrigger.addEventListener("refresh", () => locoScroll.update()); ScrollTrigger.refresh(); }, []); GSAP Code: useEffect(() => { let tl = gsap.timeline({ scrollTrigger: { trigger: oneRef.current, start: "75% center", end: "105% center", scrub: true, markers: true } }); tl.to(twoRef.current, { x: 300, duration: 5 }); }, []); Link to comment Share on other sites More sharing options...
GSAP Helper Posted September 24 Share Posted September 24 Hi there! I see you're using React - Proper animation 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. In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once. Here's the structure: // 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! }, []); This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background. We strongly recommend reading the React information we've put together at https://greensock.com/react Happy tweening! Link to comment Share on other sites More sharing options...
nguyenhoangvubt Posted November 8 Share Posted November 8 I have the same problem exactly, I tried to use gsap.context() too, but nothing change, the problem still there. Link to comment Share on other sites More sharing options...
GSAP Helper Posted November 8 Share Posted November 8 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 Stackblitz that demonstrates 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 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 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 the gsap-trial NPM package for using any of the bonus plugins: React (please read this article!) Next Svelte Sveltekit Vue Nuxt 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now