Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda