load specific image before anything else

前端 未结 5 1351
一整个雨季
一整个雨季 2020-12-15 23:25

I\'m a creating a loading screen for website I am making. The website loads many images, scripts, etc. The HTML and CSS part is great, but I need a way to guarantee that the

相关标签:
5条回答
  • 2020-12-15 23:47

    I'm not sure if it's possible to enforce.

    If it is, try adding this in the head-tag:

    <script type="text/javascript">
        if(document.images)
            (new Image()).src="http://www.image.com/example.png";
    </script>
    

    In theory that may load and cache that image before anything else.

    0 讨论(0)
  • 2020-12-15 23:53

    You can reuse resource prealoding browser support.

    I'm not sure it works across all browsers but in my case this approach helps me to load images first. Also it allows to define concrete images so UI specific could be skipped

    First define in header what resource you want to preload and define resource priority

    <link rel="preload" href="link-to-image" as="image"> 
    

    or

    <link rel="preload" href="link-to-image">
    

    Second line allow to increase loading priority across all object types (scripts / images / styles). First line - only through images.

    Then define in body link to image as usual:

    <img src="link-to-image" alt="">
    

    Here is my working example https://jsfiddle.net/vadimb/05scfL58/

    0 讨论(0)
  • 2020-12-16 00:02

    I think if you place the IMG tag at the top of your html body it will be loaded first. If you do not want to move your div just use a copy of the image tag. Once the images is loaded it will be shown in every image tag which shows the same picture.

    0 讨论(0)
  • 2020-12-16 00:05

    As long as the "loading..." image is positioned before any other html elements, it should load first. This of course depends on the size of the image. You could put the loading div right after the tag and position it using 'position:absolute'. Regarding the code to remove the loading screen, one method is to do the following.

    • Put all the images, scripts that need to be loaded in a hidden div (display: none)
    • Set up a variable that will hold the total of the images / scripts to be loaded
    • Set up a counter variable
    • Attach to each image / script the "onload" event
    • Everytime the "onload" event is triggered it will call a function that will increment the counter variable and check if the value of the counter equals the value of the total variable
    • If all resources have been loaded, fire a custom event that will show the div with the images, and hide the div with the loading screen.

    The code below isn't testes so it might not work. Hope it helps

    var totalImages = 0;
    var loadCounter = 0;
    
    function incrementLoadCounter() {
       loadCounter++;
       if(loadCounter === totalImages) {
          $(document).trigger('everythingLoaded');
       }
    }
    
    function hideLoadingScreen() {
       $('#loadingScreen').hide();
       $('#divWithImages').show();
    }
    
    $(document).ready(function(e) {
        $('#loadingScreen').bind('everythingLoaded', function(e) {
            hideLoadingScreen();
        });
        var imagesToLoad = $('img.toLoad');
        totalImages = imagesToLoad.length;
    
        $.each(imagesToLoad, function(i, item) {
            $(item).load(function(e) {
               incrementLoadCounter();
            })
        });
    })
    
    0 讨论(0)
  • 2020-12-16 00:12

    Or you could use spin.js as loading image. It display this "loading cycle image" via javascript.

    Check it out under: http://fgnass.github.com/spin.js/

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