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

前端 未结 3 1029
孤独总比滥情好
孤独总比滥情好 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: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:

提交回复
热议问题