Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda