Skip to content

Commit cbd009e

Browse files
committed
refactor: 精简代码
1 parent 566cd4d commit cbd009e

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

src/BootstrapBlazor/Components/Scroll/Scroll.razor.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,5 @@ public partial class Scroll
6363
/// <summary>
6464
/// 滚动到底部
6565
/// </summary>
66-
public async Task ScrollToBottom()
67-
{
68-
await InvokeVoidAsync("scrollToBottom", Id);
69-
}
66+
public Task ScrollToBottom() => InvokeVoidAsync("scrollToBottom", Id);
7067
}
Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
export function scrollToBottom(id) {
2-
let el = document.getElementById(id);
3-
const start = el.scrollTop;
4-
const end = el.scrollHeight;
5-
const distance = end - start;
6-
const duration = 500; // 动画持续时间(毫秒)
7-
const startTime = performance.now();
8-
9-
function scrollStep(currentTime) {
10-
const elapsed = currentTime - startTime;
11-
const progress = Math.min(elapsed / duration, 1); // 确保进度不超过1
12-
el.scrollTop = start + distance * progress;
13-
14-
if (progress < 1) {
15-
window.requestAnimationFrame(scrollStep);
16-
}
17-
}
18-
19-
window.requestAnimationFrame(scrollStep);
20-
}
21-
22-
export function init(id) {
1+
export function init(id) {
232
const el = document.getElementById(id)
243
if (el === null) {
254

265
}
276
}
7+
8+
export function scrollToBottom(id) {
9+
const el = document.getElementById(id);
10+
const top = el.scrollHeight;
11+
el.scrollTo({
12+
top,
13+
left: 0,
14+
behavior: "smooth"
15+
});
16+
}

0 commit comments

Comments
 (0)