diff options
author | crupest <crupest@outlook.com> | 2022-04-24 14:20:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-04-24 14:20:52 +0800 |
commit | d8adfa6b141c8e3a8f11592b831c574dee8602b9 (patch) | |
tree | 55c4457642f247d09d8d2af0d2e4ec82fa63258f /FrontEnd | |
parent | 392bd22adb1dcceeb14724d1e93ca44f9d1656a1 (diff) | |
download | timeline-d8adfa6b141c8e3a8f11592b831c574dee8602b9.tar.gz timeline-d8adfa6b141c8e3a8f11592b831c574dee8602b9.tar.bz2 timeline-d8adfa6b141c8e3a8f11592b831c574dee8602b9.zip |
...
Diffstat (limited to 'FrontEnd')
-rw-r--r-- | FrontEnd/src/utilities/useReverseScrollPositionRemember.ts | 77 | ||||
-rw-r--r-- | FrontEnd/src/utilities/useScrollToTop.ts | 2 |
2 files changed, 1 insertions, 78 deletions
diff --git a/FrontEnd/src/utilities/useReverseScrollPositionRemember.ts b/FrontEnd/src/utilities/useReverseScrollPositionRemember.ts deleted file mode 100644 index a5812808..00000000 --- a/FrontEnd/src/utilities/useReverseScrollPositionRemember.ts +++ /dev/null @@ -1,77 +0,0 @@ -import React from "react"; - -let on = false; - -let reverseScrollPosition = getReverseScrollPosition(); -let reverseScrollToPosition: number | null = null; -let lastScrollPosition = window.scrollY; - -export function getReverseScrollPosition(): number { - if (document.documentElement.scrollHeight <= window.innerHeight) { - return 0; - } else { - return ( - document.documentElement.scrollHeight - - document.documentElement.scrollTop - - window.innerHeight - ); - } -} - -export function scrollToReverseScrollPosition(reversePosition: number): void { - if (document.documentElement.scrollHeight <= window.innerHeight) return; - - const old = document.documentElement.style.scrollBehavior; - document.documentElement.style.scrollBehavior = "auto"; - - const newPosition = - document.documentElement.scrollHeight - - window.innerHeight - - reversePosition; - - reverseScrollToPosition = newPosition; - - window.scrollTo(0, newPosition); - - document.documentElement.style.scrollBehavior = old; -} - -const scrollListener = (): void => { - if ( - reverseScrollToPosition != null && - Math.abs(window.scrollY - reverseScrollToPosition) > 50 - ) { - scrollToReverseScrollPosition(reverseScrollPosition); - return; - } - if ( - reverseScrollToPosition == null && - Math.abs(window.scrollY - lastScrollPosition) > 1000 - ) { - scrollToReverseScrollPosition(reverseScrollPosition); - return; - } - - reverseScrollToPosition = null; - lastScrollPosition = window.scrollY; - reverseScrollPosition = getReverseScrollPosition(); -}; - -const resizeObserver = new ResizeObserver(() => { - scrollToReverseScrollPosition(reverseScrollPosition); -}); - -export default function useReverseScrollPositionRemember(): void { - React.useEffect(() => { - if (on) return; - on = true; - window.addEventListener("scroll", scrollListener); - resizeObserver.observe(document.documentElement); - - return () => { - window.removeEventListener("scroll", scrollListener); - resizeObserver.disconnect(); - on = false; - }; - }, []); -} diff --git a/FrontEnd/src/utilities/useScrollToTop.ts b/FrontEnd/src/utilities/useScrollToTop.ts index 892e3545..95c8b7b9 100644 --- a/FrontEnd/src/utilities/useScrollToTop.ts +++ b/FrontEnd/src/utilities/useScrollToTop.ts @@ -6,7 +6,7 @@ function useScrollToTop( handler: () => void, enable = true, option = { - maxOffset: 50, + maxOffset: 5, throttle: 1000, } ): void { |