Improve Floating To Top button visibility logic based on scroll direction

This commit is contained in:
2025-05-31 16:38:45 +08:00
parent f5c14917f9
commit 24ba1e44be

View File

@@ -276,12 +276,14 @@ function FloatingToTopButton() {
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
useEffect(() => { useEffect(() => {
let lastScrollY = window.scrollY;
const handleScroll = () => { const handleScroll = () => {
if (window.scrollY > 200) { const isScrollingUp = window.scrollY < lastScrollY;
setVisible(true); const isAboveThreshold = window.scrollY > 200;
} else {
setVisible(false); setVisible(isScrollingUp && isAboveThreshold);
} lastScrollY = window.scrollY;
}; };
window.addEventListener("scroll", handleScroll); window.addEventListener("scroll", handleScroll);