2021-07-16 11:57:07 -05:00
|
|
|
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;
|
2021-07-16 12:39:41 -05:00
|
|
|
|
2021-07-16 11:57:07 -05:00
|
|
|
if (curr < threshold) {
|
|
|
|
show();
|
|
|
|
document.documentElement.classList.add('initial');
|
|
|
|
} else if (dir !== prevDir) {
|
|
|
|
if (dir === 1) {
|
|
|
|
hide();
|
|
|
|
} else {
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 12:39:41 -05:00
|
|
|
|
2021-07-16 11:57:07 -05:00
|
|
|
prev = curr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hide = () => {
|
2021-07-16 12:39:41 -05:00
|
|
|
nav.classList.add('hidden')
|
2021-07-16 11:57:07 -05:00
|
|
|
document.documentElement.classList.add('scrolled');
|
|
|
|
document.documentElement.classList.remove('initial');
|
|
|
|
};
|
|
|
|
const show = () => {
|
|
|
|
nav.classList.remove('hidden');
|
|
|
|
document.documentElement.classList.remove('scrolled');
|
2021-07-16 12:39:41 -05:00
|
|
|
}
|