一、插件(文件存放位置:/components/loader/)

1、img-loader.js

Component({properties: {},data: {imgs: [],list: [],success: function () {}},lifetimes: {attached() {}},methods: {//加载load(opts) {const that = thisconst imgs = opts.imgs || []const success = opts.success || function () {}that.setData({imgs: imgs,success: success}, () => {that.callBack()})},//初始回调callBack() {const that = thisconst list = that.data.listlist.forEach(item => {if (item.loaded) {that.data.success(item)}})},//成功回调imgOnLoad(e) {const that = thisconst src = e.currentTarget.dataset.srcconst width = e.detail.widthconst height = e.detail.heightlet list = that.data.listlet has = falselist = list.map(item => {if (src == item.src) {has = trueitem.loaded = true}return item})if (!has) {list.push({src: src,width: width,height: height,loaded: true})}that.setData({list: list}, () => {that.data.success({src: src,width: width,height: height,loaded: true})})},//错误回调imgOnLoadError(e) {const that = thisconst src = e.currentTarget.dataset.srcthat.data.success({src: src,loaded: false})}},attached() {}
})

2、img-loader.json

{"component": true,"usingComponents": {}
}

3、img-loader.wxml

<image wx:for="{{imgs}}" wx:key="*this" src="{{item}}" data-src="{{item}}" bindload="imgOnLoad"binderror="imgOnLoadError" class="img-loader"></image>

4、img-loader.wxss

.img-loader {display: none;width: 0;height: 0;opacity: 0;
}

二、插件引用方法

1、wxml

<img-loader id="imgLoader"></img-loader>

2、json

{"usingComponents": {"img-loader": "/components/loader/img-loader"}
}

3、js

const app = getApp()
Page({data: {photoList:[],imgList: []},onLoad() {},onReady() {},onShow() {const that = thisconst actions = [that.getPhotoList()]Promise.all(actions.map((p) => {return p.catch(error => error)})).then(res => {let photoList= that.data.photoListlet imgList = that.data.imgListlet has = falselet loaded = falseif (0 == res[0].errcode) {photoList = res[0].dataphotoList = photoList.map(item => {has = falseloaded = falseimgList.forEach(img => {if (img.url == item.photo_url) {has = trueloaded = img.loaded}})if (!has) {imgList.push({url: item.photo_url,loaded: false})}item.loaded = loadedreturn item})}that.imgLoader = that.selectComponent('#imgLoader')that.setData({photoList: photoList,imgList: imgList}, () => {that.loadImages()})}).catch(error => {})},//获取照片列表getPhotoList() {return app.invokeApi('api_url')},//加载图片loadImages() {const that = thislet photoList = that.data.photoListlet imgList = that.data.imgListlet imgs = imgList.map(item => {return item.url})that.imgLoader.load({imgs: imgs,success: res => {const src = res.srcif (res.loaded) {photoList.forEach((item, index) => {if (src == item.photo_url) {that.setData({['photoList[' + index + '].loaded']: true})}})imgList.forEach((item, index) => {if (src == item.url) {that.setData({['imgList[' + index + '].loaded']: true})}})}}})}
})

微信小程序图片懒加载插件相关推荐

  1. 微信小程序--图片懒加载

    定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力.惰性加载是程序人性化的一种体现,提高用户体验,防止 ...

  2. 【愚公系列】2022年09月 微信小程序-图片懒加载功能实现

    文章目录 前言 一.官方图片的懒加载 1.wxml 2.js 3.css 4.效果 二.第三方包实现图片的懒加载 1.安装第三方包 2.组件引用 3.wxml 4.js 5.css 6.效果 前言 大 ...

  3. 微信小程序图片懒加载实现

    使用微信提供的 IntersectionObserver 对象 IntersectionObserver 对象,用于推断某些节点是否可以被用户看见.有多大比例可以被用户看见. 在页面渲染开始时,通过这 ...

  4. 微信小程序封装懒加载图片

    微信小程序封装懒加载图片 js /components/LazyImage/index.js // components/LazyImage/index.js Component({/*** 组件的属 ...

  5. 小程序图片懒加载较完美解决方案

    无需考虑数据结构,效果如图 话不多说,先上代码 wxml <view class="content"><block wx:key="{{img}}&qu ...

  6. 微信小程序图片的加载

    在微信小程序中,要显示一张图片,有两种图片加载方式: 加载本地图片 <image class="widget__arrow" src="/image/.png&qu ...

  7. 微信小程序——图片的加载与获取手机内部的图片

    关于将手机里的图片放在上传到小程序,在小程序里这种方法并不陌生,甚至大多数小程序都附带有这种功能,那么这种功能是怎么实现的呢,一起来看看吧!!! 1.微信小程序加载图片的几种方法 1.本地图片的加载 ...

  8. 微信小程序 # 图片预加载方案(防闪烁) 以及 首次引导

    问题背景 第一次进入小程序的时候,需要打开引导操作 点击进入下一个引导操作,一共有一系列引导操作. 引导操作这里是用图片实现的,然后点一下切换一张图片. 问题具体描述 在如上图所示的引导提示操作中,用 ...

  9. 小程序图片懒加载放在服务器,【小程序】使用uni-app搭建小程序环境---图片懒加载...

    延迟加载的理念:页面初始化时,暂不加载处于屏幕可见区域之外的图片.该方案会有如下几大好处:\n加快页面渲染速度 \n提升页面滚动性能 \n默认不下载屏幕外的图片,减少网络流量 主标题 列表二级标题 e ...

  10. 今天突然发现,微信小程序手机访问正常,PC端访问小程序图片无法加载?

    今天突然发现,微信小程序手机访问正常,PC端访问小程序图片无法加载? 所有图片在pc上查看都无法显示. 然后使用抓包工具抓包,pc端图片全部403,结果为 AccessDenied You are d ...

最新文章

  1. 延毕率逐年上升!我国博士研究生累计招生已近150万!
  2. python ‘float‘object is not iterable
  3. HDU 1271整数对
  4. java idea连数据库报错:Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone'时区错误
  5. [转]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件
  6. mysql中创建唯一索引的关键字_mysql中唯一索引的关键字是什么
  7. 搭建一款开源的微信商城小程序:海风小店
  8. weblogic12升级jdk_如何修改WEBLOGIC的JDK版本
  9. 494. 目标和(JavaScript)
  10. Spring Boot学习总结(2)——Spring Boot整合Jsp
  11. sql必知必会的数据初始化
  12. OSG仿真案例(9)——JY61陀螺仪控制飞机姿态
  13. 怎么将音频原始文件导入matlab,Matlab2019b音频文件读取
  14. 百度云无限速下载工具:JDownloader 2 for Mac
  15. 第一章-数据规范-数据分析报表设计标准
  16. ElementUI中前端分页的实现
  17. IOS开发笔记和技巧
  18. Mysql存储引擎Innodb的读写锁、行级锁
  19. lopatkin俄大神精简Windows 10 Enterprise 19041.331 20H1 Release x86-x64 EN-RU PIP
  20. linux设备驱动程序第二版 序言

热门文章

  1. 网易微专业python数据分析_网易微专业_Python数据分析师 01 数据思维导论:如何从数据中挖掘价值?...
  2. volatility常用的命令
  3. clustalw序列比对_序列比较中ClustalW和BLAST的区别
  4. 基于STM32的AD9854模块调试总结
  5. AD9854PCB的绘制以及调试中存在的问题以及解决方法
  6. HTML中的botton type=reset标签失效(不起作用)的可能原因。
  7. vue和风天气,天气预报
  8. 光纤接头截面工艺分类
  9. 汉洛塔 简单算法(c和python)
  10. 单域安全评估以及加固方案