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

100
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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