-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVirtualScroller.js
176 lines (159 loc) · 5.58 KB
/
VirtualScroller.js
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
function throttle (fn, wait) {
let lastTime = 0;
let timer;
return function (...args) {
function run () {
const now = new Date().valueOf();
if (now - lastTime > wait) {
fn.apply(this, args);
lastTime = now;
}
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(run, wait);
run();
}
}
class VirtualScroller {
scroller = null; // 监控的dom元素
data = []; // 展示的数据
contentBox = null; // 容器dom
#scrollTop = 0;// 滚动的距离
#topHiddenCount = 0;// 上面隐藏的个数
#paddingTop = 0; //contentBox paddingTop设置参数,保证滚动条正常
#lastVisibleItemIndex = 0;//最后一个可视dom下标
#bottomHiddenCount = 0;
#paddingBottom = 0;
constructor({
element,
height,
rowHeight,
pageSize,
buffer,
renderItem,
loadMore
}) {
if (typeof element === 'string') {
this.scroller = document.querySelector(element);
} else if (element instanceof HTMLElement) {
this.scroller = element;
}
if (!this.scroller) {
throw new Error('Invalid element');
}
if (!height || (typeof height !== 'number' && typeof height !== 'string')) {
throw new Error('invalid height value');
}
if (!rowHeight || typeof rowHeight !== 'number') {
throw new Error('rowHeight should be a number');
}
if (typeof renderItem !== 'function') {
throw new Error('renderItem is not a function');
}
if (typeof loadMore !== 'function') {
throw new Error('renderItem is not a function');
}
// set props
this.height = height;
this.rowHeight = rowHeight;
this.pageSize = typeof pageSize === 'number' && pageSize > 0 ? pageSize : 50;
this.buffer = typeof buffer === 'number' && buffer >= 0 ? buffer : 10;
this.renderItem = renderItem;
this.loadMore = loadMore;
this.data = [];
// create content box
const contentBox = document.createElement('div');
this.contentBox = contentBox;
this.scroller.append(contentBox);
this.scroller.style.height = typeof height === 'number' ? height + 'px' : height;
this.#loadInitData();
this.scroller.addEventListener('scroll', throttle(this.#handleScroll, 150));
}
#loadInitData () {
const scrollerRect = this.scroller.getBoundingClientRect();
const minCount = Math.ceil(scrollerRect.height / this.rowHeight);
const page = Math.ceil(minCount / this.pageSize);
const newData = this.loadMore(page * this.pageSize);
this.data.push(...newData);
this.#renderNewData(newData);
}
#renderRow (item) {
const rowContent = this.renderItem(item);
const row = document.createElement('div');
row.dataset.index = item
row.style.height = this.rowHeight + 'px';
row.appendChild(rowContent)
return row;
}
#renderNewData (newData) {
newData.forEach(item => {
this.contentBox.append(this.#renderRow(item));
});
}
#handleScroll = (e) => {
const { clientHeight, scrollHeight, scrollTop } = e.target;
if (scrollHeight - (clientHeight + scrollHeight) < 40) {
// 到底加载更多
const newData = this.loadMore(this.pageSize);
this.data.push(...newData)
}
//记录当前的滚动距离,然后对比上一次保存的距离,知道了向上滚动还是向下滚动
const direction = scrollTop > this.#scrollTop ? 1 : -1
this.#toggleTopItems(direction)
this.#toggleBottomItems(direction)
this.#scrollTop = scrollTop;
}
//替换上面的dom
#toggleTopItems = (direction) => {
const { scrollTop } = this.scroller;
const firstVisibleItemIndex = Math.floor(scrollTop / this.rowHeight);
const firstExistingItemIndex = Math.max(0, firstVisibleItemIndex - this.buffer);
const rows = this.contentBox.children;
// replace invisible top items with padding top
if (direction === 1) {
for (let i = this.#topHiddenCount; i < firstExistingItemIndex; i++) {
if (rows[0]) rows[0].remove();
}
}
// restore hidden top items
if (direction === -1) {
for (let i = this.#topHiddenCount - 1; i >= firstExistingItemIndex; i--) {
const item = this.data[i];
const row = this.#renderRow(item);
this.contentBox.prepend(row);
}
}
this.#topHiddenCount = firstExistingItemIndex;
this.#paddingTop = this.#topHiddenCount * this.rowHeight;
this.contentBox.style.paddingTop = this.#paddingTop + 'px';
}
//替换下面的dom
#toggleBottomItems = (direction) => {
const { scrollTop, clientHeight } = this.scroller;
const lastVisibleItemIndex = Math.floor((scrollTop + clientHeight) / this.rowHeight);
const lastExistingItemIndex = lastVisibleItemIndex + this.buffer;
this.#lastVisibleItemIndex = lastVisibleItemIndex;
const rows = [...this.contentBox.children];
// replace invisible bottom items with padding bottom
if (direction === -1) {
for (let i = lastExistingItemIndex + 1; i <= this.data.length; i++) {
const row = rows[i - this.#topHiddenCount];
if (row) row.remove();
}
}
// restore hidden bottom items
if (direction === 1) {
for (let i = this.#topHiddenCount + rows.length; i <= lastExistingItemIndex; i++) {
const item = this.data[i];
if (!item) break;
const row = this.#renderRow(item);
this.contentBox.append(row);
}
}
this.#bottomHiddenCount = Math.max(0, this.data.length - (this.#topHiddenCount + this.contentBox.children.length) - this.buffer);
this.#paddingBottom = this.#bottomHiddenCount * this.rowHeight;
this.contentBox.style.paddingBottom = this.#paddingBottom + 'px';
}
}