Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
How to fetch and animate multiple dom element using useRef in React GSAP

I am trying to reproduce an effect in GSAP animation which is written in html to react.

Here is the link

https://codepen.io/GreenSock/pen/QEdpLe

The problem is that in normal javascript unlike react. GSAP can access the dom element by given classname.

gsap.to(".box", {...})

box can be multiple items.

But in react to get the data I need to use useRef hook. So I am giving a reference to the parent component and animating the children elements.

    let boxAnimate = (selectedEl) => {
    gsap.to(selectedEl.current.children, {
      duration: 10,
      ease: "none",
      x: "-=600", //move each box 500px to right
      modifiers: {
        x: gsap.utils.unitize((x) => parseFloat(x) % 600), //force x value to be between 0 and 500 using modulus
      },
      repeat: -1,
    });
  };

    gsap.set(inifiteRef.current.children, {
      backgroundColor: (i) => colors[i % colors.length],
      x: (i) => i * 50,
    });
    boxAnimate(inifiteRef);
  }, []);

But its is not giving me the desire effect.

My desire effect. When the number disappear into the finish line it should appear back from the start.

What happening - All number disappear and all appear again.

           <div
              style={{
                color: "red",
                fontSize: "40px",
                height: "50px",
                width: "600px",
                overflow: "hidden",
                display: "flex",
                flexDirection: "row",
                justifyContent: "space-between",
              }}
              ref={inifiteRef}
            >
              <div style={{ width: "50px" }}>1</div>
              <div style={{ width: "50px" }}>2</div>
              <div style={{ width: "50px" }}>3</div>
              <div style={{ width: "50px" }}>4</div>
              <div style={{ width: "50px" }}>5</div>
              <div style={{ width: "50px" }}>6</div>
              <div style={{ width: "50px" }}>7</div>
            </div>

Is there anything wrong with my code?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!