// Promise加载图片
var loadImg = (url) => {
return new Promise((resolve, reject) => {
// 创建图片
var img = new Image()
// 加载成功
img.onload = () => {
resolve(img)
}
// 图片加载
img.onerror = () => {
reject(new Error('图片加载失败'))
}
img.src = url
})
}
var imgs = ['https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2553992741.webp',
'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2555538168.webp',
'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2555538168.webp'
]
// 并行加载
var loadImgAsync = (imgs)=>{
imgs.forEach(async (item)=>{
let img = await loadImg(item)
document.body.appendChild(img)
});
}
loadImgAsync(imgs)
如何实现十张十张加载呢
登录后可查看更多问答,登录/注册