Skip to content

Commit

Permalink
Auto-hide page nav when scrolling down
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsherman committed Dec 5, 2024
1 parent e88c166 commit c8ce1bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function hookupPageNavAutoHide() {
const header = document.querySelector("body > nav");
const scrollThreshold = 10;

let lastScrollY = globalThis.scrollY;

globalThis.addEventListener("scroll", () => {
const currentScrollY = globalThis.scrollY;
const isScrollingDown =
currentScrollY > scrollThreshold && currentScrollY > lastScrollY;

if (isScrollingDown) {
header?.classList.add("hidden");
} else {
header?.classList.remove("hidden");
}

lastScrollY = currentScrollY;
});
}

hookupPageNavAutoHide();

0 comments on commit c8ce1bf

Please sign in to comment.