0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/docs/public/nav.js
2021-07-20 00:37:38 +00:00

37 lines
824 B
JavaScript

const nav = document.querySelector('body > header');
if (!window.matchMedia('(prefers-reduced-motion)').matches) {
window.addEventListener('scroll', onScroll, { passive: true });
}
let prev = -1;
let prevDir = 0;
let threshold = 32;
function onScroll() {
const curr = window.scrollY;
const dir = curr > prev ? 1 : -1;
if (curr < threshold) {
show();
document.documentElement.classList.add('initial');
} else if (dir !== prevDir) {
if (dir === 1) {
hide();
} else {
show();
}
}
prev = curr;
}
const hide = () => {
nav.classList.add('hidden');
document.documentElement.classList.add('scrolled');
document.documentElement.classList.remove('initial');
};
const show = () => {
nav.classList.remove('hidden');
document.documentElement.classList.remove('scrolled');
};