I have a webpage where I want to switch the background image of a div in response to user interaction. What I still have:
function updateImg() {
imgurl=buildImgURL();
$('#mainImage').css("background-image", "url("+imgurl+")");
}
This works exactly the same as I want, except for one. Due to the combination of relatively large background images and a relatively slow server (none of which can be easily fixed), it takes some time to load the image. Therefore, when the updateImg () function is called a div, it runs for half a second or so before a new background image is loaded. I want the old background image to remain until the new image finishes loading, and then instantly switches. Can this be done?
The background image is generated on the fly by the server, so any caching or preloading on the client side is not possible.
source
share