u-lazy-load.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view class="u-wrap" :style="{
  3. opacity: Number(opacity),
  4. borderRadius: borderRadius + 'rpx',
  5. // 因为time值需要改变,所以不直接用duration值(不能改变父组件prop传过来的值)
  6. transition: `opacity ${time / 1000}s ease-in-out`
  7. }" :class="'u-lazy-item-' + elIndex">
  8. <view :class="'u-lazy-item-' + elIndex">
  9. <image :style="{ borderRadius: borderRadius + 'rpx', height: imgHeight }" v-if="!isError"
  10. class="u-lazy-item" :src="isShow ? image : loadingImg" :mode="imgMode" @load="imgLoaded"
  11. @error="loadError" @tap="clickImg">
  12. </image>
  13. <image :style="{ borderRadius: borderRadius + 'rpx', height: imgHeight }" class="u-lazy-item error" v-else
  14. :src="errorImg" :mode="imgMode" @load="errorImgLoaded" @tap="clickImg"></image>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. addUnit,
  21. guid
  22. } from '../../libs/function/index.js';
  23. /**
  24. * lazyLoad 懒加载
  25. * @description 懒加载使用的场景为:页面有很多图片时,APP会同时加载所有的图片,导致页面卡顿,各个位置的图片出现前后不一致等.
  26. * @tutorial https://uview-plus.jiangruyi.com/components/lazy-load.html
  27. * @property {String Number} index 用户自定义值,在事件触发时回调,用以区分是哪个图片
  28. * @property {String} image 图片路径
  29. * @property {String} loading-img 预加载时的占位图
  30. * @property {String} error-img 图片加载出错时的占位图
  31. * @property {String} threshold 触发加载时的位置,见上方说明,单位 rpx(默认300)
  32. * @property {String Number} duration 图片加载成功时,淡入淡出时间,单位ms(默认)
  33. * @property {String} effect 图片加载成功时,淡入淡出的css动画效果(默认ease-in-out)
  34. * @property {Boolean} is-effect 图片加载成功时,是否启用淡入淡出效果(默认true)
  35. * @property {String Number} border-radius 图片圆角值,单位rpx(默认0)
  36. * @property {String Number} height 图片高度,注意:实际高度可能受img-mode参数影响(默认450)
  37. * @property {String Number} mg-mode 图片的裁剪模式,详见image组件裁剪模式(默认widthFix)
  38. * @event {Function} click 点击图片时触发
  39. * @event {Function} load 图片加载成功时触发
  40. * @event {Function} error 图片加载失败时触发
  41. * @example <u-lazy-load :image="image" :loading-img="loadingImg" :error-img="errorImg"></u-lazy-load>
  42. */
  43. export default {
  44. name: 'u-lazy-load',
  45. props: {
  46. index: {
  47. type: [Number, String]
  48. },
  49. // 要显示的图片
  50. image: {
  51. type: String,
  52. default: ''
  53. },
  54. // 图片裁剪模式
  55. imgMode: {
  56. type: String,
  57. default: 'widthFix'
  58. },
  59. // 占位图片路径
  60. loadingImg: {
  61. type: String,
  62. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAMAAAC3Ycb+AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAv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g2XDfZCqSqOu7rGVf8cHBe8zhC2b61dtUHXv0OkGo6ZL4JkpbRYXdUaFevivx2M/1GIOctNh949TtAoumQ+TpIHMX54CJu+8BDd8FkE5BqcZh/59XvAClmTvKfB0nDqIlHo3T70SftyW1eX9dXtgQJqs1f/Q6QaOa/7wmQKtxH8eiGoCRuovODIO3VxOMmruZbHrLyD7z6DSDtGyT7ew1kf9hNn07c986JTovzzem0Id9wUG+Vk/IDr34DSNR7huZJkMFT6vEhqrPx/j5cnlZML8N6/PAzh9Y99Flm5Yde/c9BquDOkvkKkMP58dA4qi9vivE8JOvGz/j8FokfPpr288+pH2ZPOZrLmeGD+7KOh6dqYWJ48ki7yUg0tz0go/fv/LLddfV3sgOLJyaGPY/zrSlh1a36Arkzoue9CyG35ze6E6/dzO2Ga0EGHqdRJIkfn9/8OEjTW8Vq91ZWh39FeehWA7Nu9ft8CpUEk1WWOyDF0OPyEU2Pnzf/bZC0P6IPzmAvu7KauQBVrgKpJ0tG2arHzX8e5Pb3PezNs/PrX+3JMyCLn9XXf37tPFHvt09WfCDDjx+yyn1/p1V11j7GnB/q3leLuVva79S/tzed+db08YpF4uOZtmz/9oXWMq6BCAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAERECACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiACAgQAQEiIEAEBIiAALELvqt/BBgACqVeUBXxcCkAAAAASUVORK5CYII='
  63. },
  64. // 加载失败的错误占位图
  65. errorImg: {
  66. type: String,
  67. default: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAMAAAC3Ycb+AAAAGXRFWHRTb2Z0d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AEEEAIIIAQQAAhgABCACGAAEIAAYQAAggBBBACCAEEEAIIIAQQQAggfyL/BBgA8PgLdH0TBtkAAAAASUVORK5CYII='
  68. },
  69. // 图片进入可见区域前多少像素时,单位rpx,开始加载图片
  70. // 负数为图片超出屏幕底部多少距离后触发懒加载,正数为图片顶部距离屏幕底部多少距离时触发(图片还没出现在屏幕上)
  71. threshold: {
  72. type: [Number, String],
  73. default: 100
  74. },
  75. // 淡入淡出动画的过渡时间
  76. duration: {
  77. type: [Number, String],
  78. default: 500
  79. },
  80. // 渡效果的速度曲线,各个之间差别不大,因为这是淡入淡出,且时间很短,不是那些变形或者移动的情况,会明显
  81. // linear|ease|ease-in|ease-out|ease-in-out|cubic-bezier(n,n,n,n);
  82. effect: {
  83. type: String,
  84. default: 'ease-in-out'
  85. },
  86. // 是否使用过渡效果
  87. isEffect: {
  88. type: Boolean,
  89. default: true
  90. },
  91. // 圆角值
  92. borderRadius: {
  93. type: [Number, String],
  94. default: 0
  95. },
  96. // 图片高度,单位rpx
  97. height: {
  98. type: [Number, String],
  99. default: '200'
  100. }
  101. },
  102. data() {
  103. return {
  104. isShow: false,
  105. opacity: 1,
  106. time: this.duration,
  107. loadStatus: '', // 默认是懒加载中的状态
  108. isError: false, // 图片加载失败
  109. elIndex: guid()
  110. }
  111. },
  112. computed: {
  113. // 将threshold从rpx转为px
  114. getThreshold() {
  115. // 先取绝对值,因为threshold可能是负数,最后根据this.threshold是正数或者负数,重新还原
  116. let thresholdPx = uni.upx2px(Math.abs(this.threshold));
  117. return this.threshold < 0 ? -thresholdPx : thresholdPx;
  118. },
  119. // 计算图片的高度,可能为auto,带%,或者直接数值
  120. imgHeight() {
  121. return addUnit(this.height);
  122. }
  123. },
  124. created() {
  125. // 由于一些特殊原因,不能将此变量放到data中定义
  126. this.observer = {};
  127. },
  128. watch: {
  129. isShow(nVal) {
  130. // 如果是不开启过渡效果,直接返回
  131. if (!this.isEffect) return;
  132. this.time = 0;
  133. // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的白色),再改成1,是为了获得过渡效果
  134. this.opacity = 0;
  135. // 延时30ms,否则在浏览器H5,过渡效果无效
  136. setTimeout(() => {
  137. this.time = this.duration;
  138. this.opacity = 1;
  139. }, 30)
  140. },
  141. // 图片路径发生变化时,需要重新标记一些变量,否则会一直卡在某一个状态,比如isError
  142. image(n) {
  143. if (!n) {
  144. // 如果传入null或者'',或者undefined,标记为错误状态
  145. this.isError = true;
  146. } else {
  147. this.init();
  148. this.isError = false;
  149. }
  150. }
  151. },
  152. emits: ['click', 'load', 'error'],
  153. methods: {
  154. // 用于重新初始化
  155. init() {
  156. this.isError = false;
  157. this.loadStatus = '';
  158. },
  159. // 点击图片触发的事件,loadlazy-还是懒加载中状态,loading-图片正在加载,loaded-图片加加载完成
  160. clickImg() {
  161. let whichImg = '';
  162. // 如果isShow为false,意味着图片还没开始加载,点击的只能是最开始的占位图
  163. if (this.isShow == false) whichImg = 'lazyImg';
  164. // 如果isError为true,意味着图片加载失败,这是只剩下错误的占位图,所以点击的只能是错误占位图
  165. // 当然,也可以给错误的占位图元素绑定点击事件,看你喜欢~
  166. else if (this.isError == true) whichImg = 'errorImg';
  167. // 总共三张图片,除了两个占位图,剩下的只能是正常的那张图片了
  168. else whichImg = 'realImg';
  169. // 只通知当前图片的index
  170. this.$emit('click', this.index);
  171. },
  172. // 图片加载完成事件,可能是加载占位图时触发,也可能是加载真正的图片完成时触发,通过isShow区分
  173. imgLoaded() {
  174. // 占位图加载完成
  175. if (this.loadStatus == '') {
  176. this.loadStatus = 'lazyed';
  177. }
  178. // 真正的图片加载完成
  179. else if (this.loadStatus == 'lazyed') {
  180. this.loadStatus = 'loaded';
  181. this.$emit('load', this.index);
  182. }
  183. },
  184. // 错误的图片加载完成
  185. errorImgLoaded() {
  186. this.$emit('error', this.index);
  187. },
  188. // 图片加载失败
  189. loadError() {
  190. this.isError = true;
  191. },
  192. disconnectObserver(observerName) {
  193. const observer = this[observerName];
  194. observer && observer.disconnect();
  195. },
  196. },
  197. beforeUnmount() {
  198. // 销毁页面时,可能还没触发某张很底部的懒加载图片,所以把这个事件给去掉
  199. //observer.disconnect();
  200. },
  201. mounted() {
  202. // 此uOnReachBottom事件由mixin.js发出,目的是让页面到底时,保证所有图片都进行加载,做到绝对稳定且可靠
  203. this.$nextTick(() => {
  204. uni.$once('uOnReachBottom', () => {
  205. if (!this.isShow) this.isShow = true;
  206. });
  207. })
  208. // mounted的时候,不一定挂载了这个元素,延时30ms,否则会报错或者不报错,但是也没有效果
  209. setTimeout(() => {
  210. // #ifndef APP-NVUE
  211. // 这里是组件内获取布局状态,不能用uni.createIntersectionObserver,而必须用this.createIntersectionObserver
  212. // this.disconnectObserver('contentObserver');
  213. // nvue 里不支持
  214. const contentObserver = uni.createIntersectionObserver(this);
  215. // 要理解这里怎么计算的,请看这个:
  216. // https://blog.csdn.net/qq_25324335/article/details/83687695
  217. contentObserver.relativeToViewport({
  218. bottom: this.getThreshold,
  219. }).observe('.u-lazy-item-' + this.elIndex, (res) => {
  220. // console.log('relativeToViewport', res)
  221. if (res.intersectionRatio > 0) {
  222. // 懒加载状态改变
  223. this.isShow = true;
  224. // 如果图片已经加载,去掉监听,减少性能的消耗
  225. this.disconnectObserver('contentObserver');
  226. }
  227. })
  228. this.contentObserver = contentObserver;
  229. // #endif
  230. // #ifdef APP-NVUE
  231. this.isShow = true;
  232. // #endif
  233. }, 30)
  234. }
  235. }
  236. </script>
  237. <style scoped lang="scss">
  238. @import "../../libs/css/components.scss";
  239. .u-wrap {
  240. background-color: #eee;
  241. overflow: hidden;
  242. }
  243. .u-lazy-item {
  244. // 骗系统开启硬件加速
  245. transform: transition3d(0, 0, 0);
  246. /* #ifndef APP-NVUE */
  247. // 防止图片加载“闪一下”
  248. will-change: transform;
  249. display: block;
  250. width: 100%;
  251. /* #endif */
  252. }
  253. </style>