← Hub · Open the micro-demo

Table of contents (TOC) + scroll spy: aria-current, focus, and reduced motion

A TOC is just in-page navigation. A “scroll spy” is optional behavior that updates TOC state as you scroll.

Live demo: toc-scrollspy-demo.html (BEFORE/AFTER)

Baseline: a TOC that just works

If you add a scroll spy, do these things

Focus management: choose intentionally

When a TOC link is activated, you have two reasonable options:

Avoid surprise focus theft on plain scroll; only move focus on explicit navigation actions.

Reduced motion

Sticky headers / offsets

A small implementation sketch

// When active section changes:
link.setAttribute('aria-current','true');

// On TOC click (optional focus move):
content.scrollTo({ top: heading.offsetTop, behavior: reduceMotion ? 'auto' : 'smooth' });
heading.focus({ preventScroll: true });

Keep it simple. The hard part is behavior decisions (focus, motion, announcements), not the code.