Skip to content

Commit 8bbd3d2

Browse files
committed
feat: 优化
1 parent 2344222 commit 8bbd3d2

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

components/scrollbar/style/index.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
width: 100%;
1717
height: 100%;
1818
overflow: hidden;
19+
1920
.@{scrollbar-prefix-cls}-shadow-top {
2021
.shadow(@scrollbar-shadow-size, 'top')
2122
}
@@ -32,6 +33,11 @@
3233
width: 100%;
3334
height: 100%;
3435
overflow: auto;
36+
// 启用硬件加速和平滑滚动
37+
transform: translate3d(0, 0, 0);
38+
// 包含优化,减少重排重绘
39+
contain: layout style paint;
40+
will-change: scroll-position;
3541
}
3642
&-content {
3743
box-sizing: border-box;

components/virtual-list/virtual.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ export default class Virtual {
5050
private _lastScrollDirection: DIRECTION_TYPE | null = null;
5151
// 批量更新相关
5252
private _pendingUpdates: Set<string | number> = new Set();
53-
private _updateTimer: number | null = null;
54-
// 尺寸变化回调队列
55-
private _sizeChangeCallbacks: Array<() => void> = [];
53+
private _updateRAF: number | null = null;
54+
5655
constructor(param: VirtualParams, callUpdate: (range: Range) => void) {
5756
this.init(param, callUpdate);
5857
}
@@ -87,14 +86,13 @@ export default class Virtual {
8786
}
8887

8988
// 清除批量更新计时器
90-
if (this._updateTimer) {
91-
cancelAnimationFrame(this._updateTimer);
92-
this._updateTimer = null;
89+
if (this._updateRAF) {
90+
cancelAnimationFrame(this._updateRAF);
91+
this._updateRAF = null;
9392
}
9493

95-
// 清空批量更新队列和回调
94+
// 清空批量更新队列
9695
this._pendingUpdates.clear();
97-
this._sizeChangeCallbacks = [];
9896

9997
if (this.param && this.callUpdate) {
10098
this.checkRange(0, this.param.keeps - 1);
@@ -201,27 +199,18 @@ export default class Virtual {
201199

202200
// 批量更新调度器 - 避免频繁的单次更新
203201
private _scheduleUpdate() {
204-
if (this._updateTimer) {
202+
if (this._updateRAF) {
205203
return;
206204
}
207205

208-
this._updateTimer = requestAnimationFrame(() => {
206+
this._updateRAF = requestAnimationFrame(() => {
209207
if (this._pendingUpdates.size > 0) {
210208
// 批量处理所有待更新的项目
211209
const updatedIds = Array.from(this._pendingUpdates);
212210
this._batchInvalidateRelatedCaches(updatedIds);
213211
this._pendingUpdates.clear();
214-
215-
// 执行尺寸变化回调
216-
this._sizeChangeCallbacks.forEach((callback) => {
217-
try {
218-
callback();
219-
} catch (error) {
220-
console.warn('Size change callback error:', error);
221-
}
222-
});
223212
}
224-
this._updateTimer = null;
213+
this._updateRAF = null;
225214
});
226215
}
227216

@@ -289,17 +278,6 @@ export default class Virtual {
289278
}
290279
}
291280

292-
// 添加尺寸变化监听器
293-
onSizeChange(callback: () => void) {
294-
this._sizeChangeCallbacks.push(callback);
295-
return () => {
296-
const index = this._sizeChangeCallbacks.indexOf(callback);
297-
if (index > -1) {
298-
this._sizeChangeCallbacks.splice(index, 1);
299-
}
300-
};
301-
}
302-
303281
// 简化的尺寸缓存清理 - 优先保留视口附近的尺寸信息
304282
private _cleanupSizes() {
305283
if (this.sizes.size > this._maxCacheSize * 2) { // 降低触发阈值

components/virtual-list/virtualList.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,7 @@ export default defineComponent({
264264
if (oldSize !== size) {
265265
virtual.saveSize(id, size);
266266
emit(RESIZED_EVENT, id, size);
267-
268-
// 只有当尺寸变化较大时才更新滚动条,避免频繁更新
269-
const sizeChange = Math.abs((oldSize || 0) - size);
270-
if (sizeChange > 2) {
271-
updateScrollBar();
272-
}
267+
updateScrollBar();
273268
}
274269
};
275270

0 commit comments

Comments
 (0)