2012-04-22 30 views
0

是我在jquery中寫的這個函數,會不會是預加載的好方法? 謝謝這個函數可以預先裝好jquery嗎?

function loader(){ 

     imgTag('#check'); 

     function imgTag(whereToAppend){ 
      var image = 'image.jpg'; 

      $(whereToAppend).append('<img id="imgWrapper">'); 

      $('#imgWrapper').attr('src', image).hide().one('load',function(){ 
       $(this).fadeIn('slow'); 
      }) 
     } 


    } 
+0

我ddnt複製任何問題! – takeItEasy 2012-04-22 18:34:50

回答

1

我會建議使用下述溶液預載圖片:

function preload(arrayOfImages) { 
    $(arrayOfImages).each(function(){ 
     $('<img/>')[0].src = this; 
     // Alternatively you could use: 
     // (new Image()).src = this; 
    }); 
} 

// Usage: 

preload([ 
    'img/imageName.jpg', 
    'img/anotherOne.jpg', 
    'img/blahblahblah.jpg' 
]); 

來源:Preloading images with jQuery

+0

什麼意思[0]? – takeItEasy 2012-04-22 18:15:04

+0

jQuery返回一個集合,你只需要第一個元素。因此,您使用'[0]'或'.get(0)' – Johan 2012-04-22 18:18:21

+0

,這樣img標記就會隱藏,直到圖像完全下載? – takeItEasy 2012-04-22 18:21:27

相關問題