Jump to content
Search Community

Next.js, GSAP Animations, & mapping over dynamic data

jnemec test
Moderator Tag

Recommended Posts

Hello, I'm trying to place an animation (slide from right) on each column in a component (3 columns that animate individually) using Next.js. I'm mapping over dynamic data from a CMS, and I have gotten it to where it works how I would like using gsap.timeline(), except this component is more towards the middle of my site, so it's running when the page first loads. I can't get ScrollTrigger to work with my array of refs. Once I add scrolltrigger, it makes the animation happen on the columns at the same time. How can I make it so that these animations won't happen until scrolled to? I apologize for the limited amount of code. This is for a site at my job and I have been tasked with learning GSAP. 


const tl = gsap.timeline()
const columnRefs = useRef([])
columnRefs.current = []
useLayoutEffect(() => {
const ctx = gsap.context(() => {
columnRefs.current.forEach((e) => {
{ opacity: 0, x: 200 },
duration: 2,
opacity: 1,
x: 0,
ease: 'none',
stagger: 0.2,
return () => ctx.revert()
}, [])
const addToRefs = (e) => {
if (e && !columnRefs.current.includes(e)) {
return (
<section id={sectionID.current} className={styles.section}>
<div className={styles.columns}>
{columns?.map((column) => (
onMouseOver={() => handleHover(column._key)}
onFocus={() => handleHover(column._key)}
onMouseOut={() => handleHover([])}
onBlur={() => handleHover([])}
<div className={styles.content}>
<h2 className={styles.label}>{column?.label}</h2>
styles.body +
`${current === column._key ? ` ${styles.show}` : ''}`
styles.gradient +
`${current === column._key ? ` ${styles.show}` : ''}`
Link to comment
Share on other sites

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 CodeSandbox that demonstrates the issue? 


Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). 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 is a Next starter template that you can fork.


I noticed you're creating a timeline OUTSIDE a gsap.context() - that's almost surely a bad idea. In React, it's very important to do proper cleanup. I'd recommend popping that inside your useLayoutEffect() gsap.context()


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

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