I am having troubles making a working preloader for css background images. Here is the situation: I have several hidden elements, each one has a css background image.
When the user clicks on a "start" button, I then use jquery to show the elements (one after an other: as one is shown, the other is hidden). However, the "start" button should be available only after all the images are loaded.
I am using this preloader:
function preloadimages(arr) {
var newimages=[], loadedimages=0
var postaction=function(){}
var arr=(typeof arr!="object")? [arr] : arr
function imageloadpost() {
loadedimages++
if (loadedimages == arr.length) {
postaction(newimages) //call postaction and pass in newimages array as parameter
}
}
for (var i=0; i<arr.length; i++) {
newimages[i] = new Image()
newimages[i].src = arr[i]
newimages[i].onload = function() {
imageloadpost()
}
newimages[i].onerror = function() {
imageloadpost()
}
}
return { //return blank object with done() method
done:function(f) {
postaction = f || postaction //remember user defined callback functions to be called when images load
}
}
}
preloadimages('img01','img02','img03')
.done(function(images) {
alert(images.length + " images loaded");
});
This, however, loads only 1 image.
I tried some other codes as well, with no results. Should image preloaders actually work on css background images? Or are they meant only for tags?