mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 20:27:23 +00:00
Improve Floating To Top button visibility logic based on scroll direction
This commit is contained in:
@@ -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() {
|
||||
}}>
|
||||
<MdArrowUpward size={20}/>
|
||||
</button>;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user