-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfinite-Scroll.html
155 lines (154 loc) · 5.88 KB
/
Infinite-Scroll.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite-Scroll</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<style>
#app {
position: relative;
height: 51dvh;
background-color: aquamarine;
overflow-x: hidden;
overflow-y: auto;
}
.phantom {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.list {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.item {
display: flex;
align-items: center;
border: 1px solid red;
padding: 10px;
}
/* .item:not(:first-child) {
margin-top: 10px;
} */
</style>
</head>
<body>
<div id="app" ref="scrollContainer" @scroll="scrollHandler">
<div class="phantom" :style="{ height: `${listHeight}px` }"></div>
<div class="list" :style="{ transform: `translate3d(0, ${startOffset}px, 0)` }">
<div v-for="(item, index) in viewList"
:class="[`item-${item}`, 'item']"
:index="item"
:key="item">
<div>
<img src="https://pic4.zhimg.com/80/v2-a78e25d5f2d1012f1631fa0b5f434f9f_1440w.webp" width="100" alt="">
</div>
<div style="margin-left:10px;">No.{{ item }}: {{ item % 2 ? 'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aliquam facilis eius, officiis, fugit illo illum optio ipsum ipsam enim nisi porro consequuntur aliquid nostrum natus veniam repellendus eos, doloribus autem! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aliquam facilis eius, officiis, fugit illo illum optio ipsum ipsam enim nisi porro consequuntur aliquid nostrum natus veniam repellendus eos, doloribus autem! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aliquam facilis eius, officiis, fugit illo illum optio ipsum ipsam enim nisi porro consequuntur aliquid nostrum natus veniam repellendus eos, doloribus autem!' : 'aaaaaaaaaa' }}</div>
</div>
</div>
</div>
<script>
const SIZE = 96
const originData = new Array(100000).fill(0).map((val, key) => key)
const app = new Vue({
el: '#app',
data: {
list: originData,
sizes: originData.map(val => ({ height: SIZE })),
// internal
startIndex: 0,
endIndex: 0,
startOffset: 0,
scrollContainerHeight: 0,
resizeObserver: null,
},
computed: {
viewList: vm => vm.list.slice(vm.startIndex, vm.endIndex + 1),
listHeight: vm => vm.sizes.reduce((accu, curr) => accu + curr.height, 0),
},
methods: {
initScrollContainerHeight () {
this.scrollContainerHeight = this.$refs.scrollContainer.offsetHeight
},
scrollHandler () {
const oldStartIndex = this.startIndex
const oldEndIndex = this.endIndex
const { startIndex, startHeight } = this.getStartInfo()
const { endIndex, endHeight } = this.getEndInfo(startIndex, startHeight)
this.startIndex = startIndex
this.endIndex = endIndex
// 产生变化才更新渲染相关的状态
if (oldStartIndex !== startIndex || oldEndIndex !== endIndex) {
this.startOffset = startHeight
this.unobserve(oldStartIndex, oldEndIndex)
this.$nextTick(() => {
this.observe(startIndex, endIndex)
})
}
},
getStartInfo () {
const scrollTop = this.$refs.scrollContainer.scrollTop
const scrollTopWithBuffer = Math.max(scrollTop - this.scrollContainerHeight, 0)
let totalHeight = 0, i = 0
for (; i < this.sizes.length; i++) {
totalHeight += this.sizes[i].height
if (totalHeight > scrollTopWithBuffer) return { startIndex: i, startHeight: totalHeight - this.sizes[i].height }
}
return { startIndex: i, startHeight: totalHeight }
},
getEndInfo (startIndex, startHeight) {
const scrollTop = this.$refs.scrollContainer.scrollTop
const endPositionWithBuffer = scrollTop + this.scrollContainerHeight * 2
let totalHeight = startHeight, i = startIndex
for (; i < this.sizes.length; i++) {
totalHeight += this.sizes[i].height
if (totalHeight >= endPositionWithBuffer) return { endIndex: i, endHeight: totalHeight }
}
return { endIndex: i, endHeight: totalHeight }
},
observe (startIndex, endIndex) {
if (!this.resizeObserver) return
for (let i = startIndex; i < endIndex; i++) {
const el = document.querySelector(`.item-${i}`)
this.resizeObserver.observe(el)
}
},
unobserve (oldStartIndex, oldEndIndex) {
if (!this.resizeObserver) return
for (let i = oldStartIndex; i < oldEndIndex; i++) {
const el = document.querySelector(`.item-${i}`)
this.resizeObserver.unobserve(el)
}
},
initResizeObserver () {
if (!this.resizeObserver) {
this.resizeObserver = new ResizeObserver(entries => {
requestAnimationFrame(() => {
if (!Array.isArray(entries)) return
for (let i = 0; i < entries.length; i++) {
const el = entries[i].target
if (el && el.offsetHeight) {
const elIndex = el.getAttribute('index')
this.sizes[elIndex].height = el.offsetHeight
// console.log('elIndex: ', elIndex, 'offsetHeight: ', el.offsetHeight)
}
}
})
})
}
},
},
mounted () {
this.initResizeObserver()
this.initScrollContainerHeight()
this.scrollHandler()
},
})
</script>
</body>
</html>