css代碼:
ul#portfolio{margin:0;padding:0;}
ul#portfolio li{float: left;margin:0 5px 0 0;width:250px;height: 250px;list-style: none;}
ul#portfolio li.loading{background: url(../images/spinner.gif) no-repeat center center;}
ul#portfolio li img{width:250px;height: 250px;display: block;}
js代碼:
$(function(){
var images=new Array();
images[0]='./images/ads_one.jpg';
images[1]='./images/ads_two.jpg';
images[2]='./images/ads_three.jpg';
//獲取了圖像的數量
var max=$(images).length;
//如果包含一張以上的圖像,那麼創建對應的UL元素家人到wrapper div中,並且調用LoadImage方法。
if(max>0){
//create the UL element
var ul=$('<ul id="portfolio"></ul>');
//append to div#wrapper
$(ul).appendTo($('#wrapper'));
//load the first image
LoadImage(0,max);
}
//在LoadImage方法中,循環遍歷所有的圖像,對每個圖像創建li元素
function LoadImage(index,max){
if(index<max){
//利用attr方法為li元素增加了css樣式,即加上了loading的gif背景。
var list=$('<li id="portfolio_'+index+'"></li>').attr('class','loading');
//把li添加到ul元素中
$('ul#portfolio').append(list);
//獲取當前的li元素
var curr=$("ul#portfolio li#portfolio_"+index);
//創建圖像元素
var img=new Image();
//加載圖像
$(img).load(function(){
$(this).css('display','none');
$(curr).removeClass('loading').append(this);
$(this).fadeIn('slow',function(){
//采用回調函數的方法,在當前元素成功執行fadeIn方法之後 再去調用下一個元素的LoadImage方法,這樣就能實現多個圖像的順序加載了。
LoadImage(index+1,max);
});
}).error(function(){
$(curr).remove();
LoadImage(index+1,max);
}).attr('src',images[index]);
}
}
})
以上所述是小編給大家介紹的jQuery 實現圖片的依次加載圖片,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!