|
1 | 1 | <template> |
2 | | - <view> |
3 | | - <view class="uni-padding-wrap"> |
4 | | - <view class="uni-helllo-text" style="padding:30upx 0;"> |
5 | | - 延迟加载的理念:页面初始化时,暂不加载处于屏幕可见区域之外的图片。该方案会有如下几大好处: |
6 | | - <text>\n加快页面渲染速度</text> |
7 | | - <text>\n提升页面滚动性能</text> |
8 | | - <text>\n默认不下载屏幕外的图片,减少网络流量</text> |
9 | | - </view> |
10 | | - </view> |
11 | | - <view class="uni-list"> |
12 | | - <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in list" :key="index"> |
13 | | - <view class="uni-media-list"> |
14 | | - <view class="uni-media-list-logo"> |
15 | | - <image class="image" :class="{lazy:!item.show}" :data-index="index" @load="onLoad" :src="item.show?item.src:''" /> |
16 | | - <image class="image defaul" :class="{loaded:item.loaded}" :src="defaultSrc" /> |
17 | | - </view> |
18 | | - <view class="uni-media-list-body"> |
19 | | - <view class="uni-media-list-text-top">主标题</view> |
20 | | - <view class="uni-media-list-text-bottom uni-ellipsis">列表二级标题</view> |
21 | | - </view> |
22 | | - </view> |
23 | | - </view> |
24 | | - </view> |
25 | | - </view> |
| 2 | + <view> |
| 3 | + <view class="uni-padding-wrap"> |
| 4 | + <view class="uni-helllo-text" style="padding:30upx 0;"> |
| 5 | + 延迟加载的理念:页面初始化时,暂不加载处于屏幕可见区域之外的图片。该方案会有如下几大好处: |
| 6 | + <text>\n加快页面渲染速度</text> |
| 7 | + <text>\n提升页面滚动性能</text> |
| 8 | + <text>\n默认不下载屏幕外的图片,减少网络流量</text> |
| 9 | + </view> |
| 10 | + </view> |
| 11 | + <view class="uni-list"> |
| 12 | + <view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in list" :key="index"> |
| 13 | + <view class="uni-media-list"> |
| 14 | + <view class="uni-media-list-logo"> |
| 15 | + <image class="image" :class="{lazy:!item.show}" :data-index="index" @load="onLoad" :src="item.show?item.src:''" /> |
| 16 | + <image class="image placeholder" :class="{loaded:item.loaded}" :src="placeholderSrc" /> |
| 17 | + </view> |
| 18 | + <view class="uni-media-list-body"> |
| 19 | + <view class="uni-media-list-text-top">主标题</view> |
| 20 | + <view class="uni-media-list-text-bottom uni-ellipsis">列表二级标题</view> |
| 21 | + </view> |
| 22 | + </view> |
| 23 | + </view> |
| 24 | + </view> |
| 25 | + </view> |
26 | 26 | </template> |
27 | 27 |
|
28 | 28 | <script> |
29 | | - export default { |
30 | | - data() { |
31 | | - var imgs = ['shuijiao', 'muwu', 'cbd'] |
32 | | - var list = [] |
| 29 | + export default { |
| 30 | + data() { |
| 31 | + var imgs = ['shuijiao', 'muwu', 'cbd'] |
| 32 | + var list = [] |
33 | 33 |
|
34 | | - for (let i = 0; i < 20; i++) { |
35 | | - list.push({ |
36 | | - src: `https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/${imgs[i%3]}.jpg`, |
37 | | - show: false, |
38 | | - loaded: false |
39 | | - }) |
40 | | - } |
| 34 | + for (let i = 0; i < 20; i++) { |
| 35 | + list.push({ |
| 36 | + src: `https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/${imgs[i%3]}.jpg`, |
| 37 | + show: false, |
| 38 | + loaded: false |
| 39 | + }) |
| 40 | + } |
41 | 41 |
|
42 | | - return { |
43 | | - windowHeight: 0, |
44 | | - defaultSrc: '../../../static/60x60.png', |
45 | | - list: list, |
46 | | - show: false |
47 | | - } |
48 | | - }, |
49 | | - methods: { |
50 | | - load() { |
51 | | - uni.createSelectorQuery().selectAll('.lazy').boundingClientRect((images) => { |
52 | | - images.forEach((image, index) => { |
53 | | - if (image.top <= this.windowHeight) { |
54 | | - this.list[image.dataset.index].show = true |
55 | | - } |
56 | | - }) |
57 | | - }).exec() |
58 | | - }, |
59 | | - onLoad(e) { |
60 | | - this.list[e.target.dataset.index].loaded = true |
61 | | - } |
62 | | - }, |
63 | | - onLoad() { |
64 | | - this.windowHeight = uni.getSystemInfoSync().windowHeight |
65 | | - }, |
66 | | - onShow() { |
67 | | - if (!this.show) { |
68 | | - this.show = true |
69 | | - setTimeout(() => { |
70 | | - this.load() |
71 | | - }, 100) |
72 | | - } |
73 | | - }, |
74 | | - onPageScroll() { |
75 | | - this.load() |
76 | | - } |
77 | | - } |
| 42 | + return { |
| 43 | + windowHeight: 0, |
| 44 | + placeholderSrc: '../../../static/60x60.png', |
| 45 | + list: list, |
| 46 | + show: false |
| 47 | + } |
| 48 | + }, |
| 49 | + methods: { |
| 50 | + load() { |
| 51 | + console.log('load..............................'); |
| 52 | + console.log(uni.createSelectorQuery().selectAll('.lazy')); |
| 53 | + uni.createSelectorQuery().selectAll('.lazy').boundingClientRect((images) => { |
| 54 | + console.log('boundingClientRect...........'); |
| 55 | + images.forEach((image, index) => { |
| 56 | + if (image.top <= this.windowHeight) { |
| 57 | + this.list[image.dataset.index].show = true |
| 58 | + } |
| 59 | + }) |
| 60 | + }).exec() |
| 61 | + }, |
| 62 | + onLoad(e) { |
| 63 | + this.list[e.target.dataset.index].loaded = true |
| 64 | + } |
| 65 | + }, |
| 66 | + onLoad() { |
| 67 | + this.windowHeight = uni.getSystemInfoSync().windowHeight |
| 68 | + }, |
| 69 | + onShow() { |
| 70 | + if (!this.show) { |
| 71 | + this.show = true |
| 72 | + setTimeout(() => { |
| 73 | + this.load() |
| 74 | + }, 100) |
| 75 | + } |
| 76 | + }, |
| 77 | + onPageScroll() { |
| 78 | + console.log('scroll.............................'); |
| 79 | + this.load() |
| 80 | + } |
| 81 | + } |
78 | 82 | </script> |
79 | 83 |
|
80 | 84 | <style> |
81 | | - .defaul { |
82 | | - opacity: 1; |
83 | | - transition: opacity 0.4s linear; |
84 | | - } |
| 85 | + .placeholder { |
| 86 | + opacity: 1; |
| 87 | + transition: opacity 0.4s linear; |
| 88 | + } |
85 | 89 |
|
86 | | - .defaul.loaded { |
87 | | - opacity: 0; |
88 | | - } |
| 90 | + .placeholder.loaded { |
| 91 | + opacity: 0; |
| 92 | + } |
| 93 | +
|
| 94 | + .uni-media-list-logo { |
| 95 | + position: relative; |
| 96 | + } |
| 97 | +
|
| 98 | + .uni-media-list-logo .image { |
| 99 | + position: absolute; |
| 100 | + } |
89 | 101 | </style> |
0 commit comments