how to load image in background?

风格不统一 提交于 2019-12-21 14:09:08

问题


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



来源:https://stackoverflow.com/questions/6294733/how-to-load-image-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!