Dagmawi Posted March 20 Share Posted March 20 In this Minimal Reproducible Example in which gsap ScrollTrigger is used, the trigger element gets an unintended translateY style property when its start hits scroller-start causing the trigger element to kind of jump downwards. What may be the issue? App.js and App.css body { margin: 0; } .div1, .div2 { height: 100vh; } .div1 { background-color: pink; } .square { width: 150px; height: 20vh; background-color: fuchsia; } import { useEffect } from 'react' import './App.css' import gsap from 'gsap'; import { ScrollTrigger } from 'gsap/all'; function App() { useEffect(() => { gsap.registerPlugin(ScrollTrigger); gsap.to(".square", { x: 1000, duration: 8, scrollTrigger: { trigger: ".square", start: "top 80%", end: "top 30%", scrub: 4, toggleActions: "restart none none none", pin: true, markers: true, } }) }, []); return ( <> <div className="div1"></div> <div className="div2"> <div className="square"></div> </div> </> ) } export default App Link to comment Share on other sites More sharing options...
Solution GSAP Helper Posted March 20 Solution Share Posted March 20 Hi there! I see you're using React - 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/ 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...
Dagmawi Posted March 21 Author Share Posted March 21 Thanks for your thorough response. I got it fixed now. 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