From 24ba1e44be7f9a33e08a5f237dafedd829d17313 Mon Sep 17 00:00:00 2001 From: nyne Date: Sat, 31 May 2025 16:38:45 +0800 Subject: [PATCH] Improve Floating To Top button visibility logic based on scroll direction --- frontend/src/components/navigator.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/navigator.tsx b/frontend/src/components/navigator.tsx index 7aff768..f7c8a63 100644 --- a/frontend/src/components/navigator.tsx +++ b/frontend/src/components/navigator.tsx @@ -276,12 +276,14 @@ function FloatingToTopButton() { const [visible, setVisible] = useState(false); useEffect(() => { + let lastScrollY = window.scrollY; + const handleScroll = () => { - if (window.scrollY > 200) { - setVisible(true); - } else { - setVisible(false); - } + const isScrollingUp = window.scrollY < lastScrollY; + const isAboveThreshold = window.scrollY > 200; + + setVisible(isScrollingUp && isAboveThreshold); + lastScrollY = window.scrollY; }; window.addEventListener("scroll", handleScroll); @@ -296,4 +298,4 @@ function FloatingToTopButton() { }}> ; -} +} \ No newline at end of file