December 4, 2026
To hide a header when the visitor scrolls down and show it again when they scroll up, compare the scroll position with its previous value on each scroll, and toggle a class that slides the header out of view with a transform. It is about fifteen lines of JavaScript and four of CSS.
What is the code?
.site-header {
position: sticky; top: 0; z-index: 10;
transition: transform 0.3s ease;
}
.site-header.is-hidden { transform: translateY(-100%); }
html { scroll-padding-top: 5rem; } /* room for the header on jump links */
@media (prefers-reduced-motion: reduce) {
.site-header { transition: none; }
}
const header = document.querySelector(".site-header");
const THRESHOLD = 8; // ignore tiny movements
let last = scrollY, queued = false;
function update() {
queued = false;
const y = Math.max(0, scrollY); // iOS can report negatives
if (Math.abs(y - last) < THRESHOLD) return;
const goingDown = y > last;
header.classList.toggle("is-hidden", goingDown && y > header.offsetHeight);
last = y;
}
addEventListener("scroll", () => {
if (!queued) { queued = true; requestAnimationFrame(update); }
}, { passive: true });
// never hide the header from someone tabbing through it
header.addEventListener("focusin", () => header.classList.remove("is-hidden"));
Which details matter?
- The threshold. Without it, a resting finger on a trackpad makes the header twitch in and out.
- The top of the page. The check
y > header.offsetHeightkeeps the header in place until the visitor has scrolled past it. Hiding it at once looks like a glitch. - Negative scroll. When iOS bounces past the top of the page,
scrollYgoes below zero. Clamp it, or the bounce counts as scrolling up and then down. - Focus. A keyboard user who tabs to a link in a hidden header would be typing blind. The
focusinlistener brings it back. - Open menus. If the header holds an open dropdown or mobile menu, skip the hide while it is open.
Why a transform and not top?
Animating top or height makes the browser lay the page out again on every frame. A transform moves
the finished picture of the header, which is far cheaper, and the header keeps its place in the layout so nothing below it
jumps.
How do I add a shadow only after scrolling?
header.classList.toggle("is-stuck", y > 0);
Add that line to update() before the threshold check, and style .is-stuck with a shadow or a solid
background. The header then sits flat at the top of the page and lifts off once content passes beneath it.
Which finished example can I copy?
Wiggle drop header in the free Enrichr Library is a site header that slides away as you scroll down and drops back in with an elastic wobble along its bottom edge, drawn in SVG. It has live controls, the code to copy and a pen on CodePen. For the menu inside the header, see:
- Island menu: a round menu button that stretches into a wide pill.
- Expanding rounded menu: a round button that grows into a full panel.
Where to go next
- Scroll animations without a library.
- Clip-path reveal animations.
- What to do with animations when a visitor asks for reduced motion.
FAQ
How do I detect scroll direction in JavaScript?
Keep the last value of window.scrollY. On each scroll, if the new value is larger the page moved down, and if it is smaller it moved up. Then store the new value.
Why does my header flicker when I scroll?
Small scroll movements, such as trackpad jitter or the rubber-band effect on iOS, flip the direction many times a second. Add a threshold so changes under about 8 pixels are ignored.
Should I use position sticky or fixed for the header?
Either works with this pattern. position: sticky keeps the header in the page flow, so you do not need to add padding to the top of the page to make room for it.
How do I stop anchor links from hiding under the header?
Set scroll-padding-top on the html element to the height of the header. Jump links then stop with that much space above the target.