css 背景画像のプリローダーを機能させるのに問題があります。状況は次のとおりです。いくつかの隠し要素があり、それぞれに css 背景画像があります。
ユーザーが「開始」ボタンをクリックすると、jquery を使用して要素を表示します (1 つが表示されると、もう 1 つが非表示になります)。ただし、「開始」ボタンは、すべての画像がロードされた後にのみ使用可能にする必要があります。
私はこのプリローダーを使用しています:
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");
});
ただし、これは 1 つのイメージのみをロードします。
他のコードもいくつか試しましたが、結果はありませんでした。画像プリローダーは実際に css バックグラウンド画像で動作する必要がありますか? それともタグ専用ですか?