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

101
Visualizações
Javascript Appear-Disappear Animation dont work

I want to make h1 disappear and after 3 sec appear again. It disappears but dont appear again. Or do i need inline style for it? And any other useful loop for this except if?

let head1 = document.querySelector(".asd")
let head1style = getComputedStyle(head1);
let head1disp = head1style.display;

let changedisp = function() {
  if (head1disp === "block") {
    head1.style.display = "none";
  } else if (head1disp === "none") {
    head1.style.display = "block"
  } else {
    console.log("Something Wrong!")
  }
};

setInterval(changedisp, 3000);
h1 {
  display: block;
}
<body>
  <h1 class="asd">Look at me!</h1>
  <script src="app.js"></script>
</body>

</html>

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

0

You don't need JavaScript for that in a modern browser. CSS animations with keyframes are fully capable of delivering the same effect:

<style>
  @keyframes fade-out-in {
    0% {
      opacity: 1;
    }
    25% {
      opacity: 0;
    }
    75% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }

  .box {
    animation: fade-out-in 5000ms;

    /* wait time at the beginning */
    animation-delay: 2000ms;
  }
</style>

<div class="box">
  Hello World
</div>

You will probably need to adjust the timing. Learn more about CSS keyframe animations in this fantastic article - https://www.joshwcomeau.com/animation/keyframe-animations/

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use setTimeout to do it once.

const h1 = document.querySelector('h1');
h1.style.display = 'none';
setTimeout(() => h1.style.display = 'block', 3000);

Or you can use setInterval to do it every 3 seconds.

const h1 = document.querySelector('h1');
function switchDisplay() {
    if (h1.style.display === 'block') h1.style.display = 'none';
    else h1.style.display = 'block';
}
setInterval(switchDisplay, 3000);
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