how to load image in background?

2019-04-09 06:41发布

问题:

Problem: I am creating an album.So on every press of

time "NEXT" button I am loading new Images. What I want to achieve is, I want to switch from old image to new image only once the new images has been downloaded fully from server.

Actually I dont want to show partial images while loading.
Is there any solution?

PS SIMILAR QUESTION but this is for Iphone. I need for browser?

回答1:

Just make a new image via javascript, and only show the image after the onload has fired. This will make sure you don't see any partial rendering of the image.

Live Demo

    var curImg = new Image();

    curImg.src = "url to your image";
    curImg.onload = function(){
        // do whatever here, add it to the background, append the image ect.
        imgHolder.appendChild(curImg);   
    }


回答2:

You can play with 2 img tags. img1 shows the first image, while img2, is hidden, and loads the next image.

At the onload event of img2, you can hide img1, and show img2.

And load the next image with img1, and so on.



回答3:

link rel="prerender" might do the trick.

http://www.catswhocode.com/blog/mastering-html5-prefetching