navigationcontrollerz-scrollspy

Scrollspy

Scroll-driven behavior in two modes: nav highlights links whose sections are in view, and reveal animates elements as they enter the viewport.

Attributes

AttributeTypeDefaultDescription
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.
navbooleanfalseShorthand for mode="nav".
targetstring''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]).
animationstring''Animation preset(s) applied to reveal targets as they enter view; its presence also helps infer reveal mode.
no-hiddenbooleanfalseKeeps reveal targets visible before they enter view instead of hiding them with opacity: 0.
marginstring'-1px'IntersectionObserver root margin for reveal mode; the default -1px shrinks the viewport by a pixel.
thresholdnumber0IntersectionObserver threshold (0–1) for reveal mode: the fraction of the target that must be visible to trigger.
repeatbooleanfalseRe-runs the reveal animation every time a target re-enters view, instead of revealing once.
delaynumber0Delay in milliseconds before each target reveals; targets reveal in sequence through a queue.
durationnumber0Overrides the reveal animation duration in milliseconds; 0 uses the animation's own duration.
closeststring''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.
offsetstring'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.
scrollbooleanfalseEnables 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-parentbooleanfalseAlso applies the active class to ancestor li.z-parent items that contain the active link.
Inherited from Base
AttributeTypeDefaultDescription
clsstring''Adds classes to named parts as key: classes pairs; a plain string targets the default part.
stlstring''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

KeyDefault classclsstlDescription
activez-activeyesnoClass applied to the active nav item when the default part is empty.
default—yesnoDefault 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.
inviewz-scrollspy-inviewyesnoClass applied to reveal targets while they are in view.
reveal—yesnoSource of animation classes for reveal targets, used when animation is empty; per-element data-scrollspy-class overrides both.

Events

EventDetailFlagsDescription
z-scrollspy:active{ index: number; previousIndex: number; item: HTMLElement | null; link: HTMLAnchorElement | null; target: HTMLElement | null; id: string | null }bubblesFires 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, bubblesFires 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 }bubblesFires alongside active on every nav active-state change, with the same detail.
z-scrollspy:inview{ index: number; target: HTMLElement }bubblesFires when a reveal target enters view. Detail carries index and target.
z-scrollspy:mount · from ControllerRecord<string, unknown>bubblesFires when deferred template content is mounted into the container.
z-scrollspy:outview{ index: number; target: HTMLElement }bubblesFires 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 }bubblesFires after a smooth scroll to a nav target is initiated, with the same detail as before-scroll.

Methods

SignatureDescription
activeIndex(): numberReturns 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(): voidTears down and re-initializes the observers and listeners after the DOM or attributes change.

Data Hooks

AttributeValuesApplies toRoleDescription
data-fn · from ControllertemplatedescendantconfigMarks a deferred <template> child whose content is mounted into the container on demand.
data-scrollspy-class—descendantconfigPer-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.