2012-03-14 55 views
0

我需要一些幫助,我似乎無法弄清楚如何預加載圖像我的下一個按鈕在我建立的圖像查看器。我從MySQL數據庫抓取圖像源,並在每次按下下一個按鈕時遍歷它們。我如何預先加載這些?這裏是我的下一個按鈕的代碼:從jquery預加載圖像從MySQL

$("#next").click(function(){ 
      $(h).empty(); 
      var p = <?php echo json_encode($results); ?>; 
      var start = $("#intabdiv img").attr("class"); 
      var next = p[($.inArray(start, p) + 1) % p.length]; 
      //Checking the ID of thumbnail pictures and determining the id here 
      var hinext = "../php/admin/portfolio/before/"+next; 
      $("#intabdiv img").attr('src',hinext); 
      $("#intabdiv img").attr('class',next); 
      //Centering image on screen 
      var h = $("#intabdiv img").width() + 70; 
      //ww is defined beforehand and is the windows width. 
      var total = ww - (h/2); 
      $(".popupbox").css("margin-left", total); 
     }); 
+0

「var p」的內部是什麼樣的?現在很難猜測。 – 2012-03-14 16:10:00

+0

這是一個簡單的數組,看起來像這樣(image1.jpg,image2.jpg,image3.jpg)。謝謝 – liveandream 2012-03-14 17:41:54

回答

0

不喜歡你的代碼。嘗試每隔一段時間放一些換行符(在評論通常是一個好地方之前)。

無論如何,這是使用jquery預加載的圖像的好方法:

$('<img />').attr('src', 'http://url/to/image.jpg'); 

第一部分創建一個圖像元素(但不將其添加到DOM)

$('<img />') 

的下一部分設置SRC並因此在瀏覽器中加載圖像資源:

attr('src', 'http://url/to/image.jpg'); 
+0

但我的圖像結果是在一個數組中,並放置在var p。我將如何預加載這些。這是我的問題。謝謝 – liveandream 2012-03-14 16:06:18

+0

非常感謝!我所要做的只是添加一個.each循環來遍歷數組,並將其放入內部。 Thanksss! – liveandream 2012-03-14 21:02:06