We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b6afe6b commit 62c8abfCopy full SHA for 62c8abf
src/scrollToTop.ts
@@ -1,10 +1,15 @@
1
+import { animationFrameWrapper } from './animationFrameWrapper'
2
export function scrollToTop() {
3
try {
- const t = document.documentElement.scrollTop || document.body.scrollTop
4
- if (t > 0) {
5
- window.requestAnimationFrame(scrollToTop)
6
- window.scrollTo(0, t - t / 8)
7
- }
+ let pre: number
+ const stop = animationFrameWrapper(() => {
+ const t = document.documentElement.scrollTop || document.body.scrollTop
+ if (pre === undefined)
8
+ pre = t
9
+ if (pre < t || t === 0)
10
+ stop()
11
+ window.scrollTo(0, pre = t - t / 8)
12
+ }, 0)
13
}
14
catch (error: any) {
15
throw new Error(error)
0 commit comments