Mobile app version of vmapp.org
Login or Join
Pope3001725

: How to pre-load images used only on hover? I have a link that starts with one background image and then changes on hover to another. Both background images are set in the CSS. My browsers

@Pope3001725

Posted in: #Css #Images #LoadTime

I have a link that starts with one background image and then changes on hover to another. Both background images are set in the CSS. My browsers (any of them) don't seem to load the hover image until you actually hover. But then you end up with a blank image for the several seconds (on my very slow connection) it takes to load the new one. Is there a way to encourage the browser to pre-load the hover image so that delay time doesn't occur on hover?

10.04% popularity Vote Up Vote Down


Login to follow query

More posts by @Pope3001725

3 Comments

Sorted by latest first Latest Oldest Best

 

@Karen161

I found this link on how to pre-load images using pure CSS:
www.thecssninja.com/css/even-better-image-preloading-with-css2
And it worked for me when I need to change the different background images on hover.

10% popularity Vote Up Vote Down


 

@Kevin317

I don't really like the javascript solution - it's messy, difficult to maintain, and of course completely fails when JS is disabled.

The modern solution is to use CSS Sprites - try it, believe me, you'll wonder why you never did this before ;)

10% popularity Vote Up Vote Down


 

@Pope3001725

1) Use CSS Sprites (This is the preferred method).

2) Load the images in a hidden div. Place the images in the div and then set the div's CSS to display: none; to hide it.

3) Load the images with CSS:
#preloadedImages
{
width: 0px;
height: 0px;
display: inline;
background-image: url(path/to/image1.png);
background-image: url(path/to/image2.png);
background-image: url(path/to/image3.png);
background-image: url(path/to/image4.png);
background-image: url();

}


4) Use this JavaScript

<script language="JavaScript">

function preloader()
{
// counter
var i = 0;

// create object
imageObj = new Image();

// set image list
images = new Array();
images[0]="image1.jpg"
images[1]="image2.jpg"
images[2]="image3.jpg"
images[3]="image4.jpg"

// start preloading
for(i=0; i<=3; i++)
{
imageObj.src=images[i];
}
}
</script>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme