navigationcontroller
z-scrollspyScrollspy
Scroll-driven behavior in two modes: nav highlights links whose sections are in view, and reveal animates elements as they enter the viewport.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
mode | '' | 'nav' | 'reveal' | '' | Forces a mode: nav highlights links whose targets scroll into view, reveal animates elements as they enter the viewport. When empty, the mode is inferred from the attributes and markup. |
nav | boolean | false | Shorthand for mode="nav". |
target | string | '' | CSS selector for the elements to watch: the animated targets in reveal mode (default: the host), or the links in nav mode (default: owned a[href]). |
animation | string | '' | Animation preset(s) applied to reveal targets as they enter view; its presence also helps infer reveal mode. |
no-hidden | boolean | false | Keeps reveal targets visible before they enter view instead of hiding them with opacity: 0. |
margin | string | '-1px' | IntersectionObserver root margin for reveal mode; the default -1px shrinks the viewport by a pixel. |
threshold | number | 0 | IntersectionObserver threshold (0–1) for reveal mode: the fraction of the target that must be visible to trigger. |
repeat | boolean | false | Re-runs the reveal animation every time a target re-enters view, instead of revealing once. |
delay | number | 0 | Delay in milliseconds before each target reveals; targets reveal in sequence through a queue. |
duration | number | 0 | Overrides the reveal animation duration in milliseconds; 0 uses the animation's own duration. |
closest | string | '' | CSS selector for the nav item that receives the active class, resolved with closest() from each link. self/false targets the link itself; by default the parent <li> is used when present. |
offset | string | '0' | Offset from the viewport top used to decide which nav target is active and where smooth scrolling lands; accepts a length, % of the viewport height, a selector, or selector ± length. |
scroll | boolean | false | Enables smooth scrolling to a nav target on click and updates the URL hash; falls back to instant scrolling when the user prefers reduced motion. |
active-parent | boolean | false | Also applies the active class to ancestor li.z-parent items that contain the active link. |
Inherited from Base
| Attribute | Type | Default | Description |
|---|---|---|---|
cls | string | '' | Adds classes to named parts as key: classes pairs; a plain string targets the default part. |
stl | string | '' | Adds inline styles to named parts as key: styles pairs; a plain string targets the default part. |
Custom Styling (cls / stl)
Default cls target:default · defaultstl target:host-inner
| Key | Default class | cls | stl | Description |
|---|---|---|---|---|
active | z-active | yes | no | Class applied to the active nav item when the default part is empty. |
default | — | yes | no | Default cls target. In nav mode its classes mark the active item (falling back to active); in reveal mode it is a fallback source of animation classes. |
inview | z-scrollspy-inview | yes | no | Class applied to reveal targets while they are in view. |
reveal | — | yes | no | Source of animation classes for reveal targets, used when animation is empty; per-element data-scrollspy-class overrides both. |
Events
| Event | Detail | Flags | Description |
|---|---|---|---|
z-scrollspy:active | { index: number; previousIndex: number; item: HTMLElement | null; link: HTMLAnchorElement | null; target: HTMLElement | null; id: string | null } | bubbles | Fires when the active nav target changes. Detail carries index, previousIndex, item, link, target, and id. |
z-scrollspy:before-scroll | { index: number; item: HTMLElement; link: HTMLAnchorElement; target: HTMLElement; id: string } | cancelable, bubbles | Fires before smooth-scrolling to a nav target on click; preventDefault() cancels the scroll. Detail carries index, item, link, target, and id. |
z-scrollspy:change | { index: number; previousIndex: number; item: HTMLElement | null; link: HTMLAnchorElement | null; target: HTMLElement | null; id: string | null } | bubbles | Fires alongside active on every nav active-state change, with the same detail. |
z-scrollspy:inview | { index: number; target: HTMLElement } | bubbles | Fires when a reveal target enters view. Detail carries index and target. |
z-scrollspy:mount · from Controller | Record<string, unknown> | bubbles | Fires when deferred template content is mounted into the container. |
z-scrollspy:outview | { index: number; target: HTMLElement } | bubbles | Fires when a reveal target leaves view (with repeat). Detail carries index and target. |
z-scrollspy:scrolled | { index: number; item: HTMLElement; link: HTMLAnchorElement; target: HTMLElement; id: string } | bubbles | Fires after a smooth scroll to a nav target is initiated, with the same detail as before-scroll. |
Methods
| Signature | Description |
|---|---|
activeIndex(): number | Returns the index of the active nav target, or -1 in reveal mode or when none is active. |
inviewElements(): HTMLElement[] | Returns the reveal targets currently in view; empty in nav mode. |
refresh(): void | Tears down and re-initializes the observers and listeners after the DOM or attributes change. |
Data Hooks
| Attribute | Values | Applies to | Role | Description |
|---|---|---|---|---|
data-fn · from Controller | template | descendant | config | Marks a deferred <template> child whose content is mounted into the container on demand. |
data-scrollspy-class | — | descendant | config | Per-element override of the reveal animation classes, replacing the animation attribute and the reveal/default parts for that target. |
Accessibility
- In nav mode the active link gets
aria-current="location"; it is removed from all other links.