Jump to content
Search Community

Linking to a specific spot with ScrollTrigger

Samalander test
Moderator Tag

Recommended Posts

15 minutes ago, GreenSock said:

Does that help?

Not really sure how I can make this work in React (Next.js). Would I be able to do this inside of an onClick function? Not really sure how or why the code that you wrote works, but when I try, it returns this error:

image.png.a025515bc0222be7327904c6d1edd384.png

 

Here's some snippets of code:

image.png.e49f8599ae29a7e58a4859ba56893779.png

image.png.3b590f9466750ad74bdbabfbace148f0.png

 

And here's the full code: https://replit.com/@Samalander/Samalander?embed=true#pages/index.jsx

 

Link to comment
Share on other sites

16 hours ago, GreenSock said:

That error indicates that your timeline didn't have a ScrollTrigger applied to it at that point. Your code is only adding one if "normal" is truthy, so perhaps that was evaluating as false when your useEffect() ran. 

So how could I fix this? I believe that the useEffect() function runs when React renders, and all of the GSAP stuff is inside of there because it only works after everything is rendered. This means that when I have an onclick function an element that needs to do GSAP stuff, it won't work because all of the elements get rendered before the GSAP stuff is run, so it will always generate an error.

Link to comment
Share on other sites

I'm not familiar with Replit and it doesn't look at all convenient to make edits - it's always best to use something like CodeSandbox or CodePen that makes editing easy. Here's a React starter template: https://codesandbox.io/s/gsap-react-starter-ut42t

 

If I understand your question correctly, I think you can just useRef() for your timeline, sorta like: 

 

const timeline = useRef();

useEffect(() => {
  timeline.current = gsap.timeline();
  ...
}, []);
...

<a onClick={() => timeline.current && gsap.to(window, {scrollTo: timeline.current.scrollTrigger.labelToScroll("label")})}>

 

This boils down to logic in your code and timing issues, it's not a GSAP thing. Just make sure that if you're going to try to tap into a timeline's scrollTrigger property, that you've set one up.

 

Does that help? 

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