Sticky Header
A top bar that steps aside while you read.
Settings
What it does
Sticky Header is a header animation ready for your AI assistant, React or plain JavaScript, with no dependencies. Also called hide on scroll, smart navbar or headroom.
Scroll down and the header slides up out of the way, leaving the whole screen to the page. Scroll up a little and it drops right back. Tabbing into it with the keyboard always brings it into view.
Documentation
One copy gives an assistant everything it needs: the complete code, every option and every pitfall. Paste it in Claude, Cursor or ChatGPT and say where the effect goes; it adapts the integration to your stack, not the code.
Copy the prompt
Ask for it
For instance: Add this sticky header effect to the hero of my home page.What it sends# Foundry44 · Sticky Header Header hides on scroll down and slides back on scroll up. Add this component to my project. Pick the setup below that matches my project and follow only that one. When it uses the code at the end, keep its logic as written (it is self-contained, with no dependencies) and adapt only the integration: JavaScript instead of TypeScript if the project has none, file names, imports, how it starts. ## Markup it expects The element marked `data-f44="sticky-header"`, with this structure inside. Texts, images and the number of items are the demo's: use mine. The `data-f44-*` attributes set options for the ready-made script only; with the code, pass the same options to `stickyHeader()` or as props. ```html <header data-f44="sticky-header"> <span>Foundry Studio</span> <nav> <a href="#">Gardens</a> <a href="#">Studio</a> <a href="#">Journal</a> <!-- as many more as you need, same structure --> </nav> <span>Book a walk</span> </header> ``` ## Options | Option | Attribute | Type | Default | Description | |---|---|---|---|---| | `tolerance` | `data-f44-tolerance` | `number` | `8` | Pixels scrolled in one direction before the header hides or comes back. Ignores small jitters. | | `offset` | `data-f44-offset` | `number` | `0` | Extra pixels below the header's own height within which it always shows. | | `duration` | `data-f44-duration` | `number` | `300` | Time in ms for the header to slide out. Coming back takes a little less, so it answers quickly. | ## Setup - **Plain HTML, no build step**: don't paste the code below. Load the ready-made script once, at the end of `<body>`: `<script src="https://foundry44.dev/demos/sticky-header/sticky-header.js" defer></script>` (or download that file and serve it with the page). It starts itself on every `data-f44="sticky-header"` element and reads the `data-f44-*` attributes above. - **React / Next.js with shadcn/ui** (the project has a `components.json`; otherwise, see the next line): `npx shadcn@latest add https://foundry44.dev/r/sticky-header.json` writes `lib/sticky-header-core.ts` and `components/ui/sticky-header.tsx`, then use `<StickyHeader />` with the options as props. - **React / Next.js from npm** (no shadcn/ui, or the user prefers a package): `npm install @foundry44/react` (or the project's package manager), then `import { StickyHeader } from "@foundry44/react"` with the options as props. With a bundler and no React: `@foundry44/core`, then `import { stickyHeader } from "@foundry44/core/sticky-header"`. - **React / Next.js by hand**: save the code below as `lib/sticky-header-core.ts` and import it from there (`@/lib/sticky-header-core` where that alias exists). Don't name it `sticky-header.ts` next to a `StickyHeader.tsx`: on macOS and Windows the two names collide. In a component marked `"use client"`, call `stickyHeader(ref.current, options)` in a `useEffect` and return `instance.destroy` as its cleanup; call `instance.update(options)` when options change. - **Any other stack**: create on mount with `stickyHeader(element, options)`, `update(options)` when they change, `destroy()` on unmount. Only the ready-made script finds `data-f44` elements and starts them by itself. Pasted code does not: call `stickyHeader(element)` on each element. ## Pitfalls - Respects prefers-reduced-motion: keep that behaviour. - A static element is made `position: sticky; top: 0`. Sticky only holds inside its parent: put the header directly in `<body>` (or a full-height wrapper), or make it `position: fixed` yourself. - Give the header a background and a `z-index`, otherwise the page shows through or slides over it. - It listens to the window's scroll. A header inside its own scrolling container is not handled. - Style the scrolled state with `[data-f44-scrolled]`, for example a shadow; it appears once the page has scrolled past the header. ## Code: `sticky-header-core.ts` ```ts // Copyright (c) 2026 Foundry44 · MIT License (SPDX-License-Identifier: MIT) /** * Foundry44 — sticky-header * A header that stays at the top of the page, slides up out of view while the * visitor scrolls down to read, and slides back as soon as they scroll up. * Near the top of the page it always shows. Once the page has scrolled past it, * the element carries `data-f44-scrolled`, a hook for a shadow or a background. * * Self-contained: no imports, so this file can be copied as-is. */ export interface StickyHeaderOptions { /** Pixels scrolled in one direction before the header hides or comes back. Ignores small jitters. @range 0 64 1 */ tolerance?: number; /** Extra pixels below the header's own height within which it always shows. @range 0 400 10 */ offset?: number; /** Time in ms for the header to slide out. Coming back takes a little less, so it answers quickly. @range 100 1000 25 */ duration?: number; } export interface StickyHeaderInstance { update(options: StickyHeaderOptions): void; destroy(): void; } export const stickyHeaderDefaults: Required<StickyHeaderOptions> = { tolerance: 8, offset: 0, duration: 300, }; const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)"; const SHOW_RATIO = 0.75; const REDUCED_FADE = 200; export function stickyHeader(el: HTMLElement, options: StickyHeaderOptions = {}): StickyHeaderInstance { let opts = { ...stickyHeaderDefaults, ...options }; if (typeof window === "undefined") return { update() {}, destroy() {} }; // Reduced motion: no sliding, the header fades out and back in place. const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true; const original = { position: el.style.position, top: el.style.top, transform: el.style.transform, opacity: el.style.opacity, transition: el.style.transition, }; const computed = getComputedStyle(el); if (computed.position === "static") { el.style.position = "sticky"; el.style.top = "0px"; } let hidden = false; let lastY = window.scrollY; let travelledDown = 0; let travelledUp = 0; const transition = () => { const hide = opts.duration; const show = Math.round(opts.duration * SHOW_RATIO); if (reduced) { // Move it out of the way only once it has faded, and back before it fades in. el.style.transition = hidden ? `opacity ${REDUCED_FADE}ms ease, transform 0s linear ${REDUCED_FADE}ms` : `opacity ${REDUCED_FADE}ms ease`; return; } el.style.transition = `transform ${hidden ? hide : show}ms ${EASE_OUT}`; }; const setHidden = (next: boolean) => { if (next === hidden) return; hidden = next; transition(); // 100% plus a few px so a box-shadow below the header leaves with it. el.style.transform = hidden ? "translateY(calc(-100% - 8px))" : original.transform; if (reduced) el.style.opacity = hidden ? "0" : original.opacity; }; const onScroll = () => { const y = window.scrollY; const delta = y - lastY; lastY = y; const alwaysVisible = el.offsetHeight + opts.offset; el.toggleAttribute("data-f44-scrolled", y > el.offsetHeight); if (y <= alwaysVisible) { travelledDown = travelledUp = 0; setHidden(false); return; } if (delta > 0) { travelledDown += delta; travelledUp = 0; } else if (delta < 0) { travelledUp -= delta; travelledDown = 0; } if (travelledDown > opts.tolerance) setHidden(true); else if (travelledUp > opts.tolerance) setHidden(false); }; // A keyboard user tabbing into a hidden header must be able to see where focus went. const onFocusIn = () => setHidden(false); window.addEventListener("scroll", onScroll, { passive: true }); el.addEventListener("focusin", onFocusIn); onScroll(); return { update(next) { opts = { ...opts, ...next }; // tolerance and offset are read on every scroll; duration needs the transition rewritten. transition(); }, destroy() { window.removeEventListener("scroll", onScroll); el.removeEventListener("focusin", onFocusIn); el.removeAttribute("data-f44-scrolled"); el.style.position = original.position; el.style.top = original.top; el.style.transform = original.transform; el.style.opacity = original.opacity; el.style.transition = original.transition; }, }; } ```
Two files that become yours: the engine, written without React, and a thin component around it. No package to install. The component is marked use client, so it sits inside a server component as it is.
Add it with the shadcn CLI
In a project set up for shadcn/ui, one command writes both files. Without it, copy them by hand in the two steps below.npx shadcn@latest add https://foundry44.dev/r/sticky-header.json
Or add the engine by hand
Save it aslib/sticky-header-core.ts. It has no dependencies and does nothing on the server.And the component
Save it ascomponents/ui/sticky-header.tsx. It creates the effect on mount, passes every prop change to it live, and cleans up on unmount.// Copyright (c) 2026 Foundry44 · MIT License (SPDX-License-Identifier: MIT) // Generated by scripts/generate.mjs. Do not edit: change the core component instead. "use client"; import { useEffect, useRef, type ComponentPropsWithoutRef } from "react"; import { stickyHeader, type StickyHeaderOptions, type StickyHeaderInstance } from "@/lib/sticky-header-core"; export interface StickyHeaderProps extends StickyHeaderOptions, Omit<ComponentPropsWithoutRef<"header">, keyof StickyHeaderOptions> {} export function StickyHeader({ tolerance, offset, duration, style, ...props }: StickyHeaderProps) { const ref = useRef<HTMLElementTagNameMap["header"]>(null); const instance = useRef<StickyHeaderInstance | null>(null); useEffect(() => { if (!ref.current) return; instance.current = stickyHeader(ref.current); return () => { instance.current?.destroy(); instance.current = null; }; }, []); // Runs right after creation, then on every option change. useEffect(() => { instance.current?.update(definedOnly({ tolerance, offset, duration })); }, [tolerance, offset, duration]); return <header ref={ref} style={style} {...props} />; } function definedOnly<T extends object>(o: T): T { return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)) as T; }
Or install the package
It is also on npm, in@foundry44/reactwith the other free components. Import it from there instead of@/components/ui.npm install @foundry44/react
import { StickyHeader } from "@foundry44/react";
Use it
Every prop is optional: with none, you get the effect as the preview shows it.import { StickyHeader } from "@/components/ui/sticky-header"; export function Example() { return ( <StickyHeader tolerance={8} offset={0} duration={300} > {/* your content */} </StickyHeader> ); }
PropsProp Default What it is tolerance 8 Pixels scrolled in one direction before the header hides or comes back. Ignores small jitters. offset 0 Extra pixels below the header's own height within which it always shows. duration 300 Time in ms for the header to slide out. Coming back takes a little less, so it answers quickly. Avoid the pitfalls
- It follows
prefers-reduced-motion: under reduced motion the effect settles at once. - A static element is made
position: sticky; top: 0. Sticky only holds inside its parent: put the header directly in<body>(or a full-height wrapper), or make itposition: fixedyourself. - Give the header a background and a
z-index, otherwise the page shows through or slides over it. - It listens to the window's scroll. A header inside its own scrolling container is not handled.
- Style the scrolled state with
[data-f44-scrolled], for example a shadow; it appears once the page has scrolled past the header.
- It follows
One file, readable, no dependencies and no build step. It sets itself up on every element marked data-f44="sticky-header", in plain HTML, WordPress, Framer embeds or any framework.
Load the script
Download it, put it next to your page and load it once, anywhere in the page.<script src="sticky-header.js" defer></script>
The file/*! * Foundry44 · Sticky Header — https://foundry44.dev/components/sticky-header * Header hides on scroll down and slides back on scroll up. * Copyright (c) 2026 Foundry44 · MIT License (SPDX-License-Identifier: MIT) * * Usage: load this file, then add data-f44="sticky-header" to any element. * Optional attributes (defaults shown): * data-f44-tolerance="8" * data-f44-offset="0" * data-f44-duration="300" * Manual: const instance = Foundry44.stickyHeader(element, { tolerance: 8, offset: 0, duration: 300 }); * instance.destroy(); * Re-scan the page after adding elements dynamically: Foundry44.init(); * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ (function () { "use strict"; /** * Foundry44 — sticky-header * A header that stays at the top of the page, slides up out of view while the * visitor scrolls down to read, and slides back as soon as they scroll up. * Near the top of the page it always shows. Once the page has scrolled past it, * the element carries `data-f44-scrolled`, a hook for a shadow or a background. * * Self-contained: no imports, so this file can be copied as-is. */ const stickyHeaderDefaults = { tolerance: 8, offset: 0, duration: 300, }; const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)"; const SHOW_RATIO = 0.75; const REDUCED_FADE = 200; function stickyHeader(el, options = {}) { var _a; let opts = { ...stickyHeaderDefaults, ...options }; if (typeof window === "undefined") return { update() {}, destroy() {} }; // Reduced motion: no sliding, the header fades out and back in place. const reduced = ((_a = window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, "(prefers-reduced-motion: reduce)").matches) === true; const original = { position: el.style.position, top: el.style.top, transform: el.style.transform, opacity: el.style.opacity, transition: el.style.transition, }; const computed = getComputedStyle(el); if (computed.position === "static") { el.style.position = "sticky"; el.style.top = "0px"; } let hidden = false; let lastY = window.scrollY; let travelledDown = 0; let travelledUp = 0; const transition = () => { const hide = opts.duration; const show = Math.round(opts.duration * SHOW_RATIO); if (reduced) { // Move it out of the way only once it has faded, and back before it fades in. el.style.transition = hidden ? `opacity ${REDUCED_FADE}ms ease, transform 0s linear ${REDUCED_FADE}ms` : `opacity ${REDUCED_FADE}ms ease`; return; } el.style.transition = `transform ${hidden ? hide : show}ms ${EASE_OUT}`; }; const setHidden = (next) => { if (next === hidden) return; hidden = next; transition(); // 100% plus a few px so a box-shadow below the header leaves with it. el.style.transform = hidden ? "translateY(calc(-100% - 8px))" : original.transform; if (reduced) el.style.opacity = hidden ? "0" : original.opacity; }; const onScroll = () => { const y = window.scrollY; const delta = y - lastY; lastY = y; const alwaysVisible = el.offsetHeight + opts.offset; el.toggleAttribute("data-f44-scrolled", y > el.offsetHeight); if (y <= alwaysVisible) { travelledDown = travelledUp = 0; setHidden(false); return; } if (delta > 0) { travelledDown += delta; travelledUp = 0; } else if (delta < 0) { travelledUp -= delta; travelledDown = 0; } if (travelledDown > opts.tolerance) setHidden(true); else if (travelledUp > opts.tolerance) setHidden(false); }; // A keyboard user tabbing into a hidden header must be able to see where focus went. const onFocusIn = () => setHidden(false); window.addEventListener("scroll", onScroll, { passive: true }); el.addEventListener("focusin", onFocusIn); onScroll(); return { update(next) { opts = { ...opts, ...next }; // tolerance and offset are read on every scroll; duration needs the transition rewritten. transition(); }, destroy() { window.removeEventListener("scroll", onScroll); el.removeEventListener("focusin", onFocusIn); el.removeAttribute("data-f44-scrolled"); el.style.position = original.position; el.style.top = original.top; el.style.transform = original.transform; el.style.opacity = original.opacity; el.style.transition = original.transition; }, }; } function parse(value) { if (value === "true") return true; if (value === "false") return false; var n = Number(value); return value !== "" && !isNaN(n) ? n : value; } function init() { document.querySelectorAll('[data-f44="sticky-header"]').forEach(function (el) { if (el.__f44) return; var options = {}; [ ["tolerance", "data-f44-tolerance"], ["offset", "data-f44-offset"], ["duration", "data-f44-duration"], ].forEach(function (pair) { var value = el.getAttribute(pair[1]); if (value !== null) options[pair[0]] = parse(value); }); el.__f44 = stickyHeader(el, options); }); } var Foundry44 = (window.Foundry44 = window.Foundry44 || {}); Foundry44.stickyHeader = stickyHeader; Foundry44._inits = Foundry44._inits || []; Foundry44._inits.push(init); Foundry44.init = function () { Foundry44._inits.forEach(function (fn) { fn(); }); }; if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init(); })();
Mark your element
Adddata-f44="sticky-header". Each setting is an attribute on the same element, and an attribute always wins over the default.<div data-f44="sticky-header" data-f44-tolerance="8"> <!-- your content --> </div>
SettingsAttribute Default What it is data-f44-tolerance 8 Pixels scrolled in one direction before the header hides or comes back. Ignores small jitters. data-f44-offset 0 Extra pixels below the header's own height within which it always shows. data-f44-duration 300 Time in ms for the header to slide out. Coming back takes a little less, so it answers quickly. Drive it from JavaScript
When the element arrives after the page has loaded, or when the settings come from your code.// an element without data-f44, or the script would set it up twice const element = document.querySelector(".my-element"); const instance = Foundry44.stickyHeader(element, { tolerance: 8, offset: 0 }); instance.update({ tolerance: 8 }); // any option, live instance.destroy(); // listeners and observers gone, inline styles restored // elements added after the page loaded Foundry44.init();
Avoid the pitfalls
- It follows
prefers-reduced-motion: under reduced motion the effect settles at once. - A static element is made
position: sticky; top: 0. Sticky only holds inside its parent: put the header directly in<body>(or a full-height wrapper), or make itposition: fixedyourself. - Give the header a background and a
z-index, otherwise the page shows through or slides over it. - It listens to the window's scroll. A header inside its own scrolling container is not handled.
- Style the scrolled state with
[data-f44-scrolled], for example a shadow; it appears once the page has scrolled past the header.
- It follows
For a site builder: Webflow, WordPress, Shopify, Framer, or any site that accepts HTML. One block carries the whole script; paste it, then mark an element.
Paste the embed
Paste it where your builder takes custom HTML: an embed or code block on the page, or the site's footer code to use the effect on every page.Mark the element
Give the element the attributedata-f44with the valuesticky-header. Most builders have a field for custom attributes; otherwise, add it to the element's HTML.SettingsEvery setting is an attribute too, on the same element. Leave one out and the default applies.Attribute Default What it is data-f44-tolerance 8 Pixels scrolled in one direction before the header hides or comes back. Ignores small jitters. data-f44-offset 0 Extra pixels below the header's own height within which it always shows. data-f44-duration 300 Time in ms for the header to slide out. Coming back takes a little less, so it answers quickly. Avoid the pitfalls
- It follows
prefers-reduced-motion: under reduced motion the effect settles at once. - A static element is made
position: sticky; top: 0. Sticky only holds inside its parent: put the header directly in<body>(or a full-height wrapper), or make itposition: fixedyourself. - Give the header a background and a
z-index, otherwise the page shows through or slides over it. - It listens to the window's scroll. A header inside its own scrolling container is not handled.
- Style the scrolled state with
[data-f44-scrolled], for example a shadow; it appears once the page has scrolled past the header.
- It follows