- Docs
- Working examples
Working examples
Every live Scroll Spy preview with its explanation and the complete, copyable source files of each one.
Each preview is the same one shown on Home and Examples, and runs the real package. The copied files load the published browser bundle, so they work as they are.
Highlight the section you are reading
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Highlight the section you are reading</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Article sections">
<a href="#overview">Overview</a>
<a href="#install">Install</a>
<a href="#usage">Usage</a>
<a href="#faq">FAQ</a>
</nav>
<div id="article">
<section id="overview" style="min-height: 12rem">
<h2>Overview</h2>
</section>
<section id="install" style="min-height: 12rem">
<h2>Install</h2>
</section>
<section id="usage" style="min-height: 12rem">
<h2>Usage</h2>
</section>
<section id="faq" style="min-height: 4rem">
<h2>Faq</h2>
</section>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
gap: 0.25rem;
}
nav a {
padding: 0.25rem 0.5rem;
border-left: 2px solid #ddd;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
border-color: #a21caf;
color: #111;
font-weight: 600;
}
#article {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
padding: 0 1rem;
border: 1px solid #ccc;
}
JavaScript
// The article scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
ScrollSpy.auto({ root: document.querySelector('#article') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Highlight the section you are reading</title>
<style>
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
gap: 0.25rem;
}
nav a {
padding: 0.25rem 0.5rem;
border-left: 2px solid #ddd;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
border-color: #a21caf;
color: #111;
font-weight: 600;
}
#article {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
padding: 0 1rem;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Article sections">
<a href="#overview">Overview</a>
<a href="#install">Install</a>
<a href="#usage">Usage</a>
<a href="#faq">FAQ</a>
</nav>
<div id="article">
<section id="overview" style="min-height: 12rem">
<h2>Overview</h2>
</section>
<section id="install" style="min-height: 12rem">
<h2>Install</h2>
</section>
<section id="usage" style="min-height: 12rem">
<h2>Usage</h2>
</section>
<section id="faq" style="min-height: 4rem">
<h2>Faq</h2>
</section>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script>
// The article scrolls instead of the page, so pass it as the root.
// Called before the DOM is ready, this replaces the automatic start.
ScrollSpy.auto({ root: document.querySelector('#article') })
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Highlight the section you are reading</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Article sections">
<a href="#overview">Overview</a>
<a href="#install">Install</a>
<a href="#usage">Usage</a>
<a href="#faq">FAQ</a>
</nav>
<div id="article">
<section id="overview" style="min-height: 12rem">
<h2>Overview</h2>
</section>
<section id="install" style="min-height: 12rem">
<h2>Install</h2>
</section>
<section id="usage" style="min-height: 12rem">
<h2>Usage</h2>
</section>
<section id="faq" style="min-height: 4rem">
<h2>Faq</h2>
</section>
</div>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
gap: 0.25rem;
}
nav a {
padding: 0.25rem 0.5rem;
border-left: 2px solid #ddd;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
border-color: #a21caf;
color: #111;
font-weight: 600;
}
#article {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
padding: 0 1rem;
border: 1px solid #ccc;
}
JavaScript
import { autoScrollSpy } from '@samline/scroll-spy'
// The article scrolls instead of the page, so pass it as the root.
autoScrollSpy({ root: document.querySelector('#article') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Highlight the section you are reading</title>
<style>
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
gap: 0.25rem;
}
nav a {
padding: 0.25rem 0.5rem;
border-left: 2px solid #ddd;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
border-color: #a21caf;
color: #111;
font-weight: 600;
}
#article {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
padding: 0 1rem;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Article sections">
<a href="#overview">Overview</a>
<a href="#install">Install</a>
<a href="#usage">Usage</a>
<a href="#faq">FAQ</a>
</nav>
<div id="article">
<section id="overview" style="min-height: 12rem">
<h2>Overview</h2>
</section>
<section id="install" style="min-height: 12rem">
<h2>Install</h2>
</section>
<section id="usage" style="min-height: 12rem">
<h2>Usage</h2>
</section>
<section id="faq" style="min-height: 4rem">
<h2>Faq</h2>
</section>
</div>
</div>
</main>
<script type="module">
import { autoScrollSpy } from '@samline/scroll-spy'
// The article scrolls instead of the page, so pass it as the root.
autoScrollSpy({ root: document.querySelector('#article') })
</script>
</body>
</html>
A one-page menu with a class and the URL hash
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A one-page menu with a class and the URL hash</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<nav class="menu" data-scroll-spy>
<a href="#home">Home</a>
<a href="#work">Work</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<section id="home" style="min-height: 14rem">
<h2>Home</h2>
</section>
<section id="work" style="min-height: 14rem">
<h2>Work</h2>
</section>
<section id="about" style="min-height: 14rem">
<h2>About</h2>
</section>
<section id="contact" style="min-height: 14rem">
<h2>Contact</h2>
</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #ccc;
}
.menu {
position: sticky;
top: 0;
display: flex;
gap: 1rem;
padding: 0.75rem 1rem;
background: #fff;
border-bottom: 1px solid #ddd;
}
.menu a {
color: #555;
text-decoration: none;
}
.menu a.is-active {
color: #111;
font-weight: 600;
text-decoration: underline;
}
section {
padding: 0 1rem;
scroll-margin-top: 3rem;
}
JavaScript
ScrollSpy.auto({
root: document.querySelector('#page'),
activeClass: 'is-active',
updateHash: true,
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A one-page menu with a class and the URL hash</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #ccc;
}
.menu {
position: sticky;
top: 0;
display: flex;
gap: 1rem;
padding: 0.75rem 1rem;
background: #fff;
border-bottom: 1px solid #ddd;
}
.menu a {
color: #555;
text-decoration: none;
}
.menu a.is-active {
color: #111;
font-weight: 600;
text-decoration: underline;
}
section {
padding: 0 1rem;
scroll-margin-top: 3rem;
}
</style>
</head>
<body>
<main>
<div id="page">
<nav class="menu" data-scroll-spy>
<a href="#home">Home</a>
<a href="#work">Work</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<section id="home" style="min-height: 14rem">
<h2>Home</h2>
</section>
<section id="work" style="min-height: 14rem">
<h2>Work</h2>
</section>
<section id="about" style="min-height: 14rem">
<h2>About</h2>
</section>
<section id="contact" style="min-height: 14rem">
<h2>Contact</h2>
</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script>
ScrollSpy.auto({
root: document.querySelector('#page'),
activeClass: 'is-active',
updateHash: true,
})
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A one-page menu with a class and the URL hash</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="page">
<nav class="menu" data-scroll-spy>
<a href="#home">Home</a>
<a href="#work">Work</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<section id="home" style="min-height: 14rem">
<h2>Home</h2>
</section>
<section id="work" style="min-height: 14rem">
<h2>Work</h2>
</section>
<section id="about" style="min-height: 14rem">
<h2>About</h2>
</section>
<section id="contact" style="min-height: 14rem">
<h2>Contact</h2>
</section>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#page {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #ccc;
}
.menu {
position: sticky;
top: 0;
display: flex;
gap: 1rem;
padding: 0.75rem 1rem;
background: #fff;
border-bottom: 1px solid #ddd;
}
.menu a {
color: #555;
text-decoration: none;
}
.menu a.is-active {
color: #111;
font-weight: 600;
text-decoration: underline;
}
section {
padding: 0 1rem;
scroll-margin-top: 3rem;
}
JavaScript
import { autoScrollSpy } from '@samline/scroll-spy'
autoScrollSpy({
root: document.querySelector('#page'),
activeClass: 'is-active',
updateHash: true,
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A one-page menu with a class and the URL hash</title>
<style>
#page {
height: 16rem;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #ccc;
}
.menu {
position: sticky;
top: 0;
display: flex;
gap: 1rem;
padding: 0.75rem 1rem;
background: #fff;
border-bottom: 1px solid #ddd;
}
.menu a {
color: #555;
text-decoration: none;
}
.menu a.is-active {
color: #111;
font-weight: 600;
text-decoration: underline;
}
section {
padding: 0 1rem;
scroll-margin-top: 3rem;
}
</style>
</head>
<body>
<main>
<div id="page">
<nav class="menu" data-scroll-spy>
<a href="#home">Home</a>
<a href="#work">Work</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</nav>
<section id="home" style="min-height: 14rem">
<h2>Home</h2>
</section>
<section id="work" style="min-height: 14rem">
<h2>Work</h2>
</section>
<section id="about" style="min-height: 14rem">
<h2>About</h2>
</section>
<section id="contact" style="min-height: 14rem">
<h2>Contact</h2>
</section>
</div>
</main>
<script type="module">
import { autoScrollSpy } from '@samline/scroll-spy'
autoScrollSpy({
root: document.querySelector('#page'),
activeClass: 'is-active',
updateHash: true,
})
</script>
</body>
</html>
A long table of contents that follows along
Long contentsLive preview
When the navigation scrolls itself, the active link is kept in view by scrolling the navigation only, never the page.
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A long table of contents that follows along</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Chapters">
<a href="#chapter-1">Chapter 1</a>
<a href="#chapter-2">Chapter 2</a>
<a href="#chapter-3">Chapter 3</a>
<a href="#chapter-4">Chapter 4</a>
<a href="#chapter-5">Chapter 5</a>
<a href="#chapter-6">Chapter 6</a>
<a href="#chapter-7">Chapter 7</a>
<a href="#chapter-8">Chapter 8</a>
<a href="#chapter-9">Chapter 9</a>
<a href="#chapter-10">Chapter 10</a>
<a href="#chapter-11">Chapter 11</a>
<a href="#chapter-12">Chapter 12</a>
</nav>
<div id="book">
<section id="chapter-1"><h2>Chapter 1</h2></section>
<section id="chapter-2"><h2>Chapter 2</h2></section>
<section id="chapter-3"><h2>Chapter 3</h2></section>
<section id="chapter-4"><h2>Chapter 4</h2></section>
<section id="chapter-5"><h2>Chapter 5</h2></section>
<section id="chapter-6"><h2>Chapter 6</h2></section>
<section id="chapter-7"><h2>Chapter 7</h2></section>
<section id="chapter-8"><h2>Chapter 8</h2></section>
<section id="chapter-9"><h2>Chapter 9</h2></section>
<section id="chapter-10"><h2>Chapter 10</h2></section>
<section id="chapter-11"><h2>Chapter 11</h2></section>
<section id="chapter-12"><h2>Chapter 12</h2></section>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
height: 16rem;
overflow-y: auto;
}
nav a {
padding: 0.375rem 0.5rem;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
background: #efeafd;
color: #111;
}
#book {
height: 16rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
section {
min-height: 10rem;
}
JavaScript
ScrollSpy.auto({ root: document.querySelector('#book') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A long table of contents that follows along</title>
<style>
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
height: 16rem;
overflow-y: auto;
}
nav a {
padding: 0.375rem 0.5rem;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
background: #efeafd;
color: #111;
}
#book {
height: 16rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
section {
min-height: 10rem;
}
</style>
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Chapters">
<a href="#chapter-1">Chapter 1</a>
<a href="#chapter-2">Chapter 2</a>
<a href="#chapter-3">Chapter 3</a>
<a href="#chapter-4">Chapter 4</a>
<a href="#chapter-5">Chapter 5</a>
<a href="#chapter-6">Chapter 6</a>
<a href="#chapter-7">Chapter 7</a>
<a href="#chapter-8">Chapter 8</a>
<a href="#chapter-9">Chapter 9</a>
<a href="#chapter-10">Chapter 10</a>
<a href="#chapter-11">Chapter 11</a>
<a href="#chapter-12">Chapter 12</a>
</nav>
<div id="book">
<section id="chapter-1"><h2>Chapter 1</h2></section>
<section id="chapter-2"><h2>Chapter 2</h2></section>
<section id="chapter-3"><h2>Chapter 3</h2></section>
<section id="chapter-4"><h2>Chapter 4</h2></section>
<section id="chapter-5"><h2>Chapter 5</h2></section>
<section id="chapter-6"><h2>Chapter 6</h2></section>
<section id="chapter-7"><h2>Chapter 7</h2></section>
<section id="chapter-8"><h2>Chapter 8</h2></section>
<section id="chapter-9"><h2>Chapter 9</h2></section>
<section id="chapter-10"><h2>Chapter 10</h2></section>
<section id="chapter-11"><h2>Chapter 11</h2></section>
<section id="chapter-12"><h2>Chapter 12</h2></section>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-spy@1.0.1/dist/browser/scroll-spy.global.js"></script>
<script>
ScrollSpy.auto({ root: document.querySelector('#book') })
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A long table of contents that follows along</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Chapters">
<a href="#chapter-1">Chapter 1</a>
<a href="#chapter-2">Chapter 2</a>
<a href="#chapter-3">Chapter 3</a>
<a href="#chapter-4">Chapter 4</a>
<a href="#chapter-5">Chapter 5</a>
<a href="#chapter-6">Chapter 6</a>
<a href="#chapter-7">Chapter 7</a>
<a href="#chapter-8">Chapter 8</a>
<a href="#chapter-9">Chapter 9</a>
<a href="#chapter-10">Chapter 10</a>
<a href="#chapter-11">Chapter 11</a>
<a href="#chapter-12">Chapter 12</a>
</nav>
<div id="book">
<section id="chapter-1"><h2>Chapter 1</h2></section>
<section id="chapter-2"><h2>Chapter 2</h2></section>
<section id="chapter-3"><h2>Chapter 3</h2></section>
<section id="chapter-4"><h2>Chapter 4</h2></section>
<section id="chapter-5"><h2>Chapter 5</h2></section>
<section id="chapter-6"><h2>Chapter 6</h2></section>
<section id="chapter-7"><h2>Chapter 7</h2></section>
<section id="chapter-8"><h2>Chapter 8</h2></section>
<section id="chapter-9"><h2>Chapter 9</h2></section>
<section id="chapter-10"><h2>Chapter 10</h2></section>
<section id="chapter-11"><h2>Chapter 11</h2></section>
<section id="chapter-12"><h2>Chapter 12</h2></section>
</div>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
height: 16rem;
overflow-y: auto;
}
nav a {
padding: 0.375rem 0.5rem;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
background: #efeafd;
color: #111;
}
#book {
height: 16rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
section {
min-height: 10rem;
}
JavaScript
import { autoScrollSpy } from '@samline/scroll-spy'
autoScrollSpy({ root: document.querySelector('#book') })
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A long table of contents that follows along</title>
<style>
.layout {
display: grid;
grid-template-columns: 9rem 1fr;
gap: 1rem;
}
nav {
display: grid;
align-content: start;
height: 16rem;
overflow-y: auto;
}
nav a {
padding: 0.375rem 0.5rem;
color: #555;
text-decoration: none;
}
nav a[aria-current] {
background: #efeafd;
color: #111;
}
#book {
height: 16rem;
overflow-y: auto;
padding: 0 1rem;
border: 1px solid #ccc;
}
section {
min-height: 10rem;
}
</style>
</head>
<body>
<main>
<div class="layout">
<nav data-scroll-spy aria-label="Chapters">
<a href="#chapter-1">Chapter 1</a>
<a href="#chapter-2">Chapter 2</a>
<a href="#chapter-3">Chapter 3</a>
<a href="#chapter-4">Chapter 4</a>
<a href="#chapter-5">Chapter 5</a>
<a href="#chapter-6">Chapter 6</a>
<a href="#chapter-7">Chapter 7</a>
<a href="#chapter-8">Chapter 8</a>
<a href="#chapter-9">Chapter 9</a>
<a href="#chapter-10">Chapter 10</a>
<a href="#chapter-11">Chapter 11</a>
<a href="#chapter-12">Chapter 12</a>
</nav>
<div id="book">
<section id="chapter-1"><h2>Chapter 1</h2></section>
<section id="chapter-2"><h2>Chapter 2</h2></section>
<section id="chapter-3"><h2>Chapter 3</h2></section>
<section id="chapter-4"><h2>Chapter 4</h2></section>
<section id="chapter-5"><h2>Chapter 5</h2></section>
<section id="chapter-6"><h2>Chapter 6</h2></section>
<section id="chapter-7"><h2>Chapter 7</h2></section>
<section id="chapter-8"><h2>Chapter 8</h2></section>
<section id="chapter-9"><h2>Chapter 9</h2></section>
<section id="chapter-10"><h2>Chapter 10</h2></section>
<section id="chapter-11"><h2>Chapter 11</h2></section>
<section id="chapter-12"><h2>Chapter 12</h2></section>
</div>
</div>
</main>
<script type="module">
import { autoScrollSpy } from '@samline/scroll-spy'
autoScrollSpy({ root: document.querySelector('#book') })
</script>
</body>
</html>