Increase opacity of an element

5

My fadeOut is ok, but fadeIn is not working ...

function fadeIn(elem, speed)
    {

    if(!elem.style.opacity)
    {
        elem.style.opacity = 0;
    }
    InInterval(function(){

 elem.style.opacity += 0.03;

    }, speed /100);
}
    
asked by anonymous 28.05.2016 / 15:13

1 answer

4

The problem is that the opacity value is interpreted as a string (text), not a number. You can like :

function fadeIn(elem, speed) {
   if(!elem.style.opacity) {
      elem.style.opacity = 0;
   }
   timer = setInterval(function(){
      if(parseFloat(elem.style.opacity) >= 1) clearInterval(timer);
      elem.style.opacity = parseFloat(elem.style.opacity) + 0.03;
   }, speed / 100);
}

ele = document.getElementById('hey');
fadeIn(ele, 5000);
#hey {
  background-color: red;
  width: 100px;
  height: 100px;
}
<div id="hey">

</div>
    
28.05.2016 / 15:40