微信小程序> 小程序图片预加载

小程序图片预加载

浏览量:692 时间: 来源:Shine_start

1.ImgageLoader.js

let base = 0;let Img = function (src) {  this.src = src;  this.status = false;  this.fail = false;}let loop = (o, res) = {  let tem = Object.keys(o);  tem.map(v = {    if (typeof o[v] === 'object') {      loop(o[v], res);    } else {      if (v === 'BASE') {        base = o[v];      } else {        res.push(o[v]);      }    }  });}function ImageLoader(obj) {  let arr = []; if (obj.loading) {    this.loadingcallback = obj.loading;  }  if (obj.loaded) {    this.loadedcallback = obj.loaded;  }  if (obj.base) {    base = obj.base  }  this.insert = (item) = {    arr.push(item);  };  this.init = (o) = {    let res = [];    loop(o, res);    console.log(res)    res.map((v) = {      this.insert(new Img(v));    });    this.load();  };  this.load = () = {    this.start = (new Date).getTime();    arr.map((v) = {      let src = base ? base + v.src : v.src;      wx.getImageInfo({        src: src,        success: res = {          v.status = true;        },        fail: err = {          v.fail = true;        }      })    });    let timer = setInterval(() = {      let status = this.isLoaded();      if (status) {        clearTimeout(timer);      }    }, 200);    setTimeout(() = {      clearTimeout(timer);    }, 60000);  };  this.isLoaded = () = {    let status = true,      count = 0,      fail = 0;    arr.map((v) = {      if (!v.status) {        status = false;      } else {        count += 1;      }      if (v.fail) {        status = true;        fail += 1;      }    });    if (status) {      if (this.loadedcallback) {        this.loadedcallback({          status: true,          timecost: (new Date).getTime() - this.start,          success: count,          fail: fail,          totalcount: arr.length        })      }    } else {      if (this.loadingcallback) {        this.loadingcallback({          status: false,          percent: count / arr.length        });      }    }    return status;  };  if (obj.source) {    this.init(obj.source);  }}module.exports = ImageLoader;

2.ImageSource.js   需要进行预加载的图片

module.exports = {  "BASE": "https://.....", //资源路径  "imageList": [    "game.png",    "game_0.png",    "game_1.png",    "game_2.png",    "game_3.png",    "game_4.png",    "game_5.png",    "game_6.png",    "game_7.png",  ],  "index":[   "p_55.png",    "p_1.png",    "p_49.png",    "p_50.png",    "p_72.png",    "p_74.png",    "p_78.png",     "p_bj1.png",    "p_58.png",  ]}

3.使用

const ImageSource = require('../../ImageSource.js');const ImageLoader = require('../../utils/ImageLoader.js');var loader=new ImageLoader({  base: ImageSource.BASE ,  source: ImageSource.imageList,  loading: res = {    // 可以做进度条动画    console.log(res);  },  loaded: res = {    // 可以加载完毕动画    console.log(res);  }});

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎