anzorAs Posted January 10 Share Posted January 10 Hello All! I am facing a weird issue that I haven't seen anyone else encounter. Or maybe I was just searching for the wrong terms. \ Basically I have a Next.js instance where I am trying to pin a box to a scroll area... pretty straight forward. However, once the pinning is initiated, the box jumps down to a position where it should end up once the scroll is over. Im gonna attach a recording to explain the issue. Any help would be appreciated. here is my code: "use client"; import { useEffect, useRef } from "react"; import gsap from "gsap"; import ScrollTrigger from "gsap/dist/ScrollTrigger"; gsap.registerPlugin(ScrollTrigger); export default function Home() { const pinContainer = useRef(null); useEffect(() => { initScrollTrigger(); }, []); const initScrollTrigger = () => { gsap.to(".block", { scrollTrigger: { trigger: ".block", start: "top center", end: "top center-=300", pin: true, scrub: true, markers: true, }, x: 400, }); }; return ( <main> <div className="content" style={{ paddingRight: 100 }}> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> <div className="block h-64 bg-black" ref={pinContainer} style={{ height: 200, background: "red", display: "inline-block" }}> test test test{" "} </div> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> <h1>Content Content Content Content Content Content Content Content Content Content Content Content Content Content </h1> </div> </main> ); } Screen Recording 2024-01-10 at 20.37.001.mp4 Link to comment Share on other sites More sharing options...
Solution GSAP Helper Posted January 10 Solution Share Posted January 10 Hi @anzorAs and welcome to the GSAP Forums! 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. 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/ If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. Link to comment Share on other sites More sharing options...
anzorAs Posted January 10 Author Share Posted January 10 Thank you very much @GSAP Helper! I cant believe I missed it. Easy solution, love it 1 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