Number 10: Header line on scroll
Specification
- Catalogue
- No. 10 of 110
- Section
- Navigation
- Interaction
- Scroll
- Element
- Navigation
- Preview
- Live component
- Imports
- No packages
- Added
Notes
The line under a sticky header appears only once the page moves.
At the top the header sits on the page; once scrolled it separates from what slides beneath. Pure CSS, tied to the scroll.
Fades in over the first 16px of scroll. animation-timeline: scroll().
Code
React and plain CSS"use client";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./header-hairline.css";
function HeaderHairlineDemo() {
return (
<div className="dm">
<div className="dm-frame" tabIndex={0} aria-label="Sample page, scroll inside it">
<div className="dm-frame-head">
<div className="bar-line">
<span>Your brand</span>
<span style={{ fontWeight: 500, color: "var(--s-mute)" }}>Menu</span>
</div>
</div>
<div className="dm-frame-body" aria-hidden="true">
{Array.from({ length: 16 }, (_, i) => (
<i key={i} style={{ width: `${55 + ((i * 37) % 45)}%` }} />
))}
</div>
</div>
<p className="dm-hint">Scroll inside the page: the line under its header appears</p>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ scroll: ".dm-frame", to: 0.6, ms: 1400, wait: 900 },
{ scroll: ".dm-frame", to: 0, ms: 900, wait: 400 },
];
export default function HeaderHairlineEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Header line on scroll" tour={TOUR}>
<HeaderHairlineDemo />
</PieceStage>
);
}