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

102
Vistas
How to make opacity animation when text replacement

i have this code that replace index in array every 5s to display different text

    const [articles, setArticles] = useState(0)

    const [imageBanner, setImageBanner] = useState([
    {
        text: "blah blah"
    },
    {
        text: "blah blah"
    },
    {}
])

   setInterval(() => {
        if (articles < 3) {
            setArticles(articles => articles + 1)
        }
        console.log(articles)
    }, 5000);

and i would like to know how can i make opacity animation when text replacement

what i tried before

@keyframes example {
0% {
    opacity: 0;
}
3% {
    opacity: 0.2;
}
6% {
    opacity: 0.4;
}
8% {
    opacity: 0.6;
}
10% {
    opacity: 0.8;
}
50% {
    opacity: 1;
}
90% {
    opacity: 0.8;
}
93% {
    opacity: 0.6;
}
96% {
    opacity: 0.4;
}
98% {
    opacity: 0.2;
}
100% {
    opacity: 0;
}

}

But the timing of it is not good

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of using a setInterval, use the timing of the animation to change the text of the element.

By using a useRef hook we can create a reference to our animation element. Then listen to animationiteration event on that element. This event will trigger every time it starts again.

For this to work we'll need to modify the CSS animation and tell it either to go N times or infinite times.

When the event is called, use setArticleIndex to count to the next item (or to 0 whenever the end of the list is reached).

const { useRef, useState, useEffect } = React;

const imageBanners = [
  { text: "Hello There" },
  { text: "General Kenobi" },
  { text: "You are a bold one" }
];

const Component = () => {
  const animationEl = useRef(null);
  const [articleIndex, setArticleIndex] = useState(0);

  useEffect(() => {
    animationEl.current.addEventListener('animationiteration', () => {
      setArticleIndex(currentIndex => {
        if (currentIndex + 1 < imageBanners.length) {
          return currentIndex + 1;
        } else {
          return 0;
        }
      });
    });
  }, []);
  
  return (
     <div className="fade-in-out" ref={animationEl}>
       {imageBanners[articleIndex].text}
     </div>
  );
};

const app = document.querySelector('#app');
ReactDOM.render(
  <Component />, 
  app
);
@keyframes fade-in-out {
  0% {
    opacity: 0;
  }

  5%,
  95% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.fade-in-out {
  animation: fade-in-out 5s both infinite;
}
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>

<div id="app"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

This code should work by using animation and animation-fill-mode.

@keyframes move {
  from {
    opacity: 0;
  }
  3% {
    opacity: 0.2;
  }
  6% {
    opacity: 0.4;
  }
  8% {
    opacity: 0.6;
  }
  10% {
    opacity: 0.8;
  }
  50% {
    opacity: 1;
  }
  90% {
    opacity: 0.8;
  }
  93% {
    opacity: 0.6;
  }
  96% {
    opacity: 0.4;
  }
  98% {
    opacity: 0.2;
  }
  to {
    opacity: 0;
  }
}

h1 {
  animation-name: move;
  animation-delay: 0s;
  animation-duration: 5s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}
<h1>Hello, World</h1>

about 4 years ago · Juan Pablo Isaza Denunciar
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