《 vue 》图片批量预加载

/*
*   单独创建个js 并引用自动加载 资源预加载  20200329
*/
(function(){
    let images = [
         require('../assets/img/again.png'),
         require('../assets/img/answerBg.png'),
         require('../assets/img/answerFloor.png'),
         require('../assets/img/cheer.png'),
         require('../assets/img/circular.png'),
         require('../assets/img/circular2.png'),
         require('../assets/img/crown_bg.png'),
    ];

    let image = [];
    let index = 0;

    for (const key in images) {
    	 // 计数器
         index += 1;
         // 创建image对象
         image[index] = new Image();
         // 把图片地址赋值
         if (images.hasOwnProperty(key)) {
             const img = images[key];
             image[index].src = img;
         }
    }
})()

说明1 : 像下面这样写行不通,大家可以试试。 如果能找到具体原因欢迎反馈。

/*
*   单独创建个js 并引用自动加载 资源预加载  20200329
*/
(function(){
    let images = [
         '../assets/img/again.png',
         '../assets/img/answerBg.png',
         '../assets/img/answerFloor.png',
         '../assets/img/cheer.png',
         '../assets/img/circular.png',
         '../assets/img/circular2.png',
         '../assets/img/crown_bg.png',
    ];

    let image = [];
    let index = 0;

    for (const key in images) {
    	 // 计数器
         index += 1;
         // 创建image对象
         image[index] = new Image();
         // 把图片地址赋值
         if (images.hasOwnProperty(key)) {
             const img = require(images[key]);
             image[index].src = img;
         }
    }
})()

图片加载情况 可以通过控制台查看
在这里插入图片描述

大家也可以自己做 加载进度 , 自己扩展吧。
借鉴了网络上的文章 我自己做了改变。。 感谢分享!

借鉴的文章地址 https://www.jianshu.com/p/1618cb183d28

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值