csp Posted March 5 Share Posted March 5 GSAP is exactly the animation library I've been looking for, but for the life of my I can't figure out how to incorporate it into my project. Just to practice and play around I created a completely new Next project that is using styled-components and no css. I'm not sure if I'm setting something up wrong or what, but every time I try and sort of animation, as soon as the viewport hits the "start" marker everything disappears. Here is my current code which is very simple ( I am copying exactly from the scrub: true demo in the GSAP docs). Am I possibly importing incorrectly? 'use client'; import styled from 'styled-components'; import { gsap } from 'gsap/dist/gsap'; import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'; import { useGSAP } from '@gsap/react'; gsap.registerPlugin(ScrollTrigger); const Body = styled.div` height: 300vh; `; const Trigger = styled.div` position: absolute; top: 75vh; left: 0; width: 100vw; height: 80vh; background-color: lightgray; padding: 20px; `; const Box = styled.div` background-color: black; width: 10vw; height: 10vw; `; export default function Home() { useGSAP(() => { const tl = gsap.timeline({ scrollTrigger: { trigger: '.trigger', start: 'top top', end: '+=1000', scrub: 1, pin: true, markers: true, }, }); tl.to('.box', { yPercent: 350, duration: 1 }); tl.to('.box', { rotation: 360, duration: 3 }); tl.to('.box', { xPercent: 350, duration: 1 }); }, []); return ( <Body> <h1>Scroll down</h1> <Trigger class="trigger"> <Box class="box"></Box> </Trigger> </Body> ); } Link to comment Share on other sites More sharing options...
YgorLuiz Posted March 5 Share Posted March 5 You forgot to add the scope selector, like this const container = useRef(); useGSAP(() => { // gsap code here... }, { scope: container }); // <-- scope is for selector text (optional) Link to comment Share on other sites More sharing options...
csp Posted March 5 Author Share Posted March 5 Thanks, that seemed to get me a little further. If I make changes and save them, I can see everything working properly for a few seconds until the page refreshes. Once the page refreshes all of the animations and animation elements are gone and all i can see are the markers and the "Scroll down" text. 'use client'; import styled from 'styled-components'; import { useRef } from 'react'; import { gsap } from 'gsap/dist/gsap'; import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'; import { useGSAP } from '@gsap/react'; gsap.registerPlugin(useGSAP, ScrollTrigger); const Body = styled.div` height: 300vh; `; const Trigger = styled.div` position: absolute; top: 75vh; left: 0; width: 100vw; height: 80vh; background-color: lightgray; padding: 20px; `; const Box = styled.div` background-color: black; width: 10vw; height: 10vw; `; export default function Home() { const container = useRef(); useGSAP( () => { const tl = gsap.timeline({ scrollTrigger: { trigger: '.trigger', start: 'top top', end: '+=1000', scrub: 1, pin: true, markers: true, }, }); tl.to('.box', { yPercent: 350, duration: 1 }); tl.to('.box', { rotation: 360, duration: 3 }); tl.to('.box', { xPercent: 350, duration: 1 }); }, { scope: container } ); return ( <Body ref={container}> <h1>Scroll down</h1> <Trigger class="trigger"> <Box class="box"></Box> </Trigger> </Body> ); } Link to comment Share on other sites More sharing options...
GSAP Helper Posted March 5 Share Posted March 5 Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates 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 dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer. See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo: 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...
csp Posted March 5 Author Share Posted March 5 This example from the docs is exactly what I'm trying to recreate in my code shared above. When I try to refactor this to work with Next.js and styled-components I start to get issues. Here is the codepen from the docs I am trying to recreate. Sorry I'm not too familiar with codepen and can't provide a more specific example. See the Pen yLegBwO by GreenSock (@GreenSock) on CodePen Link to comment Share on other sites More sharing options...
Solution GreenSock Posted March 5 Solution Share Posted March 5 Yeah, without a minimal demo we can't do much to help, but I also noticed that you're using class="" instead of className="" in your JSX. I'm pretty sure React/JSX requires className instead of class. Is this basically what you're trying to do?: https://stackblitz.com/edit/stackblitz-starters-npxpxa?file=app%2Fpage.tsx Seems to work fine, no? Link to comment Share on other sites More sharing options...
csp Posted March 5 Author Share Posted March 5 Yep you're right, it was the class error! It must be one of those days. Thanks Greensock! Now off to apply to my project, really appreciate the help Link to comment Share on other sites More sharing options...
GreenSock Posted March 5 Share Posted March 5 No problem. Glad you got it figured out. Good luck! 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