Skip to content

Commit 62c8abf

Browse files
author
Simon he
committed
fix: scrollToTop interupt will be stoped
1 parent b6afe6b commit 62c8abf

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/scrollToTop.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { animationFrameWrapper } from './animationFrameWrapper'
12
export function scrollToTop() {
23
try {
3-
const t = document.documentElement.scrollTop || document.body.scrollTop
4-
if (t > 0) {
5-
window.requestAnimationFrame(scrollToTop)
6-
window.scrollTo(0, t - t / 8)
7-
}
4+
let pre: number
5+
const stop = animationFrameWrapper(() => {
6+
const t = document.documentElement.scrollTop || document.body.scrollTop
7+
if (pre === undefined)
8+
pre = t
9+
if (pre < t || t === 0)
10+
stop()
11+
window.scrollTo(0, pre = t - t / 8)
12+
}, 0)
813
}
914
catch (error: any) {
1015
throw new Error(error)

0 commit comments

Comments
 (0)