Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!