方法一:原生js
let imgTag = new Image()
imgTag.src = 'https://t7.baidu.com/it/u=1744157654,1808520583&fm=193&f=GIF'
imgTag.onload=function(){
console.log('图片加载完成才会执行的回调')
}
let imgList = [
{id:0,src:'https://t7.baidu.com/it/u=1744157654,1808520583&fm=193&f=GIF'},
{id:1,src:'https://img1.baidu.com/it/u=2788818474,2047444862&fm=26&fmt=auto'},
{id:2,src:'https://img0.baidu.com/it/u=770046254,637068900&fm=26&fmt=auto'},
]
let imgTags = []
let loadNum = 0
let imgTotal = imgList.length
for(var i = 0;i<imgTotal;i++){
let imgTag = imgTags[i]
imgTag=new Image()
imgTag.src = imgList[i]
imgTag.onload=function(){
loadNum++
if(loadNum==imgTotal){
}
}
}
方法二: 结合Promise
new Promise((resolve,reject)=>{
let imgTag = new Image()
imgTag.src = 'https://t7.baidu.com/it/u=1744157654,1808520583&fm=193&f=GIF'
imgTag.onload=function(){
console.log('图片加载完成才会执行的回调')
resolve(logo)
}
}).then(logo=>{
})
let imgList = [
{id:0,src:'https://t7.baidu.com/it/u=1744157654,1808520583&fm=193&f=GIF'},
{id:1,src:'https://img1.baidu.com/it/u=2788818474,2047444862&fm=26&fmt=auto'},
{id:2,src:'https://img0.baidu.com/it/u=770046254,637068900&fm=26&fmt=auto'},
]
let imgTags = []
let imgTotal = imgList.length
let promiseAll=[]
for(var i = 0;i<imgTotal;i++){
promiseAll[i] = new Promise((resolve,reject)=>{
let imgTag = imgTags[i]
imgTag=new Image()
imgTag.src = imgList[i]
imgTag.onload=function(){
resolve(imgTag)
}
})
}
Promise.all(promiseAll).then(imgTag=>{
})