How to pre-load images used only on hover?
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>
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 ;)