0

I want to make my div box fade in but it never appears.

The first time I tried to use a loop but that made an infinite loop and crashed because I put a timeout in it.

Now I'm trying to use a function that adds 0.1 opacity to the object every set amount of time until it is at max 1 but nothing happens-

function fadethis1 (fadeObjectIn) {debugger
   if (fadeObjectIn.style.opacity>1){
  setTimeout(() => {
    fadeObjectIn.style.opacity=fadeObjectIn.style.opacity+0.1
    fadeObjectIn.style.opacity=parseFloat(fadeObjectIn.style.opacity)+0.1
  }, 100);
    
  fadethis1(document.querySelector("div.alertbox")) }
}
Christian
  • 4,902
  • 4
  • 24
  • 42

1 Answers1

-1

const fadeInDiv = () => {
    const fade = document.getElementById("fade");
    let fadeIndex = 0;
    
    const fadeInterval = setInterval(() => {
      fadeIndex += 0.1;
      fade.style.opacity = fadeIndex;
      if (fadeIndex >= .9) clearInterval(fadeInterval);
    }, 100);
}

fadeInDiv();
#fade {
    width: 250px;
    height: 100px;
    background-color: green;
    opacity: 0;
}
<div id="fade"></div>
sanodzeg
  • 44
  • 1
  • 3