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

93
Views
How to add animation?

How will I add fadeout animation in this javascript code?

document.getElementById("mt-alerts").style.display="block";
setTimeout(function(){
    document.getElementById("mt-alerts").style.display="none";
}, 2000);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should make classses for every animations, and the JS should change only the classes of the items. You can with this method, make your own animatsions.

const alertMsg = document.getElementById("mt-alerts");
const hide = document.getElementById("hide");
const red = document.getElementById("red");

hide.addEventListener("click", ()=> alertMsg.classList.toggle("hidden"));
red.addEventListener("click", ()=> alertMsg.classList.toggle("red"));
#mt-alerts {
  opacity: 1;
  transition: all 0.5s linear;
  color: black;
}

.hidden {
  opacity: 0!important;
}

.red {
  color: red!important;
}
<div id="mt-alerts">My Alert</div>
<button id="hide">Hide/show</button>
<button id="red">Make it red</button>

about 4 years ago · Juan Pablo Isaza Report

0

Instead of using display, you need to start with opacity for this.

The idea is simply decrease the opacity of your element until it reaches the 0, then set its display as none. To fade in you can repeat the idea in reverse.

function js_fadeOut(targetID, intervalMs, fadeWeight) {

    var fadeTarget = document.getElementById(targetID);
    
    var fadeEffect = setInterval(function () {
        if (!fadeTarget.style.opacity) {
            fadeTarget.style.opacity = 1;
        }
        if (fadeTarget.style.opacity > 0) {
            fadeTarget.style.opacity -= fadeWeight;
        } else {
            fadeTarget.style.display = "none";
            clearInterval(fadeEffect);
        }
    }, intervalMs*fadeWeight);
}
<div id='target' style='padding:8px; background:lightblue;' onclick='js_fadeOut("target", 200, 0.1)'>Click to fadeOut</div>

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!