Is there something like a ready event when an image in html is loaded and rendered?

前端 未结 3 1002
孤独总比滥情好
孤独总比滥情好 2020-12-17 01:12

I am creating a webview app for iPad, and want to show a splashscreen until index file and a set of images is loaded and rendered.

I want this splashscreen to disap

相关标签:
3条回答
  • 2020-12-17 01:31

    There are two event listeners DOMContentLoaded and load. The load event fires when all files have finished loading from all resources, including ads and images. The images are rendered during download, so this should be what you are looking for.

    See the demo here

    0 讨论(0)
  • 2020-12-17 01:36

    I'll write it out for you:

    loadedImages = 0;
    timesChecked = 0;
    checkInterval = 500;
    maxLoadTime = 10000; // in miliseconds
    window.onload = function() {
      for (var i = 0; i < document.getElementsByName('img').length; i++) {
        document.getElementsByName('img')[i].onload = function() {
          loadedImages++;
        }
      }
      intervalID = setInterval("checkSplash()", checkInterval);
    }
    
    function checkSplash() {
      timesChecked++;
      if (loadedImages >= document.getElementsByName('img').length || timesChecked * checkInterval >= maxLoadTime) {
        clearInterval(intervalID);
        // hide splash screen
      }
    }
    

    Enjoy :)

    It's pretty simple actually: <img src=".." onload="alert('image 1 is loaded!')" />

    0 讨论(0)
  • 2020-12-17 01:54

    You can add .load() to each image and then simply check when all images are loaded by declaring a variable.

    0 讨论(0)
提交回复
热议问题