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

222
Visualizações
How to make an animation in javascript/react js?

I am trying to change the opacity of an element after every 100 milliseconds after a click event. But it is not working. The function change is called after a click event. Here is my code:

function change (i) {
  var o:number = 0;
  setTimeout( function() {
    o = o + 0.1;
    document.querySelector("div.label").style.opacity = o;
    console.log("o = " + o);
  }, 100)
}

When I console log, the value of o is always 0.1. What is wrong with this code?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to update the var o with the current opacity before increasing it:

function change (i) {
  var o:number = 0;
  setTimeout( function() {
    o = document.querySelector("div.label").style.opacity; // <-- ADD THIS
    o = o + 0.1;
    document.querySelector("div.label").style.opacity = o;
    console.log("o = " + o);
  }, 100)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You're using setTimeout(), but I think you're looking for setInterval() method (if you want something to repeat itself).

Try :

function change (i) {
  var o:number = 0;
  setInterval( function() {
    o = o + 0.1;
    document.querySelector("div.label").style.opacity = o;
    console.log("o = " + o);
  }, 100)
}

BE CAREFUL : you have to prevent your variable o to go beyond 1 (maximum opacity). You can use clearInterval().

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