图片异步加载

作者: 冷r | 来源:发表于2019-09-29 10:46 被阅读0次
function creatImg(url) {
    return new Promise((resolve, reject) => {
        const oImg = new Image();
        oImg.onload = () => {
            resolve(oImg);
        };
        oImg.onerror = () => {
            reject(new Error(`img url:${url} is not find!`));
        };
        oImg.src = url;
    });
}

const imgList = ['/timg.jpg', '/xiaohai.jpg', '/timg.jpg', '/timg.jpg'];
const oImgList = [];

imgList.forEach(imgUrl => {
    oImgList.push(creatImg(imgUrl));
});

Promise.all(oImgList).then(oImg => {
    oImg.forEach(img => {
        document.body.append(img);
    });
});

相关文章

网友评论

    本文标题:图片异步加载

    本文链接:https://www.haomeiwen.com/subject/pvqfpctx.html