Fade in images after they have loaded

后端 未结 6 1216
半阙折子戏
半阙折子戏 2020-12-13 00:58

I found this code to achieve the desired effect and tried it on JSFiddle

http://jsfiddle.net/AndyMP/7F9tY/366/

It seems to work in that it fades the image in

6条回答
  •  既然无缘
    2020-12-13 01:40

    Pure java script and CSS solution:

    Using the transitions effect with opactiy.

    const images = document.getElementsByTagName("img");
    for (let image of images) {
      image.addEventListener("load", fadeImg);
      image.style.opacity = "0";
    }
    
    function fadeImg () {
      this.style.transition = "opacity 2s";
      this.style.opacity = "1";
    }
    
    
    

提交回复
热议问题