-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualScroll.vue
83 lines (79 loc) · 1.71 KB
/
virtualScroll.vue
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
<template>
<div class="list-view" @scroll="handleScroll">
<!-- 撐虛擬高 -->
<div :style="{
height: virtualHeight + 'px'
}" class="list-view-phantom"></div>
<!-- 內容物 -->
<div class="list-view-content">
<slot :data="virtuallist"></slot>
</div>
</div>
</template>
<script>
export default {
name: "virtualScroll",
props: {
data: {
type: Array,
required: true
},
minHeight: {
type: Number,
required: true
},
minCount: {
type: Number,
required: true
}
},
data() {
return {
virtuallist: this.data,
virtualHeight: this.data.length * this.minHeight
};
},
computed: {},
created() {
this.virtuallist = this.data.slice(0, this.minCount);
},
mounted() {
//綁上設定的高
for (let i = 0; i < this.$el.lastChild.children.length; i++) {
this.$el.lastChild.children[i].style.height = this.minHeight + "px";
}
},
methods: {
handleScroll() {
const scrollTop = this.$el.scrollTop;
let start = Math.ceil(scrollTop / this.minHeight);
let end = Math.ceil(
(this.minHeight * this.minCount + scrollTop) / this.minHeight
);
this.virtuallist = this.data.slice(start, end);
this.$el.lastChild.style.webkitTransform = `translate3d(0, ${start *
this.minHeight}px, 0)`;
}
}
};
</script>
<style>
.list-view {
min-height: 160px;
overflow: auto;
position: relative;
}
.list-view-phantom {
position: absolute;
left: 0;
top: 0;
right: 0;
z-index: -1;
}
.list-view-content {
left: 0;
right: 0;
top: 0;
position: absolute;
}
</style>