css の背景画像をプリロードする方法については昔からの質問ですが、少しひねりを加えています。背景画像は、Vimeo からのように jQuery を介して設定されています。各背景画像は vimeo のビデオ画像なので、サイトの読み込み時に vimeo サムネイル URL を使用して背景を設定しています。どういうわけかこれらの画像をプリロードできますか? コンテンツが読み込まれたときに消えたいオーバーレイを配置していますが、努力しても、オーバーレイが消えても背景画像が読み込まれているように見えます!
ここに私が試したいくつかの(それほど雄弁ではない)コードがあります:
var count = $('.video_stub').length;
$('.video_stub').each(function(index) {
var bgUrl = $(this).data('thumb');
$('<img/>')[0].src = bgUrl;
if(index == (count - 1)) {
$('.video_stub').each(function(i) {
var bgUrl = $(this).data('thumb');
// Set the video thumb's background using the vimeo thumb image
$(this).css('background-image', 'url(' + bgUrl + ')');
if(index == (count - 1)) {
$('#temp_overlay').fadeOut('slow');
}
});
}
});
何か案は?よろしくお願いします。
編集:
このコードを試しましたが、成功しませんでした。それはスコープの問題なのか疑問に思っていますか?ロード コールバック内からインデックス変数にアクセスできない可能性があります。
// Set video thumb click handler
var count = $('.video_stub').length;
// Set video thumb click handler
// Iterate through each video stub
$('.video_stub').each(function(index) {
var bgUrl = $(this).data('thumb');
$('<img/>').attr('src', bgUrl).load(function() {
$('.video_stub:eq(' + index + ')').css('background-image', 'url(' + bgUrl + ')');
});
if(index == (count - 1)) {
$('#temp_overlay').fadeOut('slow');
}
});
私は試した:
$('<img/>').attr('src', bgUrl).load(function() {
$(this).css('background-image', 'url(' + bgUrl + ')');
});
無駄に。