This snippet lets you control your site’s header visibility for a smooth browsing experience. The header hides when scrolling down and reappears when scrolling up, with customizable thresholds. It can also auto-hide when the footer comes into view, keeping navigation accessible without taking up space.
Add this code with a snippets plugin right before the closing </body> tag.
document.addEventListener('DOMContentLoaded', () => {
const header = document.querySelector('[ou-header]');
const footer = document.querySelector('[ou-footer]');
if (!header) return;
const HIDE_ON_SCROLL = true; // hide on scroll down, show on scroll up
const FOOTER_HIDES = true; // hide when footer enters viewport
const THRESHOLD_PCT = 5; // % of page height before header can hide
const REVEAL_UP_PCT = 5; // % of viewport height to scroll up before showing again
let lastY = window.scrollY;
let minScrollY = window.scrollY;
let footerVisible = false;
let headerHidden = false;
const getThresholds = () => ({
threshold: document.body.scrollHeight * (THRESHOLD_PCT / 100),
revealUp: window.innerHeight * (REVEAL_UP_PCT / 100)
});
let { threshold, revealUp } = getThresholds();
const toggleHeader = hide => {
if (hide === headerHidden) return;
headerHidden = hide;
header.classList.toggle('is-hidden', hide);
};
const update = () => {
const y = window.scrollY;
const goingDown = y > lastY;
if (!HIDE_ON_SCROLL) {
if (FOOTER_HIDES && footerVisible) toggleHeader(true);
else toggleHeader(false);
lastY = y;
return;
}
if (FOOTER_HIDES && footerVisible) {
toggleHeader(true);
lastY = y;
return;
}
if (goingDown) {
if (y > threshold) {
toggleHeader(true);
minScrollY = y;
}
} else {
if (minScrollY - y > revealUp || y <= threshold) toggleHeader(false);
}
lastY = y;
};
if (footer) {
const io = new IntersectionObserver(entries => {
footerVisible = entries.some(e => e.isIntersecting);
update();
}, { threshold: 0.01 });
io.observe(footer);
}
window.addEventListener('resize', () => {
({ threshold, revealUp } = getThresholds());
update();
}, { passive: true });
window.addEventListener('scroll', update, { passive: true });
update();
});
Add this code with a snippets plugin in <head>, or place it in Page CSS block or Site Settings. This CSS controls the fade-and-hide effect for the sticky header.
[ou-header] {
opacity: 1;
transition: opacity 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
[ou-header].is-hidden {
opacity: 0;
pointer-events: none;
}
Some solutions only work on the live site. Always publish and test after each change, as results may not appear in the editor.