How to detect when an image has finished rendering in the browser (i.e. painted)?

后端 未结 5 1983
不思量自难忘°
不思量自难忘° 2020-12-08 04:19

On a web app I need to work with a lot of high-res images, which are dynamically added to the DOM tree. They are pre-loaded, then added to the page.

It\'s one thing

5条回答
  •  不思量自难忘°
    2020-12-08 04:58

    I used requestAnimationFrame to do the trick. After image loaded it will be rendered during the next animation frame. So if you wait two animation frame your image will be rendered.

    function rendered() {
        //Render complete
        alert("image rendered");
    }
    
    function startRender() {
        //Rendering start
        requestAnimationFrame(rendered);
    }
    
    function loaded()  {
        requestAnimationFrame(startRender);
    }
    

    See http://jsfiddle.net/4fg3K/1/

提交回复
热议问题