js加载完所有的图片,适合电子相册哦~~~~

时间:2020-12-15 21:59:24

有些时候要一口气加载完所有的图片,所以代码就要这么写。。。。

        var imgArray = [
'http://s16.mogucdn.com/p1/160202/upload_ifqwcmlcmyywmnrxgyzdambqgyyde_750x1206.jpg',
'https://img.alicdn.com/tps/TB19uTbLpXXXXaEaXXXXXXXXXXX-520-280.jpg',
'http://s18.mogucdn.com/p1/160203/upload_ifqwizbthaztgyzxgyzdambqgyyde_666x610.png',
'http://s18.mogucdn.com/p1/160203/upload_ifrdcmlcge4tgyzxgyzdambqmeyde_743x242.png',
'http://imgsrc.baidu.com/forum/pic/item/8d5494eef01f3a2957d7fc4a9925bc315c607c12.jpg',
'http://img.bizhi.sogou.com/images/2013/01/29/330291.jpg',
'http://s16.mogucdn.com/p1/160202/upload_ie4wmzbwmvsdcobxgyzdambqgqyde_750x1334.gif',
'http://s16.mogucdn.com/p1/160202/upload_ifqwcmlcmyywmnrxgyzdambqgyyde_750x1206.jpg',
'http://s18.mogucdn.com/p1/160203/upload_ifqwizbthaztgyzxgyzdambqgyyde_666x610.png',
'http://s18.mogucdn.com/p1/160203/upload_ifrdcmlcge4tgyzxgyzdambqmeyde_743x242.png',
'http://s16.mogucdn.com/p1/160201/upload_ifrwinrugntdqmzxgyzdambqhayde_750x1206.jpg',
'http://s16.mogucdn.com/p1/160202/upload_ie4wmzbwmvsdcobxgyzdambqgqyde_750x1334.gif'
]; function imgLoad(url,callback) {
var img = new Image();
img.src = url;
if (img.complete) {
if(typeof(callback)==='function') callback();
} else {
img.onload = function () {
if(typeof(callback)==='function') callback();
img.onload = null;
};
}
} function preImgLoad(array,callback){
var index = 0;
while(index < array.length){
if(index==array.length-1){
imgLoad(array[index],callback)
}else{
imgLoad(array[index],array[index+1])
}
index ++;
}
} preImgLoad(imgArray,function(){console.log('图片已经全部加载成功')})

睡觉去。。。。