Navigation

Scrollspy

An “On this page” table of contents that marks the section being read.

Class nameTypeDescription
scrollspyplf:scrollspyComponentThe table of contents landmark
scrollspy-titleplf:scrollspy-titlePartThe heading
scrollspy-listplf:scrollspy-listPartA list of links; nests for subsections
scrollspy-itemplf:scrollspy-itemPartA list item
scrollspy-linkplf:scrollspy-linkPartA link to a section

Overview

Content for the overview. It is long enough to scroll past before the next section starts.

Install

Content for the install section. It is long enough to scroll past before the next section starts.

Usage

Content for the usage section. It is long enough to scroll past before the next section starts.

FAQ

Content for the last section.

Each link needs exactly one target, inside the scrolling element when data-plf-container is set.

Introduction

Content for the introduction. It is long enough to scroll past before the next section starts.

Step one

Content for step one. It is long enough to scroll past before the next section starts.

Step two

Content for step two. It is long enough to scroll past before the next section starts.

Summary

Content for the summary.

JavaScript

Scrollspy needs JavaScript to mark the current link. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-scrollspy Marks the table of contents, named with aria-label or aria-labelledby
data-plf-container Optional: the id of the element that scrolls the sections; the page by default
href="#…" Each link points at exactly one element with that id

The script sets aria-current="location" on the current link.

Methods

Method Behavior
PLF.Scrollspy.getOrCreateInstance(element) Returns the instance, creating it for valid markup
PLF.Scrollspy.getInstance(element) Returns the instance or null
instance.getState() Returns { active, target }: the current link and its section
instance.refresh() Marks the current link again, for example after content changed height
instance.destroy() Removes listeners and aria-current
Event When
plf:scrollspy:change After the current link changes

The event’s detail:

Property Value
instance The Scrollspy instance
link The current link
target The current link’s section
previous The link current before
reason scroll, trigger when a link or the address’s hash is followed, or api from refresh()
trigger The clicked link, or null for a scroll, the address’s hash or refresh()
Key Action
Tab, Shift+Tab Move between the links
Enter Follows the focused link, which becomes current

React

Scrollspy from @pelatform/react renders the nav around your links and marks the link of the section being read with aria-current="location", as the script does; each part is a component of its own.

Prop Values Default
container The element that scrolls the sections and holds each link’s section, as a ref or the element The page

The parts:

Part Element
ScrollspyTitle A p with scrollspy-title
ScrollspyList A ul with scrollspy-list
ScrollspyItem An li with scrollspy-item
ScrollspyLink An a with scrollspy-link, whose href="#…" names its section

Other props are the element’s own, className and ref included.

Variable Default
--plf-scrollspy-line-color --plf-color-border-subtle
--plf-scrollspy-indicator-color --plf-color-primary
--plf-scrollspy-current-color --plf-color-text

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons