How to make fadeOut effect with pure JavaScript

后端 未结 5 549
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 01:59

I\'m trying to make fadeOut effect for a div with pure JavaScript.

This is what I\'m currently using:

//Imagin         


        
5条回答
  •  自闭症患者
    2020-11-29 02:24

    Just this morning I found this piece of code at http://vanilla-js.com, it's very simple, compact and fast:

    var s = document.getElementById('thing').style;
    s.opacity = 1;
    (function fade(){(s.opacity-=.1)<0?s.display="none":setTimeout(fade,40)})();
    

    You can change the speed of the fade changing the second parameter in the setTimeOut function.

    var s = document.getElementById('thing').style;
    s.opacity = 1;
    (function fade(){(s.opacity-=.1)<0?s.display="none":setTimeout(fade,40)})();
    #thing {
      background: red;
      line-height: 40px;
    }
    I will fade...

提交回复
热议问题