我一直在使用PHP和Javscript中的圖片庫很長一段時間。畫廊的「賣點」是它預加載圖像,因此當您切換到下一張圖像時,您不必重新加載頁面和它幾乎是瞬間的。加載後不會更改爲圖片(圖片庫) - Javascript
問題是,當您切換到尚未加載的照片時,它會加載之前的每張圖像。我想加載應該當前顯示給觀衆的圖像。但即使當控制檯顯示當前圖像已加載(我設置的console.log消息)時,它仍會在加載之前切換到所有其他照片。
有問題的example page。 我會把一些最重要的代碼放在這裏,因爲我不想給出所有的代碼。
此功能load_image將下一張要加載的照片添加到hidden_container中。問題是當current_photo加載時,它不會改變。您可以通過查看控制檯並使用箭頭鍵一直走到最後一張照片來在頁面中測試。
function load_image() {
// find the next photo to load
var next_to_load = next_photo_to_load();
//stop condition:
if (next_to_load == null)
return false;
if (img[current_photo]['loaded'] == 0) {
console.log("current_photo: "+current_photo);
next_to_load = current_photo;
console.log("next_to_load: "+next_to_load)
}
else {
console.log("*current_photo loaded*");
$("main_img").src = img[current_photo]['img'];
}
img[next_to_load]['loaded'] = 1;
var url = img[next_to_load]['img'];
var image = new Image();
image.src = url;
image.onload = function() {
load_image();
};
document.getElementById("hidden_container").appendChild(image);
}
此外,load_image最初在第一張照片加載此代碼時被調用。 (我包括這個,因爲它解釋了額外的控制檯日誌)。
img[current_photo]['loaded'] = 1;
console.log("loaded:"+current_photo);
load_image();
感謝您閱讀所有這些內容。我知道它很長。
我不知道你在問什麼。我可能讀了你的代碼錯誤,但是在任何階段你都不會設置current_photo的值(除了最後一個代碼片段)。當我瀏覽時,我在控制檯中得到的所有內容都是「* current_photo loaded *」以及「loaded:x」,其中x是要加載的正確照片(我假設)。 – djlumley 2011-05-31 05:34:06
@djlumley:不,你沒有看錯代碼。通過「設置current_photo的值」,你的意思是將main_img src設置爲current_img,還是意味着改變current_img的加載狀態? 要查看加載錯誤的照片,您可以按右箭頭直至最後一張照片(請查看右上角)並查看控制檯。 – SamB 2011-05-31 18:56:29