1.说明

开发不易,能给插件一个好评**吗,拜托了!

监听scroll-view标签中的scroll事件,实现图片懒加载。
请尽快更新版本


2.下载地址

github: https://github.com/2460392754/uniapp-tools/tree/master/lazyLoad
dcloud: https://ext.dcloud.net.cn/plugin?id=495


3.全局参数(setConfig)

参数列表

参数 类型 必填 默认值 说明
loading String 加载完成之前的替代图片
error String 加载错误的替代图片
preLoadHeight Number 0 预加载图片。提前加载图片距离scroll-view可视距离的差值,单位为px

demo

  1. import lazyLoad from "@/utils/lazyLoad.js";
  2. lazyLoad.setConfig({
  3. loading: "/static/loading.png", // 加载完成之前的替代图片
  4. error: "/static/error.png", // 加载错误的替代图片,
  5. preLoadHeight: 100 // 预加载图片
  6. });

4.完整demo

  1. <template>
  2. <view class='index-page'>
  3. <scroll-view id="scroll"
  4. :scroll-y="true"
  5. @scroll="scroll">
  6. <v-lazyLoad src="/static/img1.jpg"
  7. mode="widthFix"></v-lazyLoad>
  8. <v-lazyLoad src="/static/img2.jpg"
  9. mode="widthFix"></v-lazyLoad>
  10. <v-lazyLoad src="/static/img3.jpg"
  11. mode="widthFix"></v-lazyLoad>
  12. <v-lazyLoad src="/static/img4.jpg"
  13. mode="widthFix"></v-lazyLoad>
  14. <v-lazyLoad src="/static/img5.jpg"
  15. mode="widthFix"></v-lazyLoad>
  16. </scroll-view>
  17. </view>
  18. </template>
  19. <script>
  20. import lazyLoad from "../../utils/lazyLoad"
  21. import VLazyLoad from "../../components/lazyLoad"
  22. export default {
  23. methods: {
  24. scroll (e) {
  25. lazyLoad.scroll(); // 监听滚动条事件
  26. }
  27. },
  28. mounted () {
  29. lazyLoad.init("#scroll"); // 填写scroll-view标签id,初始化插件
  30. },
  31. components: {
  32. VLazyLoad
  33. }
  34. }
  35. </script>
  36. <style>
  37. #scroll {
  38. height: 700px; /* 需要给scroll-view设置高度 */
  39. }
  40. .index-page >>> .img-lazyLoad image {
  41. width: 100px;
  42. }
  43. </style>