Copied!
Guide

How to hide the header on scroll down and show it on scroll up

A header that gets out of the way while you read and comes back the moment you scroll up is one class toggled by scroll direction. Here is the code, with the small details that stop it flickering.

← Blog
Title card: Hide-on-scroll header. Preview of the Wiggle drop header element from the Enrichr Library.

December 4, 2026

3 min readAbout 15 lines of code

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.

Try it on any site. The free animation library checker shows you which animation libraries actually run on a page (GSAP, Lenis, Framer Motion). Paste a URL, no sign-up for your first lookup.

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?

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:

Where to go next

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.