2011-05-31 92 views
0

我一直在使用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(); 

感謝您閱讀所有這些內容。我知道它很長。

+0

我不知道你在問什麼。我可能讀了你的代碼錯誤,但是在任何階段你都不會設置current_photo的值(除了最後一個代碼片段)。當我瀏覽時,我在控制檯中得到的所有內容都是「* current_photo loaded *」以及「loaded:x」,其中x是要加載的正確照片(我假設)。 – djlumley 2011-05-31 05:34:06

+0

@djlumley:不,你沒有看錯代碼。通過「設置current_photo的值」,你的意思是將main_img src設置爲current_img,還是意味着改變current_img的加載狀態? 要查看加載錯誤的照片,您可以按右箭頭直至最後一張照片(請查看右上角)並查看控制檯。 – SamB 2011-05-31 18:56:29

回答

0

你可以嘗試這樣的事情,我已經在最近開發的移動畫廊中使用過。

var imageList = $('#imagelist'); //id of div tag 
imageList.empty(); 
var imageFiles = '<?=$images_js?>'; 
imageFiles = $.parseJSON(imageFiles); //convert to json for javascript readability 

var images = []; 
for(i = 0; i<imageFiles.length; i++){ 
    var image = document.createElement('img'); 
    image.src = imageFiles[i]; 
    images.push(image); 
} 

var count = imageFiles.length; 
var i = 0; 

imageList.append(images[i]); 

你可以看到我在javascript中使用了一個php變量。我已經這樣做了,所以我可以讓這個JavaScript包含的php文件動態地讀取文件夾的內容。

php文件非常小,包括:

<?php 

$imagesDir = 'gallery/img/'; 
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); 

$images_js = json_encode($images); 
?> 

這裏是在行動jQuery的:

$('#contentPlaylist').live('swipeleft swiperight',function(event){ 

    if (event.type == "swipeleft") { 
     imageList.fadeOut('slow', function(){ 
      imageList.empty(); 

      if(i != 0){ 

       i--; 
       imageList.append(images[i]);    
       imageList.fadeIn('slow'); 
      } 


      else{ 
       i = count - 1; 
       imageList.append(images[i]);    
       imageList.fadeIn('slow'); 

      } 
     }); 
    } 
    if (event.type == "swiperight") { 
      imageList.fadeOut('slow', function(){ 
       imageList.empty(); 
       i++;   
       if(i < count){ 

        imageList.append(images[i]);    
        imageList.fadeIn('slow'); 
       } 
       else{ 

        imageList.append(images[0]); 

        imageList.fadeIn('slow'); 
        var reset = 0; 
        i = reset; 
       } 
      }); 
    } 
+0

糾正我,如果我錯了,但不是一次加載所有的圖像?關於我的圖片庫的另一個具體問題是,它只能在一次加載一張圖片,因此您可以更快地查看下一張圖片。 – SamB 2011-05-31 05:11:13

+0

它一次加載是,但可以在加載後立即顯示第一張圖像。直到您設置點擊功能在循環中增加或減少時纔會顯示其他圖像。我可以在編輯我的帖子的那一刻添加該代碼。 – lockdown 2011-05-31 05:18:20

+0

我不知道我的理解。這對我的畫廊的工作方式是一種限制,一次只能加載一張圖片。我對你所說的內容的理解是,它會加載主圖像,然後同時加載所有圖像。 – SamB 2011-05-31 05:20:58