All effectsScroll & reveal

Hide-on-Scroll Header

下滑隐藏上滑显示导航

The header retreats when you scroll down and returns when you reverse direction.

Scroll down, then back up

Where to use it

Give a narrow reading viewport more usable space while keeping navigation close. Use it on content-heavy pages with a header that can reappear as soon as people reverse direction. Keep the header visible while it contains keyboard focus.

Practical example

In a recipe reader on a phone, the navigation retreats as the cook scrolls through ingredients. A small upward scroll brings the Save recipe action back within reach.

AI prompt

Add Hide-on-Scroll Header to [target]. Compare consecutive scrollTop values with a small dead zone. Hide the pinned header on downward scroll and restore it at the top, on up-scroll, or whenever it receives focus. Keep the content readable in reduced-motion mode and preserve keyboard navigation.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Hide-on-Scroll Header</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .scroll-panel {
      position: relative;
      width: min(100%, 215px);
      height: 133px;
      overflow: auto;
      overscroll-behavior: contain;
      scrollbar-width: thin;
      scrollbar-color: #9881cb #2e2540;
      border: 1px solid #806ca8;
      border-radius: 9px;
      background: #2d2342;
      color: #f5efff;
      font: 650 12px system-ui, sans-serif;
    }
    .scroll-panel p { margin: 0; }
    .scroll-panel hr { border: 0; margin: 6px 0; height: 5px; border-radius: 9px; background: #ae96dc4a; }
    .scroll-panel hr:nth-of-type(2n) { width: 72%; }
    .scroll-note { color: #ddcff1; text-align: center; font-size: 10px; }
    .scroll-panel:focus-visible { outline-offset: 2px; }
    @media (prefers-reduced-motion: reduce) {
      .scroll-panel, .scroll-panel *, .scroll-panel *::before, .scroll-panel *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }

    .reading-section { min-height: 110px; padding: 8px 12px; }
    .reading-section h3 { color: #f3eaff; margin: 0 0 8px; font-size: 14px; font-weight: 720; }

    .floating-header {
      position: sticky; z-index: 1; top: 0; background: #694d8b; text-align: center;
      padding: 7px; transition: transform .22s ease;
    }
    .scroll-panel[data-hide] .floating-header { transform: translateY(-100%); }
  </style>
</head>
<body>
  <main class="demo">
    <div class="scroll-panel" role="region" tabindex="0" aria-label="Hide-on-scroll header demo">
    <header class="floating-header">Guide navigation</header>
    <section class="reading-section"><h3>Color</h3><hr><hr><hr><hr><hr><hr></section>
    <section class="reading-section"><h3>Type</h3><hr><hr><hr><hr><hr><hr></section>
    <section class="reading-section"><h3>Layout</h3><hr><hr><hr><hr><hr><hr></section>
    </div>
  </main>
  <script>
    const panel = document.querySelector('.scroll-panel');
    let previous = 0;
    panel.addEventListener('scroll', () => {
      const now = Math.max(0, panel.scrollTop);
      if (now < 4) panel.removeAttribute('data-hide');
      else if (Math.abs(now - previous) > 3) panel.toggleAttribute('data-hide', now > previous);
      previous = now;
    }, { passive: true });
  </script>
</body>
</html>

Original example by UI Skills