From fe83d46e84b728bd374b4adddf1e1426c813e078 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 13 Apr 2021 18:41:07 +0800 Subject: ... --- .../utilities/useReverseScrollPositionRemember.ts | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts') diff --git a/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts b/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts index 8a2bd7a0..67c7c458 100644 --- a/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts +++ b/FrontEnd/src/app/utilities/useReverseScrollPositionRemember.ts @@ -7,19 +7,31 @@ export default function useReverseScrollPositionRemember(): void { if (on) return; on = true; - let scrollPosition = - document.documentElement.scrollHeight - - document.documentElement.scrollTop; - + function getScrollPosition(): number { + if (document.documentElement.scrollHeight <= window.innerHeight) { + return 0; + } else { + return ( + document.documentElement.scrollHeight - + document.documentElement.scrollTop - + window.innerHeight + ); + } + } + + let scrollPosition = getScrollPosition(); const scrollListener = (): void => { - scrollPosition = document.documentElement.scrollHeight - window.scrollY; + scrollPosition = getScrollPosition(); }; window.addEventListener("scroll", scrollListener); const resizeObserver = new ResizeObserver(() => { + if (document.documentElement.scrollHeight <= window.innerHeight) return; document.documentElement.scrollTop = - document.documentElement.scrollHeight - scrollPosition; + document.documentElement.scrollHeight - + window.innerHeight - + scrollPosition; }); resizeObserver.observe(document.documentElement); -- cgit v1.2.3