tui-virtual-list.vue
9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<template>
<view class="tui-virtual-list" :style="{background:background}">
<scroll-view :style="getStyle" class="tui-virtual__scroll-view" :scroll-y="!enablePageScroll"
:upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-top="innerScrollOffset"
:scroll-with-animation="scrollWithAnimation" :enable-back-to-top="enableBackToTop" @scroll="onScroll"
@scrolltoupper="onScrollToUpper" @scrolltolower="onScrollToLower">
<view :style="styles">
<slot></slot>
</view>
</scroll-view>
</view>
</template>
<script>
import {
mapVirtualToProps,
getVisibleItemBounds
} from './index.js'
export default {
name: "tui-virtual-list",
emits: ['change', 'scroll', 'scrolltoupper', 'scrolltolower', 'init'],
provide() {
return {
virtuallist: this
}
},
props: {
height: {
type: [Number, String],
default: 600
},
//rpx 或 px
unit: {
type: String,
default: 'rpx'
},
background: {
type: String,
default: 'transparent'
},
enablePageScroll: {
type: Boolean,
default: false
},
itemHeight: {
type: [Number, String],
default: 100
},
itemBuffer: {
type: [Number, String],
default: 0
},
currentIndex: {
type: [Number, String],
default: 0
},
upperThreshold: {
type: [Number, String],
default: 50
},
lowerThreshold: {
type: [Number, String],
default: 50
},
scrollWithAnimation: {
type: Boolean,
default: false
},
enableBackToTop: {
type: Boolean,
default: false
}
},
computed: {
getStyle() {
let style = ''
if (!this.enablePageScroll) {
const height = this.unit === 'rpx' ? this.getPx(this.height || 600) : this.height;
style += `height:${height}px;`
}
return style;
},
valueChange() {
return `${this.enablePageScroll?1:0}_${this.height}_${this.itemHeight}_${this.itemBuffer}`
}
},
watch: {
itemHeight(newVal) {
this.updated(newVal)
},
valueChange(val) {
this.scrollHeight = this.unit === 'rpx' ? this.getPx(this.height || 600) : this.height;
if (this.firstRendered) {
this.onChange(this.scrollOffset, true)
}
},
currentIndex(newVal) {
if (this.firstRendered) {
this.scrollToIndex(Number(newVal))
}
}
},
created() {
this.items = []
this.firstRendered = false
this.children = []
this.scrollHeight = this.unit === 'rpx' ? this.getPx(this.height || 600) : this.height;
this.cellHeight = this.getPx(this.itemHeight);
this.$emit('init', {
itemHeight: this.cellHeight
})
},
mounted() {
this.$nextTick(() => {
this.getBoundingClientRect()
this.loadData()
})
},
data() {
return {
styles: '',
offsetTop: undefined,
// 用于记录滚动条实际位置
scrollOffset: 0,
// 用于设置滚动条位置
innerScrollOffset: 0,
// 第一个元素的索引值
startIndex: 0,
// 最后一个元素的索引值
endIndex: -1,
//容器总高度 px
scrollHeight: 300,
//item项高度 px
cellHeight: 50
};
},
methods: {
getPx(rpx) {
let px = uni.upx2px(Number(rpx))
return px % 2 === 0 ? px : px + 1
},
getCellHeight() {
return this.getPx(this.itemHeight)
},
/**
* 设置子元素的高度
* @param {Number} itemHeight 子元素高度
*/
updated(itemHeight) {
this.cellHeight = this.getPx(itemHeight || this.itemHeight);
const elements = this.children || []
if (elements.length > 0) {
elements.forEach((element, index) => {
element.updated(index, this.cellHeight)
})
}
this.$emit('init', {
itemHeight: this.cellHeight
})
},
/**
* 用于计算虚拟列表数据
* @param {Function} callback 设置完成后的回调函数
*/
loadData(callback) {
const {
cellHeight,
startIndex,
endIndex,
scrollOffset
} = this
const options = {
items: this.items,
itemHeight: cellHeight,
}
const indexes = {
startIndex,
endIndex
}
const virtual = mapVirtualToProps(options, indexes)
this.styles = virtual.style
if (typeof callback === 'function') {
callback.call(this, {
items: virtual.items,
...indexes,
scrollOffset
})
}
},
/**
* 数据变化时的回调函数
* @param {Number} scrollOffset 记录滚动条实际位置
* @param {Boolean} scrolled 是否设置滚动条位置
* @param {Function} callback 设置完成后的回调函数
*/
onChange(scrollOffset, scrolled, callback) {
// 计算起始点是否发生变化
const {
cellHeight,
scrollHeight,
itemBuffer,
startIndex,
endIndex,
offsetTop,
enablePageScroll
} = this
const itemCount = Math.max(0, this.items.length - 1)
const listTop = enablePageScroll ? offsetTop : 0
const viewTop = scrollOffset - listTop
const state = getVisibleItemBounds(viewTop, scrollHeight, itemCount, cellHeight, Number(itemBuffer))
const hasChanged = state.startIndex !== startIndex || state.endIndex !== endIndex
// 计算起始点是否可视
const direction = scrollOffset > this.scrollOffset ? 'Down' : 'Up'
const firstItemVisible = direction === 'Up' && viewTop < startIndex * cellHeight
const lastItemVisible = direction === 'Down' && viewTop > (endIndex * cellHeight - scrollHeight)
// 判断起始点大小
if (state === undefined || state.startIndex > state.endIndex) return
// 判断起始点是否发生变化及是否可视状态
if (hasChanged && (firstItemVisible || lastItemVisible) || scrolled) {
this.startIndex = state.startIndex
this.endIndex = state.endIndex
this.loadData((values) => {
// scroll into view
if (scrolled) {
this.innerScrollOffset = scrollOffset
}
// trigger change
this.$emit('change', {
...values,
direction,
scrollOffset
})
// trigger callback
if (typeof callback === 'function') {
callback.call(this, {
...values,
direction,
scrollOffset
})
}
})
}
// 记录滚动条的位置(仅记录不去设置)
if (this.scrollOffset != scrollOffset) {
this.scrollOffset = scrollOffset
}
},
/**
* 滚动时触发的事件
*/
onScroll(e) {
this.onChange(e.detail.scrollTop)
this.$emit('scroll', e.detail)
},
/**
* 滚动到顶部时触发的事件
*/
onScrollToUpper(e) {
this.$emit('scrolltoupper', e.detail)
},
/**
* 滚动到底部时触发的事件
*/
onScrollToLower(e) {
this.$emit('scrolltolower', e.detail)
},
/**
* 根据索引值获取偏移量
* @param {Number} index 指定的索引值
* @param {Number} itemHeight 子元素高度
* @param {Number} itemSize 子元素个数
*/
getOffsetForIndex(index, itemHeight, itemSize) {
itemHeight = itemHeight || this.cellHeight
itemSize = itemSize || this.items.length
const realIndex = Math.max(0, Math.min(index, itemSize - 1))
const scrollOffset = realIndex * itemHeight
return scrollOffset
},
/**
* 更新组件
* @param {Array} items 实际数据列表,当需要动态加载数据时设置
* @param {Function} success 设置完成后的回调函数
*/
render(items, success) {
if (Array.isArray(items)) {
this.items = items
this.children = []
}
// 首次渲染时滚动至 scrollToIndex 指定的位置
if (!this.firstRendered) {
this.firstRendered = true
this.scrollOffset = this.getOffsetForIndex(this.currentIndex)
}
this.getBoundingClientRect(() => this.onChange(this.scrollOffset, true, success))
},
/**
* 滚动到指定的位置
* @param {Number} scrollOffset 指定的位置
* @param {Function} success 设置完成后的回调函数
*/
scrollTo(scrollOffset, success) {
if (typeof scrollOffset === 'number') {
const offset = Math.max(0, Math.min(scrollOffset, this.items.length * this.cellHeight))
this.onChange(offset, true, success)
}
},
/**
* 根据索引值滚动到指定的位置
* @param {Number} index 指定元素的索引值
* @param {Function} success 设置完成后的回调函数
*/
scrollToIndex(index, success) {
if (typeof index === 'number') {
this.onChange(this.getOffsetForIndex(index), true, success)
}
},
/**
* 获取容器的偏移量
* @param {Function} callback 设置完成后的回调函数
* @param {Boolean} isForce 是否强制更新
*/
getBoundingClientRect(callback, isForce) {
if (this.offsetTop !== undefined && !isForce && typeof callback === 'function') {
callback.call(this)
return
}
const className = '.tui-virtual-list'
uni.createSelectorQuery()
// #ifndef MP-ALIPAY
.in(this)
// #endif
.select(className)
.boundingClientRect((rect) => {
if (!rect) return
this.offsetTop = rect.top
callback && callback()
})
.exec()
}
}
}
</script>
<style scoped>
.tui-virtual-list {
position: relative;
/* #ifndef APP-NVUE */
width: 100%;
display: block;
overflow: auto;
height: auto;
/* #endif */
}
.tui-virtual__scroll-view {
/* #ifndef APP-NVUE */
width: 100%;
/* #endif */
}
</style>