Skip to content
@samline/scroll-tov1.6.0
AboutInstallRecipesFAQExamplesDocs

@samline/scroll-to

Explore Scroll To

A starting point, a working example, and the details when you need them.

  • 01AboutUnderstand the problem and the approach.
  • 02InstallChoose your setup and follow the first steps.
  • 03RecipesUseful patterns and integration context.
  • 04FAQFind the practical details.
  • 05ExamplesTry a preview and explore its code.
  • 06DocsRead guides and the complete reference.

Around the project

  • GitHub(opens in a new tab)
  • npm(opens in a new tab)

v1.6.0·MIT

GitHub(opens in a new tab)Get started
Examples

Scroll To examples.

Live previews of distinct scenarios, each with independently copyable files. The same examples appear in the documentation.

Try the exampleInstallation guide

Each preview runs the real package. The copied files load the published browser bundle, so they run as they are.

Choose an example

Fixed headerCentered tabsWait for arrivalFollow the scrollEvents and stateStacked barsPage a stripNext and previousHidden answersHighlightBack to topNested boxesGuided tour
01 / Fixed header

Anchors that land below a fixed header

Fixed headerLive preview

Same-page links scroll smoothly and stop below the header, move focus to the section and add a history entry, so Back works. The header height is read live.

FeaturesPricingFAQ

Features

Lands just below the bar, and focus moves here.

Pricing

Lands just below the bar, and focus moves here.

FAQ

Lands just below the bar, and focus moves here.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Anchors that land below a fixed header</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <header id="header">
        <nav>
          <a href="#features">Features</a>
          <a href="#pricing">Pricing</a>
          <a href="#faq">FAQ</a>
        </nav>
      </header>
      <main>
        <section id="features">
          <h2>Features</h2>
        </section>
        <section id="pricing">
          <h2>Pricing</h2>
        </section>
        <section id="faq">
          <h2>Faq</h2>
        </section>
        <button type="button" data-scroll-to="top">Back to top</button>
      </main>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
body {
  margin: 0;
  font-family: system-ui, sans-serif;
}
header {
  position: fixed;
  inset: 0 0 auto;
  z-index: 1;
  padding: 1rem 1.5rem;
  background: #fff;
  border-bottom: 1px solid #ddd;
}
nav {
  display: flex;
  gap: 1rem;
}
main {
  padding: 5rem 1.5rem 50vh;
}
section {
  min-height: 70vh;
  border-top: 1px solid #ddd;
}
section:focus {
  outline: none;
}

JavaScript

main.js
// Called before the DOM is ready, this replaces the automatic start.
ScrollTo.auto({ offset: '#header' })

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Anchors that land below a fixed header</title>
    <style>
      body {
        margin: 0;
        font-family: system-ui, sans-serif;
      }
      header {
        position: fixed;
        inset: 0 0 auto;
        z-index: 1;
        padding: 1rem 1.5rem;
        background: #fff;
        border-bottom: 1px solid #ddd;
      }
      nav {
        display: flex;
        gap: 1rem;
      }
      main {
        padding: 5rem 1.5rem 50vh;
      }
      section {
        min-height: 70vh;
        border-top: 1px solid #ddd;
      }
      section:focus {
        outline: none;
      }
    </style>
  </head>
  <body>
    <main>
      <header id="header">
        <nav>
          <a href="#features">Features</a>
          <a href="#pricing">Pricing</a>
          <a href="#faq">FAQ</a>
        </nav>
      </header>
      <main>
        <section id="features">
          <h2>Features</h2>
        </section>
        <section id="pricing">
          <h2>Pricing</h2>
        </section>
        <section id="faq">
          <h2>Faq</h2>
        </section>
        <button type="button" data-scroll-to="top">Back to top</button>
      </main>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script>
      // Called before the DOM is ready, this replaces the automatic start.
      ScrollTo.auto({ offset: '#header' })
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Anchors that land below a fixed header</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <header id="header">
        <nav>
          <a href="#features">Features</a>
          <a href="#pricing">Pricing</a>
          <a href="#faq">FAQ</a>
        </nav>
      </header>
      <main>
        <section id="features">
          <h2>Features</h2>
        </section>
        <section id="pricing">
          <h2>Pricing</h2>
        </section>
        <section id="faq">
          <h2>Faq</h2>
        </section>
        <button type="button" data-scroll-to="top">Back to top</button>
      </main>
    </main>
    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
body {
  margin: 0;
  font-family: system-ui, sans-serif;
}
header {
  position: fixed;
  inset: 0 0 auto;
  z-index: 1;
  padding: 1rem 1.5rem;
  background: #fff;
  border-bottom: 1px solid #ddd;
}
nav {
  display: flex;
  gap: 1rem;
}
main {
  padding: 5rem 1.5rem 50vh;
}
section {
  min-height: 70vh;
  border-top: 1px solid #ddd;
}
section:focus {
  outline: none;
}

JavaScript

main.js
import { autoScrollTo, scrollTo } from '@samline/scroll-to'

autoScrollTo({ offset: '#header' })

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Anchors that land below a fixed header</title>
    <style>
      body {
        margin: 0;
        font-family: system-ui, sans-serif;
      }
      header {
        position: fixed;
        inset: 0 0 auto;
        z-index: 1;
        padding: 1rem 1.5rem;
        background: #fff;
        border-bottom: 1px solid #ddd;
      }
      nav {
        display: flex;
        gap: 1rem;
      }
      main {
        padding: 5rem 1.5rem 50vh;
      }
      section {
        min-height: 70vh;
        border-top: 1px solid #ddd;
      }
      section:focus {
        outline: none;
      }
    </style>
  </head>
  <body>
    <main>
      <header id="header">
        <nav>
          <a href="#features">Features</a>
          <a href="#pricing">Pricing</a>
          <a href="#faq">FAQ</a>
        </nav>
      </header>
      <main>
        <section id="features">
          <h2>Features</h2>
        </section>
        <section id="pricing">
          <h2>Pricing</h2>
        </section>
        <section id="faq">
          <h2>Faq</h2>
        </section>
        <button type="button" data-scroll-to="top">Back to top</button>
      </main>
    </main>
    <script type="module">
      import { autoScrollTo, scrollTo } from '@samline/scroll-to'

      autoScrollTo({ offset: '#header' })
    </script>
  </body>
</html>
02 / Centered tabs

Keep the selected tab in the middle

Centered tabsLive preview

Inside a horizontal strip, data-scroll-to-container and data-scroll-to-inline="center" scroll only the strip, never the page.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Keep the selected tab in the middle</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <div class="tabs" id="tabs" role="tablist">
        <button type="button" role="tab" id="tab-1" aria-selected="true">Topic 1</button>
        <button type="button" role="tab" id="tab-2" aria-selected="false">Topic 2</button>
        <button type="button" role="tab" id="tab-3" aria-selected="false">Topic 3</button>
        <button type="button" role="tab" id="tab-4" aria-selected="false">Topic 4</button>
        <button type="button" role="tab" id="tab-5" aria-selected="false">Topic 5</button>
        <button type="button" role="tab" id="tab-6" aria-selected="false">Topic 6</button>
        <button type="button" role="tab" id="tab-7" aria-selected="false">Topic 7</button>
        <button type="button" role="tab" id="tab-8" aria-selected="false">Topic 8</button>
        <button type="button" role="tab" id="tab-9" aria-selected="false">Topic 9</button>
        <button type="button" role="tab" id="tab-10" aria-selected="false">Topic 10</button>
        <button type="button" role="tab" id="tab-11" aria-selected="false">Topic 11</button>
        <button type="button" role="tab" id="tab-12" aria-selected="false">Topic 12</button>
      </div>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
body {
  font-family: system-ui, sans-serif;
}
.tabs {
  display: flex;
  gap: 0.5rem;
  max-width: 32rem;
  overflow-x: auto;
  padding: 0.5rem;
  border: 1px solid #ddd;
}
.tabs button {
  flex: none;
  padding: 0.5rem 1rem;
  border: 1px solid #ccc;
  border-radius: 999px;
  background: #fff;
}
.tabs [aria-selected='true'] {
  background: #78350f;
  border-color: #78350f;
  color: #fff;
}

JavaScript

main.js
const tabs = document.querySelector('#tabs')

tabs.addEventListener('click', event => {
  const tab = event.target.closest('[role="tab"]')
  if (!tab) return
  tabs.querySelector('[aria-selected="true"]').setAttribute('aria-selected', 'false')
  tab.setAttribute('aria-selected', 'true')
  ScrollTo.scrollTo(tab, { container: tabs, inline: 'center', block: 'nearest' })
})

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Keep the selected tab in the middle</title>
    <style>
      body {
        font-family: system-ui, sans-serif;
      }
      .tabs {
        display: flex;
        gap: 0.5rem;
        max-width: 32rem;
        overflow-x: auto;
        padding: 0.5rem;
        border: 1px solid #ddd;
      }
      .tabs button {
        flex: none;
        padding: 0.5rem 1rem;
        border: 1px solid #ccc;
        border-radius: 999px;
        background: #fff;
      }
      .tabs [aria-selected='true'] {
        background: #78350f;
        border-color: #78350f;
        color: #fff;
      }
    </style>
  </head>
  <body>
    <main>
      <div class="tabs" id="tabs" role="tablist">
        <button type="button" role="tab" id="tab-1" aria-selected="true">Topic 1</button>
        <button type="button" role="tab" id="tab-2" aria-selected="false">Topic 2</button>
        <button type="button" role="tab" id="tab-3" aria-selected="false">Topic 3</button>
        <button type="button" role="tab" id="tab-4" aria-selected="false">Topic 4</button>
        <button type="button" role="tab" id="tab-5" aria-selected="false">Topic 5</button>
        <button type="button" role="tab" id="tab-6" aria-selected="false">Topic 6</button>
        <button type="button" role="tab" id="tab-7" aria-selected="false">Topic 7</button>
        <button type="button" role="tab" id="tab-8" aria-selected="false">Topic 8</button>
        <button type="button" role="tab" id="tab-9" aria-selected="false">Topic 9</button>
        <button type="button" role="tab" id="tab-10" aria-selected="false">Topic 10</button>
        <button type="button" role="tab" id="tab-11" aria-selected="false">Topic 11</button>
        <button type="button" role="tab" id="tab-12" aria-selected="false">Topic 12</button>
      </div>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script>
      const tabs = document.querySelector('#tabs')

      tabs.addEventListener('click', event => {
        const tab = event.target.closest('[role="tab"]')
        if (!tab) return
        tabs.querySelector('[aria-selected="true"]').setAttribute('aria-selected', 'false')
        tab.setAttribute('aria-selected', 'true')
        ScrollTo.scrollTo(tab, { container: tabs, inline: 'center', block: 'nearest' })
      })
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Keep the selected tab in the middle</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <div class="tabs" id="tabs" role="tablist">
        <button type="button" role="tab" id="tab-1" aria-selected="true">Topic 1</button>
        <button type="button" role="tab" id="tab-2" aria-selected="false">Topic 2</button>
        <button type="button" role="tab" id="tab-3" aria-selected="false">Topic 3</button>
        <button type="button" role="tab" id="tab-4" aria-selected="false">Topic 4</button>
        <button type="button" role="tab" id="tab-5" aria-selected="false">Topic 5</button>
        <button type="button" role="tab" id="tab-6" aria-selected="false">Topic 6</button>
        <button type="button" role="tab" id="tab-7" aria-selected="false">Topic 7</button>
        <button type="button" role="tab" id="tab-8" aria-selected="false">Topic 8</button>
        <button type="button" role="tab" id="tab-9" aria-selected="false">Topic 9</button>
        <button type="button" role="tab" id="tab-10" aria-selected="false">Topic 10</button>
        <button type="button" role="tab" id="tab-11" aria-selected="false">Topic 11</button>
        <button type="button" role="tab" id="tab-12" aria-selected="false">Topic 12</button>
      </div>
    </main>
    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
body {
  font-family: system-ui, sans-serif;
}
.tabs {
  display: flex;
  gap: 0.5rem;
  max-width: 32rem;
  overflow-x: auto;
  padding: 0.5rem;
  border: 1px solid #ddd;
}
.tabs button {
  flex: none;
  padding: 0.5rem 1rem;
  border: 1px solid #ccc;
  border-radius: 999px;
  background: #fff;
}
.tabs [aria-selected='true'] {
  background: #78350f;
  border-color: #78350f;
  color: #fff;
}

JavaScript

main.js
import { autoScrollTo, scrollTo } from '@samline/scroll-to'

const tabs = document.querySelector('#tabs')

tabs.addEventListener('click', event => {
  const tab = event.target.closest('[role="tab"]')
  if (!tab) return
  tabs.querySelector('[aria-selected="true"]').setAttribute('aria-selected', 'false')
  tab.setAttribute('aria-selected', 'true')
  scrollTo(tab, { container: tabs, inline: 'center', block: 'nearest' })
})

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Keep the selected tab in the middle</title>
    <style>
      body {
        font-family: system-ui, sans-serif;
      }
      .tabs {
        display: flex;
        gap: 0.5rem;
        max-width: 32rem;
        overflow-x: auto;
        padding: 0.5rem;
        border: 1px solid #ddd;
      }
      .tabs button {
        flex: none;
        padding: 0.5rem 1rem;
        border: 1px solid #ccc;
        border-radius: 999px;
        background: #fff;
      }
      .tabs [aria-selected='true'] {
        background: #78350f;
        border-color: #78350f;
        color: #fff;
      }
    </style>
  </head>
  <body>
    <main>
      <div class="tabs" id="tabs" role="tablist">
        <button type="button" role="tab" id="tab-1" aria-selected="true">Topic 1</button>
        <button type="button" role="tab" id="tab-2" aria-selected="false">Topic 2</button>
        <button type="button" role="tab" id="tab-3" aria-selected="false">Topic 3</button>
        <button type="button" role="tab" id="tab-4" aria-selected="false">Topic 4</button>
        <button type="button" role="tab" id="tab-5" aria-selected="false">Topic 5</button>
        <button type="button" role="tab" id="tab-6" aria-selected="false">Topic 6</button>
        <button type="button" role="tab" id="tab-7" aria-selected="false">Topic 7</button>
        <button type="button" role="tab" id="tab-8" aria-selected="false">Topic 8</button>
        <button type="button" role="tab" id="tab-9" aria-selected="false">Topic 9</button>
        <button type="button" role="tab" id="tab-10" aria-selected="false">Topic 10</button>
        <button type="button" role="tab" id="tab-11" aria-selected="false">Topic 11</button>
        <button type="button" role="tab" id="tab-12" aria-selected="false">Topic 12</button>
      </div>
    </main>
    <script type="module">
      import { autoScrollTo, scrollTo } from '@samline/scroll-to'

      const tabs = document.querySelector('#tabs')

      tabs.addEventListener('click', event => {
        const tab = event.target.closest('[role="tab"]')
        if (!tab) return
        tabs.querySelector('[aria-selected="true"]').setAttribute('aria-selected', 'false')
        tab.setAttribute('aria-selected', 'true')
        scrollTo(tab, { container: tabs, inline: 'center', block: 'nearest' })
      })
    </script>
  </body>
</html>
03 / Wait for arrival

Do something once it arrives

Wait for arrivalLive preview

scrollTo returns a promise: true when it arrives, false when the visitor took over. Here the answer is highlighted only after the scroll ends.

Question 1

Answer 1.

Question 2

Answer 2.

Question 3

Answer 3.

Question 4

Answer 4.

Question 5

Answer 5.

Question 6

Answer 6.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Do something once it arrives</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <button type="button" id="show">Show the answer to question 4</button>
      <main>
        <article id="q1">
          <h2>Question 1</h2>
          <p>Answer 1.</p>
        </article>
        <article id="q2">
          <h2>Question 2</h2>
          <p>Answer 2.</p>
        </article>
        <article id="q3">
          <h2>Question 3</h2>
          <p>Answer 3.</p>
        </article>
        <article id="q4">
          <h2>Question 4</h2>
          <p>Answer 4.</p>
        </article>
        <article id="q5">
          <h2>Question 5</h2>
          <p>Answer 5.</p>
        </article>
        <article id="q6">
          <h2>Question 6</h2>
          <p>Answer 6.</p>
        </article>
      </main>
      <p id="status" aria-live="polite"></p>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
body {
  font-family: system-ui, sans-serif;
}
#show {
  position: fixed;
  top: 1rem;
  right: 1rem;
}
#status {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  margin: 0;
}
article {
  min-height: 60vh;
  padding: 1rem;
  border-top: 1px solid #ddd;
  transition: background 0.6s;
}
article.flash {
  background: #fff6c2;
}

JavaScript

main.js
const status = document.querySelector('#status')

document.querySelector('#show').addEventListener('click', async () => {
  const answer = document.querySelector('#q4')
  const arrived = await ScrollTo.scrollTo(answer, { offset: 16, duration: 900 })
  status.textContent = arrived ? 'Arrived' : 'Interrupted'
  if (arrived) {
    answer.classList.add('flash')
    setTimeout(() => answer.classList.remove('flash'), 1200)
  }
})

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Do something once it arrives</title>
    <style>
      body {
        font-family: system-ui, sans-serif;
      }
      #show {
        position: fixed;
        top: 1rem;
        right: 1rem;
      }
      #status {
        position: fixed;
        bottom: 1rem;
        right: 1rem;
        margin: 0;
      }
      article {
        min-height: 60vh;
        padding: 1rem;
        border-top: 1px solid #ddd;
        transition: background 0.6s;
      }
      article.flash {
        background: #fff6c2;
      }
    </style>
  </head>
  <body>
    <main>
      <button type="button" id="show">Show the answer to question 4</button>
      <main>
        <article id="q1">
          <h2>Question 1</h2>
          <p>Answer 1.</p>
        </article>
        <article id="q2">
          <h2>Question 2</h2>
          <p>Answer 2.</p>
        </article>
        <article id="q3">
          <h2>Question 3</h2>
          <p>Answer 3.</p>
        </article>
        <article id="q4">
          <h2>Question 4</h2>
          <p>Answer 4.</p>
        </article>
        <article id="q5">
          <h2>Question 5</h2>
          <p>Answer 5.</p>
        </article>
        <article id="q6">
          <h2>Question 6</h2>
          <p>Answer 6.</p>
        </article>
      </main>
      <p id="status" aria-live="polite"></p>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script>
      const status = document.querySelector('#status')

      document.querySelector('#show').addEventListener('click', async () => {
        const answer = document.querySelector('#q4')
        const arrived = await ScrollTo.scrollTo(answer, { offset: 16, duration: 900 })
        status.textContent = arrived ? 'Arrived' : 'Interrupted'
        if (arrived) {
          answer.classList.add('flash')
          setTimeout(() => answer.classList.remove('flash'), 1200)
        }
      })
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Do something once it arrives</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <button type="button" id="show">Show the answer to question 4</button>
      <main>
        <article id="q1">
          <h2>Question 1</h2>
          <p>Answer 1.</p>
        </article>
        <article id="q2">
          <h2>Question 2</h2>
          <p>Answer 2.</p>
        </article>
        <article id="q3">
          <h2>Question 3</h2>
          <p>Answer 3.</p>
        </article>
        <article id="q4">
          <h2>Question 4</h2>
          <p>Answer 4.</p>
        </article>
        <article id="q5">
          <h2>Question 5</h2>
          <p>Answer 5.</p>
        </article>
        <article id="q6">
          <h2>Question 6</h2>
          <p>Answer 6.</p>
        </article>
      </main>
      <p id="status" aria-live="polite"></p>
    </main>
    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
body {
  font-family: system-ui, sans-serif;
}
#show {
  position: fixed;
  top: 1rem;
  right: 1rem;
}
#status {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  margin: 0;
}
article {
  min-height: 60vh;
  padding: 1rem;
  border-top: 1px solid #ddd;
  transition: background 0.6s;
}
article.flash {
  background: #fff6c2;
}

JavaScript

main.js
import { autoScrollTo, scrollTo } from '@samline/scroll-to'

const status = document.querySelector('#status')

document.querySelector('#show').addEventListener('click', async () => {
  const answer = document.querySelector('#q4')
  const arrived = await scrollTo(answer, { offset: 16, duration: 900 })
  status.textContent = arrived ? 'Arrived' : 'Interrupted'
  if (arrived) {
    answer.classList.add('flash')
    setTimeout(() => answer.classList.remove('flash'), 1200)
  }
})

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Do something once it arrives</title>
    <style>
      body {
        font-family: system-ui, sans-serif;
      }
      #show {
        position: fixed;
        top: 1rem;
        right: 1rem;
      }
      #status {
        position: fixed;
        bottom: 1rem;
        right: 1rem;
        margin: 0;
      }
      article {
        min-height: 60vh;
        padding: 1rem;
        border-top: 1px solid #ddd;
        transition: background 0.6s;
      }
      article.flash {
        background: #fff6c2;
      }
    </style>
  </head>
  <body>
    <main>
      <button type="button" id="show">Show the answer to question 4</button>
      <main>
        <article id="q1">
          <h2>Question 1</h2>
          <p>Answer 1.</p>
        </article>
        <article id="q2">
          <h2>Question 2</h2>
          <p>Answer 2.</p>
        </article>
        <article id="q3">
          <h2>Question 3</h2>
          <p>Answer 3.</p>
        </article>
        <article id="q4">
          <h2>Question 4</h2>
          <p>Answer 4.</p>
        </article>
        <article id="q5">
          <h2>Question 5</h2>
          <p>Answer 5.</p>
        </article>
        <article id="q6">
          <h2>Question 6</h2>
          <p>Answer 6.</p>
        </article>
      </main>
      <p id="status" aria-live="polite"></p>
    </main>
    <script type="module">
      import { autoScrollTo, scrollTo } from '@samline/scroll-to'

      const status = document.querySelector('#status')

      document.querySelector('#show').addEventListener('click', async () => {
        const answer = document.querySelector('#q4')
        const arrived = await scrollTo(answer, { offset: 16, duration: 900 })
        status.textContent = arrived ? 'Arrived' : 'Interrupted'
        if (arrived) {
          answer.classList.add('flash')
          setTimeout(() => answer.classList.remove('flash'), 1200)
        }
      })
    </script>
  </body>
</html>
04 / Follow the scroll

Show progress and why it ended

Follow the scrollLive preview

createScrollTo shares the duration and the callbacks: onProgress fills the bar, onEnd says why the scroll ended. Stop it with cancel(), take over with the wheel or touch, or let an AbortSignal give up after one second.

Ready

Part 1

Use the wheel or touch here while it moves to interrupt it.

Part 2

Use the wheel or touch here while it moves to interrupt it.

Part 3

Use the wheel or touch here while it moves to interrupt it.

Part 4

Use the wheel or touch here while it moves to interrupt it.

Part 5

Use the wheel or touch here while it moves to interrupt it.

Part 6

Use the wheel or touch here while it moves to interrupt it.

Part 7

Use the wheel or touch here while it moves to interrupt it.

Part 8

Use the wheel or touch here while it moves to interrupt it.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Show progress and why it ended</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <div class="toolbar">
        <button type="button" id="go">Scroll to the end</button>
        <button type="button" id="stop">Stop</button>
        <button type="button" id="give-up">Give up after 1 s</button>
        <button type="button" id="back">Back to top</button>
        <output id="status" aria-live="polite">Ready</output>
        <div class="progress" aria-hidden="true"><span id="bar"></span></div>
      </div>
      <main>
        <section id="part-1"><h2>Part 1</h2></section>
        <section id="part-2"><h2>Part 2</h2></section>
        <section id="part-3"><h2>Part 3</h2></section>
        <section id="part-4"><h2>Part 4</h2></section>
        <section id="part-5"><h2>Part 5</h2></section>
        <section id="part-6"><h2>Part 6</h2></section>
        <section id="part-7"><h2>Part 7</h2></section>
        <section id="part-8"><h2>Part 8</h2></section>
      </main>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
body {
  margin: 0;
  font-family: system-ui, sans-serif;
}
.toolbar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  padding: 0.75rem 1rem;
  background: #fff;
  border-bottom: 1px solid #ddd;
}
.progress {
  position: absolute;
  inset: auto 0 0;
  height: 3px;
}
#bar {
  display: block;
  height: 100%;
  background: #78350f;
  transform: scaleX(0);
  transform-origin: left;
}
main {
  padding: 4rem 1rem 0;
}
section {
  min-height: 80vh;
  border-top: 1px solid #ddd;
}

JavaScript

main.js
const bar = document.querySelector('#bar')
const status = document.querySelector('#status')

// Defaults shared by every scroll below, callbacks included.
const scroller = ScrollTo.createScrollTo({
  duration: 2500,
  onStart: () => {
    status.textContent = 'Scrolling…'
  },
  onProgress: ({ eased }) => {
    bar.style.transform = `scaleX(${eased})`
  },
  onEnd: ({ reason }) => {
    status.textContent = `Ended: ${reason}`
  },
})

document.querySelector('#go').addEventListener('click', () => scroller.to('#part-8'))
document.querySelector('#stop').addEventListener('click', () => scroller.cancel())
document
  .querySelector('#give-up')
  .addEventListener('click', () => scroller.to('#part-8', { signal: AbortSignal.timeout(1000) }))
document
  .querySelector('#back')
  .addEventListener('click', () => scroller.to('top', { duration: 600 }))

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Show progress and why it ended</title>
    <style>
      body {
        margin: 0;
        font-family: system-ui, sans-serif;
      }
      .toolbar {
        position: fixed;
        inset: 0 0 auto;
        z-index: 1;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        align-items: center;
        padding: 0.75rem 1rem;
        background: #fff;
        border-bottom: 1px solid #ddd;
      }
      .progress {
        position: absolute;
        inset: auto 0 0;
        height: 3px;
      }
      #bar {
        display: block;
        height: 100%;
        background: #78350f;
        transform: scaleX(0);
        transform-origin: left;
      }
      main {
        padding: 4rem 1rem 0;
      }
      section {
        min-height: 80vh;
        border-top: 1px solid #ddd;
      }
    </style>
  </head>
  <body>
    <main>
      <div class="toolbar">
        <button type="button" id="go">Scroll to the end</button>
        <button type="button" id="stop">Stop</button>
        <button type="button" id="give-up">Give up after 1 s</button>
        <button type="button" id="back">Back to top</button>
        <output id="status" aria-live="polite">Ready</output>
        <div class="progress" aria-hidden="true"><span id="bar"></span></div>
      </div>
      <main>
        <section id="part-1"><h2>Part 1</h2></section>
        <section id="part-2"><h2>Part 2</h2></section>
        <section id="part-3"><h2>Part 3</h2></section>
        <section id="part-4"><h2>Part 4</h2></section>
        <section id="part-5"><h2>Part 5</h2></section>
        <section id="part-6"><h2>Part 6</h2></section>
        <section id="part-7"><h2>Part 7</h2></section>
        <section id="part-8"><h2>Part 8</h2></section>
      </main>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
    <script>
      const bar = document.querySelector('#bar')
      const status = document.querySelector('#status')

      // Defaults shared by every scroll below, callbacks included.
      const scroller = ScrollTo.createScrollTo({
        duration: 2500,
        onStart: () => {
          status.textContent = 'Scrolling…'
        },
        onProgress: ({ eased }) => {
          bar.style.transform = `scaleX(${eased})`
        },
        onEnd: ({ reason }) => {
          status.textContent = `Ended: ${reason}`
        },
      })

      document.querySelector('#go').addEventListener('click', () => scroller.to('#part-8'))
      document.querySelector('#stop').addEventListener('click', () => scroller.cancel())
      document
        .querySelector('#give-up')
        .addEventListener('click', () =>
          scroller.to('#part-8', { signal: AbortSignal.timeout(1000) })
        )
      document
        .querySelector('#back')
        .addEventListener('click', () => scroller.to('top', { duration: 600 }))
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Show progress and why it ended</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <main>
      <div class="toolbar">
        <button type="button" id="go">Scroll to the end</button>
        <button type="button" id="stop">Stop</button>
        <button type="button" id="give-up">Give up after 1 s</button>
        <button type="button" id="back">Back to top</button>
        <output id="status" aria-live="polite">Ready</output>
        <div class="progress" aria-hidden="true"><span id="bar"></span></div>
      </div>
      <main>
        <section id="part-1"><h2>Part 1</h2></section>
        <section id="part-2"><h2>Part 2</h2></section>
        <section id="part-3"><h2>Part 3</h2></section>
        <section id="part-4"><h2>Part 4</h2></section>
        <section id="part-5"><h2>Part 5</h2></section>
        <section id="part-6"><h2>Part 6</h2></section>
        <section id="part-7"><h2>Part 7</h2></section>
        <section id="part-8"><h2>Part 8</h2></section>
      </main>
    </main>
    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
body {
  margin: 0;
  font-family: system-ui, sans-serif;
}
.toolbar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  padding: 0.75rem 1rem;
  background: #fff;
  border-bottom: 1px solid #ddd;
}
.progress {
  position: absolute;
  inset: auto 0 0;
  height: 3px;
}
#bar {
  display: block;
  height: 100%;
  background: #78350f;
  transform: scaleX(0);
  transform-origin: left;
}
main {
  padding: 4rem 1rem 0;
}
section {
  min-height: 80vh;
  border-top: 1px solid #ddd;
}

JavaScript

main.js
import { createScrollTo } from '@samline/scroll-to'

const bar = document.querySelector('#bar')
const status = document.querySelector('#status')

// Defaults shared by every scroll below, callbacks included.
const scroller = createScrollTo({
  duration: 2500,
  onStart: () => {
    status.textContent = 'Scrolling…'
  },
  onProgress: ({ eased }) => {
    bar.style.transform = `scaleX(${eased})`
  },
  onEnd: ({ reason }) => {
    status.textContent = `Ended: ${reason}`
  },
})

document.querySelector('#go').addEventListener('click', () => scroller.to('#part-8'))
document.querySelector('#stop').addEventListener('click', () => scroller.cancel())
document
  .querySelector('#give-up')
  .addEventListener('click', () => scroller.to('#part-8', { signal: AbortSignal.timeout(1000) }))
document
  .querySelector('#back')
  .addEventListener('click', () => scroller.to('top', { duration: 600 }))

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Show progress and why it ended</title>
    <style>
      body {
        margin: 0;
        font-family: system-ui, sans-serif;
      }
      .toolbar {
        position: fixed;
        inset: 0 0 auto;
        z-index: 1;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        align-items: center;
        padding: 0.75rem 1rem;
        background: #fff;
        border-bottom: 1px solid #ddd;
      }
      .progress {
        position: absolute;
        inset: auto 0 0;
        height: 3px;
      }
      #bar {
        display: block;
        height: 100%;
        background: #78350f;
        transform: scaleX(0);
        transform-origin: left;
      }
      main {
        padding: 4rem 1rem 0;
      }
      section {
        min-height: 80vh;
        border-top: 1px solid #ddd;
      }
    </style>
  </head>
  <body>
    <main>
      <div class="toolbar">
        <button type="button" id="go">Scroll to the end</button>
        <button type="button" id="stop">Stop</button>
        <button type="button" id="give-up">Give up after 1 s</button>
        <button type="button" id="back">Back to top</button>
        <output id="status" aria-live="polite">Ready</output>
        <div class="progress" aria-hidden="true"><span id="bar"></span></div>
      </div>
      <main>
        <section id="part-1"><h2>Part 1</h2></section>
        <section id="part-2"><h2>Part 2</h2></section>
        <section id="part-3"><h2>Part 3</h2></section>
        <section id="part-4"><h2>Part 4</h2></section>
        <section id="part-5"><h2>Part 5</h2></section>
        <section id="part-6"><h2>Part 6</h2></section>
        <section id="part-7"><h2>Part 7</h2></section>
        <section id="part-8"><h2>Part 8</h2></section>
      </main>
    </main>
    <script type="module">
      import { createScrollTo } from '@samline/scroll-to'

      const bar = document.querySelector('#bar')
      const status = document.querySelector('#status')

      // Defaults shared by every scroll below, callbacks included.
      const scroller = createScrollTo({
        duration: 2500,
        onStart: () => {
          status.textContent = 'Scrolling…'
        },
        onProgress: ({ eased }) => {
          bar.style.transform = `scaleX(${eased})`
        },
        onEnd: ({ reason }) => {
          status.textContent = `Ended: ${reason}`
        },
      })

      document.querySelector('#go').addEventListener('click', () => scroller.to('#part-8'))
      document.querySelector('#stop').addEventListener('click', () => scroller.cancel())
      document
        .querySelector('#give-up')
        .addEventListener('click', () =>
          scroller.to('#part-8', { signal: AbortSignal.timeout(1000) })
        )
      document
        .querySelector('#back')
        .addEventListener('click', () => scroller.to('top', { duration: 600 }))
    </script>
  </body>
</html>
05 / Events and state

React from HTML and CSS

Events and stateLive preview

While a link scrolls, it and the page carry data-scroll-to-state="scrolling", so CSS can style them. Every scroll ends with a bubbling scroll-to:end event: one listener on the document logs where each one went and why it ended.

IntroPlansContact

Intro

While it scrolls, the link and the box carry data-scroll-to-state.

Plans

While it scrolls, the link and the box carry data-scroll-to-state.

Contact

While it scrolls, the link and the box carry data-scroll-to-state.

scroll-to:end

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>React from HTML and CSS</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#intro">Intro</a>
              <a href="#plans">Plans</a>
              <a href="#contact">Contact</a>
            </nav>
            <ol id="log" aria-live="polite"></ol>
          </header>
          <main>
            <section id="intro">
              <h2>Intro</h2>
            </section>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 1;
      display: flex;
      justify-content: space-between;
      gap: 1rem;
      padding: 1rem 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
      transition: border-color 0.2s;
    }
    nav {
      display: flex;
      gap: 1rem;
    }
    #log {
      margin: 0;
      font-size: 0.875rem;
      color: #555;
    }
    main {
      padding: 5rem 1.5rem 50vh;
    }
    section {
      min-height: 80vh;
      border-top: 1px solid #ddd;
    }
    section:focus {
      outline: none;
    }
    /* While a scroll runs: the page, and the link that started it. */
    html[data-scroll-to-state='scrolling'] header {
      border-color: #78350f;
    }
    a[data-scroll-to-state='scrolling'] {
      color: #78350f;
      text-decoration-style: wavy;
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ offset: '#header', duration: 900 })
    
    const log = document.querySelector('#log')
    
    document.addEventListener('scroll-to:end', event => {
      const { target, reason } = event.detail
      const item = document.createElement('li')
      item.textContent = `${target?.id ?? 'top'}: ${reason}`
      log.prepend(item)
      while (log.children.length > 3) log.lastElementChild.remove()
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>React from HTML and CSS</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 1;
            display: flex;
            justify-content: space-between;
            gap: 1rem;
            padding: 1rem 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
            transition: border-color 0.2s;
          }
          nav {
            display: flex;
            gap: 1rem;
          }
          #log {
            margin: 0;
            font-size: 0.875rem;
            color: #555;
          }
          main {
            padding: 5rem 1.5rem 50vh;
          }
          section {
            min-height: 80vh;
            border-top: 1px solid #ddd;
          }
          section:focus {
            outline: none;
          }
          /* While a scroll runs: the page, and the link that started it. */
          html[data-scroll-to-state='scrolling'] header {
            border-color: #78350f;
          }
          a[data-scroll-to-state='scrolling'] {
            color: #78350f;
            text-decoration-style: wavy;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#intro">Intro</a>
              <a href="#plans">Plans</a>
              <a href="#contact">Contact</a>
            </nav>
            <ol id="log" aria-live="polite"></ol>
          </header>
          <main>
            <section id="intro">
              <h2>Intro</h2>
            </section>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ offset: '#header', duration: 900 })
    
          const log = document.querySelector('#log')
    
          document.addEventListener('scroll-to:end', event => {
            const { target, reason } = event.detail
            const item = document.createElement('li')
            item.textContent = `${target?.id ?? 'top'}: ${reason}`
            log.prepend(item)
            while (log.children.length > 3) log.lastElementChild.remove()
          })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>React from HTML and CSS</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#intro">Intro</a>
              <a href="#plans">Plans</a>
              <a href="#contact">Contact</a>
            </nav>
            <ol id="log" aria-live="polite"></ol>
          </header>
          <main>
            <section id="intro">
              <h2>Intro</h2>
            </section>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 1;
      display: flex;
      justify-content: space-between;
      gap: 1rem;
      padding: 1rem 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
      transition: border-color 0.2s;
    }
    nav {
      display: flex;
      gap: 1rem;
    }
    #log {
      margin: 0;
      font-size: 0.875rem;
      color: #555;
    }
    main {
      padding: 5rem 1.5rem 50vh;
    }
    section {
      min-height: 80vh;
      border-top: 1px solid #ddd;
    }
    section:focus {
      outline: none;
    }
    /* While a scroll runs: the page, and the link that started it. */
    html[data-scroll-to-state='scrolling'] header {
      border-color: #78350f;
    }
    a[data-scroll-to-state='scrolling'] {
      color: #78350f;
      text-decoration-style: wavy;
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ offset: '#header', duration: 900 })
    
    const log = document.querySelector('#log')
    
    document.addEventListener('scroll-to:end', event => {
      const { target, reason } = event.detail
      const item = document.createElement('li')
      item.textContent = `${target?.id ?? 'top'}: ${reason}`
      log.prepend(item)
      while (log.children.length > 3) log.lastElementChild.remove()
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>React from HTML and CSS</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 1;
            display: flex;
            justify-content: space-between;
            gap: 1rem;
            padding: 1rem 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
            transition: border-color 0.2s;
          }
          nav {
            display: flex;
            gap: 1rem;
          }
          #log {
            margin: 0;
            font-size: 0.875rem;
            color: #555;
          }
          main {
            padding: 5rem 1.5rem 50vh;
          }
          section {
            min-height: 80vh;
            border-top: 1px solid #ddd;
          }
          section:focus {
            outline: none;
          }
          /* While a scroll runs: the page, and the link that started it. */
          html[data-scroll-to-state='scrolling'] header {
            border-color: #78350f;
          }
          a[data-scroll-to-state='scrolling'] {
            color: #78350f;
            text-decoration-style: wavy;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#intro">Intro</a>
              <a href="#plans">Plans</a>
              <a href="#contact">Contact</a>
            </nav>
            <ol id="log" aria-live="polite"></ol>
          </header>
          <main>
            <section id="intro">
              <h2>Intro</h2>
            </section>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ offset: '#header', duration: 900 })
    
          const log = document.querySelector('#log')
    
          document.addEventListener('scroll-to:end', event => {
            const { target, reason } = event.detail
            const item = document.createElement('li')
            item.textContent = `${target?.id ?? 'top'}: ${reason}`
            log.prepend(item)
            while (log.children.length > 3) log.lastElementChild.remove()
          })
        </script>
      </body>
    </html>
    
    06 / Stacked bars

    Clear a header and the bar stuck below it

    Stacked barsLive preview

    offset: 'auto' finds every fixed and sticky bar stuck at the top and keeps space for all of them, measured on every frame. Hide the notice bar and the links land higher, with no change to the code.

    PlansFAQContact

    Intro

    Scroll down: the notice sticks below the bar.

    Free shipping this week

    Plans

    Lands below the bar and the notice, or just the bar when the notice is hidden.

    FAQ

    Lands below the bar and the notice, or just the bar when the notice is hidden.

    Contact

    Lands below the bar and the notice, or just the bar when the notice is hidden.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Clear a header and the bar stuck below it</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#plans">Plans</a>
              <a href="#faq">FAQ</a>
              <a href="#contact">Contact</a>
            </nav>
            <button type="button" id="toggle" aria-pressed="true">Notice bar</button>
          </header>
          <main>
            <section id="intro"><h2>Intro</h2></section>
            <div class="notice" id="notice">Free shipping this week</div>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="faq">
              <h2>Faq</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 2;
      box-sizing: border-box;
      height: 3.5rem;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    nav {
      display: flex;
      gap: 1rem;
    }
    main {
      padding-top: 3.5rem;
      padding-bottom: 50vh;
    }
    .notice {
      position: sticky;
      top: 3.5rem;
      z-index: 1;
      padding: 0.5rem 1.5rem;
      background: #fff6c2;
    }
    section {
      min-height: 70vh;
      padding: 0 1.5rem;
      border-top: 1px solid #ddd;
    }
    section:focus {
      outline: none;
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ offset: 'auto' })
    
    const toggle = document.querySelector('#toggle')
    const notice = document.querySelector('#notice')
    
    toggle.addEventListener('click', () => {
      notice.hidden = !notice.hidden
      toggle.setAttribute('aria-pressed', String(!notice.hidden))
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Clear a header and the bar stuck below it</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 2;
            box-sizing: border-box;
            height: 3.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          nav {
            display: flex;
            gap: 1rem;
          }
          main {
            padding-top: 3.5rem;
            padding-bottom: 50vh;
          }
          .notice {
            position: sticky;
            top: 3.5rem;
            z-index: 1;
            padding: 0.5rem 1.5rem;
            background: #fff6c2;
          }
          section {
            min-height: 70vh;
            padding: 0 1.5rem;
            border-top: 1px solid #ddd;
          }
          section:focus {
            outline: none;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#plans">Plans</a>
              <a href="#faq">FAQ</a>
              <a href="#contact">Contact</a>
            </nav>
            <button type="button" id="toggle" aria-pressed="true">Notice bar</button>
          </header>
          <main>
            <section id="intro"><h2>Intro</h2></section>
            <div class="notice" id="notice">Free shipping this week</div>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="faq">
              <h2>Faq</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ offset: 'auto' })
    
          const toggle = document.querySelector('#toggle')
          const notice = document.querySelector('#notice')
    
          toggle.addEventListener('click', () => {
            notice.hidden = !notice.hidden
            toggle.setAttribute('aria-pressed', String(!notice.hidden))
          })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Clear a header and the bar stuck below it</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#plans">Plans</a>
              <a href="#faq">FAQ</a>
              <a href="#contact">Contact</a>
            </nav>
            <button type="button" id="toggle" aria-pressed="true">Notice bar</button>
          </header>
          <main>
            <section id="intro"><h2>Intro</h2></section>
            <div class="notice" id="notice">Free shipping this week</div>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="faq">
              <h2>Faq</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 2;
      box-sizing: border-box;
      height: 3.5rem;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    nav {
      display: flex;
      gap: 1rem;
    }
    main {
      padding-top: 3.5rem;
      padding-bottom: 50vh;
    }
    .notice {
      position: sticky;
      top: 3.5rem;
      z-index: 1;
      padding: 0.5rem 1.5rem;
      background: #fff6c2;
    }
    section {
      min-height: 70vh;
      padding: 0 1.5rem;
      border-top: 1px solid #ddd;
    }
    section:focus {
      outline: none;
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ offset: 'auto' })
    
    const toggle = document.querySelector('#toggle')
    const notice = document.querySelector('#notice')
    
    toggle.addEventListener('click', () => {
      notice.hidden = !notice.hidden
      toggle.setAttribute('aria-pressed', String(!notice.hidden))
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Clear a header and the bar stuck below it</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 2;
            box-sizing: border-box;
            height: 3.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          nav {
            display: flex;
            gap: 1rem;
          }
          main {
            padding-top: 3.5rem;
            padding-bottom: 50vh;
          }
          .notice {
            position: sticky;
            top: 3.5rem;
            z-index: 1;
            padding: 0.5rem 1.5rem;
            background: #fff6c2;
          }
          section {
            min-height: 70vh;
            padding: 0 1.5rem;
            border-top: 1px solid #ddd;
          }
          section:focus {
            outline: none;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <nav>
              <a href="#plans">Plans</a>
              <a href="#faq">FAQ</a>
              <a href="#contact">Contact</a>
            </nav>
            <button type="button" id="toggle" aria-pressed="true">Notice bar</button>
          </header>
          <main>
            <section id="intro"><h2>Intro</h2></section>
            <div class="notice" id="notice">Free shipping this week</div>
            <section id="plans">
              <h2>Plans</h2>
            </section>
            <section id="faq">
              <h2>Faq</h2>
            </section>
            <section id="contact">
              <h2>Contact</h2>
            </section>
          </main>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ offset: 'auto' })
    
          const toggle = document.querySelector('#toggle')
          const notice = document.querySelector('#notice')
    
          toggle.addEventListener('click', () => {
            notice.hidden = !notice.hidden
            toggle.setAttribute('aria-pressed', String(!notice.hidden))
          })
        </script>
      </body>
    </html>
    
    07 / Page a strip

    Page through a strip, with any easing

    Page a stripLive preview

    { left: '+=100%' } moves the strip by its own width, wherever it is. speed keeps the pace steady and maxDuration caps it; pick an easing, including a CSS cubic-bezier() curve.

    Card 1
    Card 2
    Card 3
    Card 4
    Card 5
    Card 6
    Card 7
    Card 8
    Card 9
    Card 10
    Card 11
    Card 12
    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Page through a strip, with any easing</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <div class="pager">
            <button type="button" id="prev">Previous</button>
            <label
              >Easing
              <select id="easing">
                <option value="ease-in-out">Ease in-out</option>
                <option value="ease-out-quint">Ease out quint</option>
                <option value="ease-out-back">Ease out back</option>
                <option value="cubic-bezier(0.2, 0.8, 0.2, 1)">cubic-bezier(0.2, 0.8, 0.2, 1)</option>
                <option value="linear">Linear</option>
              </select>
            </label>
            <button type="button" id="next">Next</button>
          </div>
          <div class="strip" id="strip">
            <article>Card 1</article>
            <article>Card 2</article>
            <article>Card 3</article>
            <article>Card 4</article>
            <article>Card 5</article>
            <article>Card 6</article>
            <article>Card 7</article>
            <article>Card 8</article>
            <article>Card 9</article>
            <article>Card 10</article>
            <article>Card 11</article>
            <article>Card 12</article>
          </div>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    .pager {
      display: flex;
      flex-wrap: wrap;
      gap: 0.75rem;
      align-items: center;
      margin-bottom: 0.75rem;
    }
    .strip {
      display: flex;
      gap: 1rem;
      max-width: 40rem;
      overflow-x: auto;
      padding: 0.5rem;
      border: 1px solid #ddd;
    }
    .strip article {
      flex: 0 0 12rem;
      height: 8rem;
      display: grid;
      place-items: center;
      border-radius: 0.5rem;
      background: #f4ede6;
    }
    

    JavaScript

    main.js
    const strip = document.querySelector('#strip')
    const easing = document.querySelector('#easing')
    
    const page = direction =>
      ScrollTo.scrollTo(
        { left: `${direction}=100%` },
        { container: strip, easing: easing.value, speed: 1500, maxDuration: 900 }
      )
    
    document.querySelector('#prev').addEventListener('click', () => page('-'))
    document.querySelector('#next').addEventListener('click', () => page('+'))
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Page through a strip, with any easing</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          .pager {
            display: flex;
            flex-wrap: wrap;
            gap: 0.75rem;
            align-items: center;
            margin-bottom: 0.75rem;
          }
          .strip {
            display: flex;
            gap: 1rem;
            max-width: 40rem;
            overflow-x: auto;
            padding: 0.5rem;
            border: 1px solid #ddd;
          }
          .strip article {
            flex: 0 0 12rem;
            height: 8rem;
            display: grid;
            place-items: center;
            border-radius: 0.5rem;
            background: #f4ede6;
          }
        </style>
      </head>
      <body>
        <main>
          <div class="pager">
            <button type="button" id="prev">Previous</button>
            <label
              >Easing
              <select id="easing">
                <option value="ease-in-out">Ease in-out</option>
                <option value="ease-out-quint">Ease out quint</option>
                <option value="ease-out-back">Ease out back</option>
                <option value="cubic-bezier(0.2, 0.8, 0.2, 1)">cubic-bezier(0.2, 0.8, 0.2, 1)</option>
                <option value="linear">Linear</option>
              </select>
            </label>
            <button type="button" id="next">Next</button>
          </div>
          <div class="strip" id="strip">
            <article>Card 1</article>
            <article>Card 2</article>
            <article>Card 3</article>
            <article>Card 4</article>
            <article>Card 5</article>
            <article>Card 6</article>
            <article>Card 7</article>
            <article>Card 8</article>
            <article>Card 9</article>
            <article>Card 10</article>
            <article>Card 11</article>
            <article>Card 12</article>
          </div>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          const strip = document.querySelector('#strip')
          const easing = document.querySelector('#easing')
    
          const page = direction =>
            ScrollTo.scrollTo(
              { left: `${direction}=100%` },
              { container: strip, easing: easing.value, speed: 1500, maxDuration: 900 }
            )
    
          document.querySelector('#prev').addEventListener('click', () => page('-'))
          document.querySelector('#next').addEventListener('click', () => page('+'))
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Page through a strip, with any easing</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <div class="pager">
            <button type="button" id="prev">Previous</button>
            <label
              >Easing
              <select id="easing">
                <option value="ease-in-out">Ease in-out</option>
                <option value="ease-out-quint">Ease out quint</option>
                <option value="ease-out-back">Ease out back</option>
                <option value="cubic-bezier(0.2, 0.8, 0.2, 1)">cubic-bezier(0.2, 0.8, 0.2, 1)</option>
                <option value="linear">Linear</option>
              </select>
            </label>
            <button type="button" id="next">Next</button>
          </div>
          <div class="strip" id="strip">
            <article>Card 1</article>
            <article>Card 2</article>
            <article>Card 3</article>
            <article>Card 4</article>
            <article>Card 5</article>
            <article>Card 6</article>
            <article>Card 7</article>
            <article>Card 8</article>
            <article>Card 9</article>
            <article>Card 10</article>
            <article>Card 11</article>
            <article>Card 12</article>
          </div>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    .pager {
      display: flex;
      flex-wrap: wrap;
      gap: 0.75rem;
      align-items: center;
      margin-bottom: 0.75rem;
    }
    .strip {
      display: flex;
      gap: 1rem;
      max-width: 40rem;
      overflow-x: auto;
      padding: 0.5rem;
      border: 1px solid #ddd;
    }
    .strip article {
      flex: 0 0 12rem;
      height: 8rem;
      display: grid;
      place-items: center;
      border-radius: 0.5rem;
      background: #f4ede6;
    }
    

    JavaScript

    main.js
    import { scrollTo } from '@samline/scroll-to'
    
    const strip = document.querySelector('#strip')
    const easing = document.querySelector('#easing')
    
    const page = direction =>
      scrollTo(
        { left: `${direction}=100%` },
        { container: strip, easing: easing.value, speed: 1500, maxDuration: 900 }
      )
    
    document.querySelector('#prev').addEventListener('click', () => page('-'))
    document.querySelector('#next').addEventListener('click', () => page('+'))
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Page through a strip, with any easing</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          .pager {
            display: flex;
            flex-wrap: wrap;
            gap: 0.75rem;
            align-items: center;
            margin-bottom: 0.75rem;
          }
          .strip {
            display: flex;
            gap: 1rem;
            max-width: 40rem;
            overflow-x: auto;
            padding: 0.5rem;
            border: 1px solid #ddd;
          }
          .strip article {
            flex: 0 0 12rem;
            height: 8rem;
            display: grid;
            place-items: center;
            border-radius: 0.5rem;
            background: #f4ede6;
          }
        </style>
      </head>
      <body>
        <main>
          <div class="pager">
            <button type="button" id="prev">Previous</button>
            <label
              >Easing
              <select id="easing">
                <option value="ease-in-out">Ease in-out</option>
                <option value="ease-out-quint">Ease out quint</option>
                <option value="ease-out-back">Ease out back</option>
                <option value="cubic-bezier(0.2, 0.8, 0.2, 1)">cubic-bezier(0.2, 0.8, 0.2, 1)</option>
                <option value="linear">Linear</option>
              </select>
            </label>
            <button type="button" id="next">Next</button>
          </div>
          <div class="strip" id="strip">
            <article>Card 1</article>
            <article>Card 2</article>
            <article>Card 3</article>
            <article>Card 4</article>
            <article>Card 5</article>
            <article>Card 6</article>
            <article>Card 7</article>
            <article>Card 8</article>
            <article>Card 9</article>
            <article>Card 10</article>
            <article>Card 11</article>
            <article>Card 12</article>
          </div>
        </main>
        <script type="module">
          import { scrollTo } from '@samline/scroll-to'
    
          const strip = document.querySelector('#strip')
          const easing = document.querySelector('#easing')
    
          const page = direction =>
            scrollTo(
              { left: `${direction}=100%` },
              { container: strip, easing: easing.value, speed: 1500, maxDuration: 900 }
            )
    
          document.querySelector('#prev').addEventListener('click', () => page('-'))
          document.querySelector('#next').addEventListener('click', () => page('+'))
        </script>
      </body>
    </html>
    
    08 / Next and previous

    Step through sections

    Next and previousLive preview

    data-scroll-to="next" and "prev" find the next or previous section below the header. Press Next several times quickly: the presses add up. data-scroll-to-focus="false" keeps focus on the button, so the keyboard can keep pressing it.

    Chapter 1

    Press Next a few times quickly: each press counts.

    Chapter 2

    Press Next a few times quickly: each press counts.

    Chapter 3

    Press Next a few times quickly: each press counts.

    Chapter 4

    Press Next a few times quickly: each press counts.

    Chapter 5

    Press Next a few times quickly: each press counts.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Step through sections</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <button type="button" data-scroll-to="prev" data-scroll-to-focus="false">
              Previous chapter
            </button>
            <button type="button" data-scroll-to="next" data-scroll-to-focus="false">
              Next chapter
            </button>
          </header>
          <main class="chapters">
            <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>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 1;
      box-sizing: border-box;
      height: 3.5rem;
      display: flex;
      gap: 0.5rem;
      align-items: center;
      padding: 0 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    .chapters {
      padding: 3.5rem 1.5rem 0;
    }
    section {
      min-height: 90vh;
      border-top: 1px solid #ddd;
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ offset: 'auto', hash: false })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Step through sections</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 1;
            box-sizing: border-box;
            height: 3.5rem;
            display: flex;
            gap: 0.5rem;
            align-items: center;
            padding: 0 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          .chapters {
            padding: 3.5rem 1.5rem 0;
          }
          section {
            min-height: 90vh;
            border-top: 1px solid #ddd;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <button type="button" data-scroll-to="prev" data-scroll-to-focus="false">
              Previous chapter
            </button>
            <button type="button" data-scroll-to="next" data-scroll-to-focus="false">
              Next chapter
            </button>
          </header>
          <main class="chapters">
            <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>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ offset: 'auto', hash: false })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Step through sections</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <button type="button" data-scroll-to="prev" data-scroll-to-focus="false">
              Previous chapter
            </button>
            <button type="button" data-scroll-to="next" data-scroll-to-focus="false">
              Next chapter
            </button>
          </header>
          <main class="chapters">
            <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>
          </main>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      margin: 0;
      font-family: system-ui, sans-serif;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      z-index: 1;
      box-sizing: border-box;
      height: 3.5rem;
      display: flex;
      gap: 0.5rem;
      align-items: center;
      padding: 0 1.5rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    .chapters {
      padding: 3.5rem 1.5rem 0;
    }
    section {
      min-height: 90vh;
      border-top: 1px solid #ddd;
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ offset: 'auto', hash: false })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Step through sections</title>
        <style>
          body {
            margin: 0;
            font-family: system-ui, sans-serif;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            z-index: 1;
            box-sizing: border-box;
            height: 3.5rem;
            display: flex;
            gap: 0.5rem;
            align-items: center;
            padding: 0 1.5rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          .chapters {
            padding: 3.5rem 1.5rem 0;
          }
          section {
            min-height: 90vh;
            border-top: 1px solid #ddd;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <button type="button" data-scroll-to="prev" data-scroll-to-focus="false">
              Previous chapter
            </button>
            <button type="button" data-scroll-to="next" data-scroll-to-focus="false">
              Next chapter
            </button>
          </header>
          <main class="chapters">
            <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>
          </main>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ offset: 'auto', hash: false })
        </script>
      </body>
    </html>
    
    09 / Hidden answers

    Links that open the answer first

    Hidden answersLive preview

    The answers sit inside closed <details> and a hidden="until-found" panel. Each link opens what hides its answer, then scrolls and moves focus there, like the browser does for fragment links. The panel gets a beforematch event, so your own state can follow.

    How do refunds work?When does it ship?Is there a warranty?

    The answers are further down, closed.

    Payments

    Refunds

    Refunds reach your card within five days.

    Delivery

    Shipping

    Orders ship the next working day.

    Warranty

    Two years, parts and labor.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Links that open the answer first</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav>
            <a href="#refunds">How do refunds work?</a>
            <a href="#shipping">When does it ship?</a>
            <a href="#warranty" data-scroll-to-hash="replace">Is there a warranty?</a>
          </nav>
          <main>
            <details>
              <summary>Payments</summary>
              <h3 id="refunds">Refunds</h3>
              <p>Refunds reach your card within five days.</p>
            </details>
            <details>
              <summary>Delivery</summary>
              <h3 id="shipping">Shipping</h3>
              <p>Orders ship the next working day.</p>
            </details>
            <div id="warranty-panel" hidden="until-found">
              <h3 id="warranty">Warranty</h3>
              <p>Two years, parts and labor.</p>
            </div>
          </main>
          <p id="log" aria-live="polite"></p>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    nav {
      display: flex;
      flex-direction: column;
      gap: 0.5rem;
    }
    main {
      padding: 80vh 0;
    }
    details,
    #warranty-panel {
      margin-block: 30vh;
      padding: 1rem;
      border: 1px solid #ddd;
    }
    h3:focus {
      outline: 2px solid #78350f;
      outline-offset: 4px;
    }
    #log {
      position: fixed;
      bottom: 1rem;
      right: 1rem;
      margin: 0;
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ offset: 16 })
    
    const log = document.querySelector('#log')
    
    document.querySelector('#warranty-panel').addEventListener('beforematch', () => {
      log.textContent = 'The warranty panel opened'
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Links that open the answer first</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          nav {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
          }
          main {
            padding: 80vh 0;
          }
          details,
          #warranty-panel {
            margin-block: 30vh;
            padding: 1rem;
            border: 1px solid #ddd;
          }
          h3:focus {
            outline: 2px solid #78350f;
            outline-offset: 4px;
          }
          #log {
            position: fixed;
            bottom: 1rem;
            right: 1rem;
            margin: 0;
          }
        </style>
      </head>
      <body>
        <main>
          <nav>
            <a href="#refunds">How do refunds work?</a>
            <a href="#shipping">When does it ship?</a>
            <a href="#warranty" data-scroll-to-hash="replace">Is there a warranty?</a>
          </nav>
          <main>
            <details>
              <summary>Payments</summary>
              <h3 id="refunds">Refunds</h3>
              <p>Refunds reach your card within five days.</p>
            </details>
            <details>
              <summary>Delivery</summary>
              <h3 id="shipping">Shipping</h3>
              <p>Orders ship the next working day.</p>
            </details>
            <div id="warranty-panel" hidden="until-found">
              <h3 id="warranty">Warranty</h3>
              <p>Two years, parts and labor.</p>
            </div>
          </main>
          <p id="log" aria-live="polite"></p>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ offset: 16 })
    
          const log = document.querySelector('#log')
    
          document.querySelector('#warranty-panel').addEventListener('beforematch', () => {
            log.textContent = 'The warranty panel opened'
          })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Links that open the answer first</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav>
            <a href="#refunds">How do refunds work?</a>
            <a href="#shipping">When does it ship?</a>
            <a href="#warranty" data-scroll-to-hash="replace">Is there a warranty?</a>
          </nav>
          <main>
            <details>
              <summary>Payments</summary>
              <h3 id="refunds">Refunds</h3>
              <p>Refunds reach your card within five days.</p>
            </details>
            <details>
              <summary>Delivery</summary>
              <h3 id="shipping">Shipping</h3>
              <p>Orders ship the next working day.</p>
            </details>
            <div id="warranty-panel" hidden="until-found">
              <h3 id="warranty">Warranty</h3>
              <p>Two years, parts and labor.</p>
            </div>
          </main>
          <p id="log" aria-live="polite"></p>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    nav {
      display: flex;
      flex-direction: column;
      gap: 0.5rem;
    }
    main {
      padding: 80vh 0;
    }
    details,
    #warranty-panel {
      margin-block: 30vh;
      padding: 1rem;
      border: 1px solid #ddd;
    }
    h3:focus {
      outline: 2px solid #78350f;
      outline-offset: 4px;
    }
    #log {
      position: fixed;
      bottom: 1rem;
      right: 1rem;
      margin: 0;
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ offset: 16 })
    
    const log = document.querySelector('#log')
    
    document.querySelector('#warranty-panel').addEventListener('beforematch', () => {
      log.textContent = 'The warranty panel opened'
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Links that open the answer first</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          nav {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
          }
          main {
            padding: 80vh 0;
          }
          details,
          #warranty-panel {
            margin-block: 30vh;
            padding: 1rem;
            border: 1px solid #ddd;
          }
          h3:focus {
            outline: 2px solid #78350f;
            outline-offset: 4px;
          }
          #log {
            position: fixed;
            bottom: 1rem;
            right: 1rem;
            margin: 0;
          }
        </style>
      </head>
      <body>
        <main>
          <nav>
            <a href="#refunds">How do refunds work?</a>
            <a href="#shipping">When does it ship?</a>
            <a href="#warranty" data-scroll-to-hash="replace">Is there a warranty?</a>
          </nav>
          <main>
            <details>
              <summary>Payments</summary>
              <h3 id="refunds">Refunds</h3>
              <p>Refunds reach your card within five days.</p>
            </details>
            <details>
              <summary>Delivery</summary>
              <h3 id="shipping">Shipping</h3>
              <p>Orders ship the next working day.</p>
            </details>
            <div id="warranty-panel" hidden="until-found">
              <h3 id="warranty">Warranty</h3>
              <p>Two years, parts and labor.</p>
            </div>
          </main>
          <p id="log" aria-live="polite"></p>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ offset: 16 })
    
          const log = document.querySelector('#log')
    
          document.querySelector('#warranty-panel').addEventListener('beforematch', () => {
            log.textContent = 'The warranty panel opened'
          })
        </script>
      </body>
    </html>
    
    10 / Highlight

    Show where the visitor landed

    HighlightLive preview

    Each term link scrolls to its definition and marks it with data-scroll-to-arrived for a moment, so the eye finds it even when it stops in the middle of the screen. While the scroll runs, the link fills from --scroll-to-progress. Both are plain CSS hooks, and reduced motion keeps only the color.

    CacheLatencyThroughput
    Cache

    A copy kept close by, so the next read is fast.

    Latency

    How long one request waits for its answer.

    Throughput

    How many requests finish each second.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Show where the visitor landed</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav>
            <a href="#cache" data-scroll-to-block="center">Cache</a>
            <a href="#latency" data-scroll-to-block="center">Latency</a>
            <a href="#throughput" data-scroll-to-block="center">Throughput</a>
          </nav>
          <dl>
            <dt id="cache">Cache</dt>
            <dd>A copy kept close by, so the next read is fast.</dd>
            <dt id="latency">Latency</dt>
            <dd>How long one request waits for its answer.</dd>
            <dt id="throughput">Throughput</dt>
            <dd>How many requests finish each second.</dd>
          </dl>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    nav {
      position: fixed;
      top: 1rem;
      left: 1rem;
      display: flex;
      gap: 1rem;
    }
    nav a {
      background: linear-gradient(#fde68a, #fde68a) no-repeat left /
        calc(var(--scroll-to-progress, 0) * 100%) 100%;
    }
    dl {
      padding: 100vh 1rem;
    }
    dt {
      margin-top: 60vh;
      padding: 0.25rem 0.5rem;
      font-weight: 600;
      border-radius: 0.25rem;
    }
    dt:focus {
      outline: 2px solid #78350f;
      outline-offset: 2px;
    }
    dt[data-scroll-to-arrived] {
      background: #fde68a;
    }
    @media (prefers-reduced-motion: no-preference) {
      dt {
        transition: background-color 0.6s;
      }
      dt[data-scroll-to-arrived] {
        animation: pulse 0.6s 2;
      }
    }
    @keyframes pulse {
      50% {
        scale: 1.04;
      }
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ highlight: 1500, hash: false })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Show where the visitor landed</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          nav {
            position: fixed;
            top: 1rem;
            left: 1rem;
            display: flex;
            gap: 1rem;
          }
          nav a {
            background: linear-gradient(#fde68a, #fde68a) no-repeat left /
              calc(var(--scroll-to-progress, 0) * 100%) 100%;
          }
          dl {
            padding: 100vh 1rem;
          }
          dt {
            margin-top: 60vh;
            padding: 0.25rem 0.5rem;
            font-weight: 600;
            border-radius: 0.25rem;
          }
          dt:focus {
            outline: 2px solid #78350f;
            outline-offset: 2px;
          }
          dt[data-scroll-to-arrived] {
            background: #fde68a;
          }
          @media (prefers-reduced-motion: no-preference) {
            dt {
              transition: background-color 0.6s;
            }
            dt[data-scroll-to-arrived] {
              animation: pulse 0.6s 2;
            }
          }
          @keyframes pulse {
            50% {
              scale: 1.04;
            }
          }
        </style>
      </head>
      <body>
        <main>
          <nav>
            <a href="#cache" data-scroll-to-block="center">Cache</a>
            <a href="#latency" data-scroll-to-block="center">Latency</a>
            <a href="#throughput" data-scroll-to-block="center">Throughput</a>
          </nav>
          <dl>
            <dt id="cache">Cache</dt>
            <dd>A copy kept close by, so the next read is fast.</dd>
            <dt id="latency">Latency</dt>
            <dd>How long one request waits for its answer.</dd>
            <dt id="throughput">Throughput</dt>
            <dd>How many requests finish each second.</dd>
          </dl>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ highlight: 1500, hash: false })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Show where the visitor landed</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav>
            <a href="#cache" data-scroll-to-block="center">Cache</a>
            <a href="#latency" data-scroll-to-block="center">Latency</a>
            <a href="#throughput" data-scroll-to-block="center">Throughput</a>
          </nav>
          <dl>
            <dt id="cache">Cache</dt>
            <dd>A copy kept close by, so the next read is fast.</dd>
            <dt id="latency">Latency</dt>
            <dd>How long one request waits for its answer.</dd>
            <dt id="throughput">Throughput</dt>
            <dd>How many requests finish each second.</dd>
          </dl>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    nav {
      position: fixed;
      top: 1rem;
      left: 1rem;
      display: flex;
      gap: 1rem;
    }
    nav a {
      background: linear-gradient(#fde68a, #fde68a) no-repeat left /
        calc(var(--scroll-to-progress, 0) * 100%) 100%;
    }
    dl {
      padding: 100vh 1rem;
    }
    dt {
      margin-top: 60vh;
      padding: 0.25rem 0.5rem;
      font-weight: 600;
      border-radius: 0.25rem;
    }
    dt:focus {
      outline: 2px solid #78350f;
      outline-offset: 2px;
    }
    dt[data-scroll-to-arrived] {
      background: #fde68a;
    }
    @media (prefers-reduced-motion: no-preference) {
      dt {
        transition: background-color 0.6s;
      }
      dt[data-scroll-to-arrived] {
        animation: pulse 0.6s 2;
      }
    }
    @keyframes pulse {
      50% {
        scale: 1.04;
      }
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ highlight: 1500, hash: false })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Show where the visitor landed</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          nav {
            position: fixed;
            top: 1rem;
            left: 1rem;
            display: flex;
            gap: 1rem;
          }
          nav a {
            background: linear-gradient(#fde68a, #fde68a) no-repeat left /
              calc(var(--scroll-to-progress, 0) * 100%) 100%;
          }
          dl {
            padding: 100vh 1rem;
          }
          dt {
            margin-top: 60vh;
            padding: 0.25rem 0.5rem;
            font-weight: 600;
            border-radius: 0.25rem;
          }
          dt:focus {
            outline: 2px solid #78350f;
            outline-offset: 2px;
          }
          dt[data-scroll-to-arrived] {
            background: #fde68a;
          }
          @media (prefers-reduced-motion: no-preference) {
            dt {
              transition: background-color 0.6s;
            }
            dt[data-scroll-to-arrived] {
              animation: pulse 0.6s 2;
            }
          }
          @keyframes pulse {
            50% {
              scale: 1.04;
            }
          }
        </style>
      </head>
      <body>
        <main>
          <nav>
            <a href="#cache" data-scroll-to-block="center">Cache</a>
            <a href="#latency" data-scroll-to-block="center">Latency</a>
            <a href="#throughput" data-scroll-to-block="center">Throughput</a>
          </nav>
          <dl>
            <dt id="cache">Cache</dt>
            <dd>A copy kept close by, so the next read is fast.</dd>
            <dt id="latency">Latency</dt>
            <dd>How long one request waits for its answer.</dd>
            <dt id="throughput">Throughput</dt>
            <dd>How many requests finish each second.</dd>
          </dl>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ highlight: 1500, hash: false })
        </script>
      </body>
    </html>
    
    11 / Back to top

    A back-to-top button with a progress ring

    Back to topLive preview

    The button stays out of the way until the page is scrolled one screen down, then fades in. Its ring fills from --scroll-to-scrolled as you read. Pressing it scrolls to the top and moves focus there, so keyboard users do not stay on a button that hides. Without JavaScript it is a plain link to the top.

    Page 1

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Page 2

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Page 3

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Page 4

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Page 5

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Page 6

    Scroll down: the button appears after one screen, and its ring fills as you read.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A back-to-top button with a progress ring</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <main id="top">
            <h1>A long article</h1>
            <p>Scroll down: the button appears after one screen.</p>
          </main>
          <a class="back-to-top" href="#top" data-scroll-to-back-to-top aria-label="Back to top">
            <svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
              <path d="M12 19V5M5 12l7-7 7 7" fill="none" stroke="currentColor" stroke-width="2" />
            </svg>
          </a>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    main {
      min-height: 400vh;
      padding: 1rem;
    }
    .back-to-top {
      position: fixed;
      right: 1rem;
      bottom: 1rem;
      display: grid;
      place-items: center;
      width: 3rem;
      height: 3rem;
      border-radius: 50%;
      color: #78350f;
      background:
        radial-gradient(closest-side, #fff 85%, transparent 86%),
        conic-gradient(#78350f calc(var(--scroll-to-scrolled, 0) * 1turn), #e5e5e5 0);
    }
    /* Only hidden once the script says so: without JavaScript the link stays. */
    .back-to-top[data-scroll-to-visible='false'] {
      visibility: hidden;
      opacity: 0;
    }
    @media (prefers-reduced-motion: no-preference) {
      .back-to-top {
        transition:
          opacity 0.3s,
          visibility 0.3s;
      }
    }
    

    JavaScript

    main.js
    // The script turns [data-scroll-to-back-to-top] into a back-to-top button.
    // Its value is the threshold: empty for one screen, or pixels such as "600".
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A back-to-top button with a progress ring</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          main {
            min-height: 400vh;
            padding: 1rem;
          }
          .back-to-top {
            position: fixed;
            right: 1rem;
            bottom: 1rem;
            display: grid;
            place-items: center;
            width: 3rem;
            height: 3rem;
            border-radius: 50%;
            color: #78350f;
            background:
              radial-gradient(closest-side, #fff 85%, transparent 86%),
              conic-gradient(#78350f calc(var(--scroll-to-scrolled, 0) * 1turn), #e5e5e5 0);
          }
          /* Only hidden once the script says so: without JavaScript the link stays. */
          .back-to-top[data-scroll-to-visible='false'] {
            visibility: hidden;
            opacity: 0;
          }
          @media (prefers-reduced-motion: no-preference) {
            .back-to-top {
              transition:
                opacity 0.3s,
                visibility 0.3s;
            }
          }
        </style>
      </head>
      <body>
        <main>
          <main id="top">
            <h1>A long article</h1>
            <p>Scroll down: the button appears after one screen.</p>
          </main>
          <a class="back-to-top" href="#top" data-scroll-to-back-to-top aria-label="Back to top">
            <svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
              <path d="M12 19V5M5 12l7-7 7 7" fill="none" stroke="currentColor" stroke-width="2" />
            </svg>
          </a>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          // The script turns [data-scroll-to-back-to-top] into a back-to-top button.
          // Its value is the threshold: empty for one screen, or pixels such as "600".
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A back-to-top button with a progress ring</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <main id="top">
            <h1>A long article</h1>
            <p>Scroll down: the button appears after one screen.</p>
          </main>
          <a class="back-to-top" href="#top" data-scroll-to-back-to-top aria-label="Back to top">
            <svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
              <path d="M12 19V5M5 12l7-7 7 7" fill="none" stroke="currentColor" stroke-width="2" />
            </svg>
          </a>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
    }
    main {
      min-height: 400vh;
      padding: 1rem;
    }
    .back-to-top {
      position: fixed;
      right: 1rem;
      bottom: 1rem;
      display: grid;
      place-items: center;
      width: 3rem;
      height: 3rem;
      border-radius: 50%;
      color: #78350f;
      background:
        radial-gradient(closest-side, #fff 85%, transparent 86%),
        conic-gradient(#78350f calc(var(--scroll-to-scrolled, 0) * 1turn), #e5e5e5 0);
    }
    /* Only hidden once the script says so: without JavaScript the link stays. */
    .back-to-top[data-scroll-to-visible='false'] {
      visibility: hidden;
      opacity: 0;
    }
    @media (prefers-reduced-motion: no-preference) {
      .back-to-top {
        transition:
          opacity 0.3s,
          visibility 0.3s;
      }
    }
    

    JavaScript

    main.js
    import { backToTop } from '@samline/scroll-to'
    
    backToTop('.back-to-top', { target: '#top' })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A back-to-top button with a progress ring</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
          }
          main {
            min-height: 400vh;
            padding: 1rem;
          }
          .back-to-top {
            position: fixed;
            right: 1rem;
            bottom: 1rem;
            display: grid;
            place-items: center;
            width: 3rem;
            height: 3rem;
            border-radius: 50%;
            color: #78350f;
            background:
              radial-gradient(closest-side, #fff 85%, transparent 86%),
              conic-gradient(#78350f calc(var(--scroll-to-scrolled, 0) * 1turn), #e5e5e5 0);
          }
          /* Only hidden once the script says so: without JavaScript the link stays. */
          .back-to-top[data-scroll-to-visible='false'] {
            visibility: hidden;
            opacity: 0;
          }
          @media (prefers-reduced-motion: no-preference) {
            .back-to-top {
              transition:
                opacity 0.3s,
                visibility 0.3s;
            }
          }
        </style>
      </head>
      <body>
        <main>
          <main id="top">
            <h1>A long article</h1>
            <p>Scroll down: the button appears after one screen.</p>
          </main>
          <a class="back-to-top" href="#top" data-scroll-to-back-to-top aria-label="Back to top">
            <svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
              <path d="M12 19V5M5 12l7-7 7 7" fill="none" stroke="currentColor" stroke-width="2" />
            </svg>
          </a>
        </main>
        <script type="module">
          import { backToTop } from '@samline/scroll-to'
    
          backToTop('.back-to-top', { target: '#top' })
        </script>
      </body>
    </html>
    
    12 / Nested boxes

    A card in a strip, further down the page

    Nested boxesLive preview

    With container: 'auto', a link scrolls every box around its target, then the page, like scrollIntoView but with the offset, easing and focus. The strip snaps, and its snapping is paused while it moves, so it does not stutter. Switch it to right to left: inline start becomes the right edge.

    Card 2Card 7Card 11

    The shelf is further down. Its cards scroll sideways.

    Card 1

    Card 2

    Card 3

    Card 4

    Card 5

    Card 6

    Card 7

    Card 8

    Card 9

    Card 10

    Card 11

    Card 12

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A card in a strip, further down the page</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <a href="#card-2">Card 2</a>
            <a href="#card-7">Card 7</a>
            <a href="#card-11">Card 11</a>
            <button type="button" id="direction" aria-pressed="false">Right to left</button>
          </header>
          <main>
            <p class="intro">The shelf is further down. Its cards scroll sideways.</p>
            <section aria-labelledby="shelf-title">
              <h2 id="shelf-title">Shelf</h2>
              <div class="strip" id="strip" tabindex="0" aria-label="Cards">
                <article class="card" id="card-1">Card 1</article>
                <article class="card" id="card-2">Card 2</article>
                <article class="card" id="card-3">Card 3</article>
                <article class="card" id="card-4">Card 4</article>
                <article class="card" id="card-5">Card 5</article>
                <article class="card" id="card-6">Card 6</article>
                <article class="card" id="card-7">Card 7</article>
                <article class="card" id="card-8">Card 8</article>
                <article class="card" id="card-9">Card 9</article>
                <article class="card" id="card-10">Card 10</article>
                <article class="card" id="card-11">Card 11</article>
                <article class="card" id="card-12">Card 12</article>
              </div>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
      margin: 0;
    }
    html {
      scroll-padding-top: 4rem;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      display: flex;
      gap: 1rem;
      align-items: center;
      height: 3rem;
      padding: 0 1rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    main {
      padding: 4rem 1rem 60vh;
    }
    .intro {
      min-height: 120vh;
    }
    .strip {
      display: flex;
      gap: 1rem;
      max-width: 40rem;
      overflow-x: auto;
      padding: 1rem;
      scroll-snap-type: x mandatory;
      border: 1px solid #ddd;
    }
    .card {
      flex: 0 0 12rem;
      height: 8rem;
      padding: 1rem;
      scroll-snap-align: start;
      border-radius: 0.5rem;
      background: #fef3c7;
    }
    .card:focus {
      outline: 2px solid #78350f;
      outline-offset: 2px;
    }
    

    JavaScript

    main.js
    ScrollTo.auto({ container: 'auto', offset: '#header', inline: 'start', hash: false })
    
    const direction = document.querySelector('#direction')
    const strip = document.querySelector('#strip')
    
    direction.addEventListener('click', () => {
      const rtl = direction.getAttribute('aria-pressed') !== 'true'
      direction.setAttribute('aria-pressed', String(rtl))
      strip.dir = rtl ? 'rtl' : 'ltr'
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A card in a strip, further down the page</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
            margin: 0;
          }
          html {
            scroll-padding-top: 4rem;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            display: flex;
            gap: 1rem;
            align-items: center;
            height: 3rem;
            padding: 0 1rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          main {
            padding: 4rem 1rem 60vh;
          }
          .intro {
            min-height: 120vh;
          }
          .strip {
            display: flex;
            gap: 1rem;
            max-width: 40rem;
            overflow-x: auto;
            padding: 1rem;
            scroll-snap-type: x mandatory;
            border: 1px solid #ddd;
          }
          .card {
            flex: 0 0 12rem;
            height: 8rem;
            padding: 1rem;
            scroll-snap-align: start;
            border-radius: 0.5rem;
            background: #fef3c7;
          }
          .card:focus {
            outline: 2px solid #78350f;
            outline-offset: 2px;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <a href="#card-2">Card 2</a>
            <a href="#card-7">Card 7</a>
            <a href="#card-11">Card 11</a>
            <button type="button" id="direction" aria-pressed="false">Right to left</button>
          </header>
          <main>
            <p class="intro">The shelf is further down. Its cards scroll sideways.</p>
            <section aria-labelledby="shelf-title">
              <h2 id="shelf-title">Shelf</h2>
              <div class="strip" id="strip" tabindex="0" aria-label="Cards">
                <article class="card" id="card-1">Card 1</article>
                <article class="card" id="card-2">Card 2</article>
                <article class="card" id="card-3">Card 3</article>
                <article class="card" id="card-4">Card 4</article>
                <article class="card" id="card-5">Card 5</article>
                <article class="card" id="card-6">Card 6</article>
                <article class="card" id="card-7">Card 7</article>
                <article class="card" id="card-8">Card 8</article>
                <article class="card" id="card-9">Card 9</article>
                <article class="card" id="card-10">Card 10</article>
                <article class="card" id="card-11">Card 11</article>
                <article class="card" id="card-12">Card 12</article>
              </div>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          ScrollTo.auto({ container: 'auto', offset: '#header', inline: 'start', hash: false })
    
          const direction = document.querySelector('#direction')
          const strip = document.querySelector('#strip')
    
          direction.addEventListener('click', () => {
            const rtl = direction.getAttribute('aria-pressed') !== 'true'
            direction.setAttribute('aria-pressed', String(rtl))
            strip.dir = rtl ? 'rtl' : 'ltr'
          })
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A card in a strip, further down the page</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <header id="header">
            <a href="#card-2">Card 2</a>
            <a href="#card-7">Card 7</a>
            <a href="#card-11">Card 11</a>
            <button type="button" id="direction" aria-pressed="false">Right to left</button>
          </header>
          <main>
            <p class="intro">The shelf is further down. Its cards scroll sideways.</p>
            <section aria-labelledby="shelf-title">
              <h2 id="shelf-title">Shelf</h2>
              <div class="strip" id="strip" tabindex="0" aria-label="Cards">
                <article class="card" id="card-1">Card 1</article>
                <article class="card" id="card-2">Card 2</article>
                <article class="card" id="card-3">Card 3</article>
                <article class="card" id="card-4">Card 4</article>
                <article class="card" id="card-5">Card 5</article>
                <article class="card" id="card-6">Card 6</article>
                <article class="card" id="card-7">Card 7</article>
                <article class="card" id="card-8">Card 8</article>
                <article class="card" id="card-9">Card 9</article>
                <article class="card" id="card-10">Card 10</article>
                <article class="card" id="card-11">Card 11</article>
                <article class="card" id="card-12">Card 12</article>
              </div>
            </section>
          </main>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
      margin: 0;
    }
    html {
      scroll-padding-top: 4rem;
    }
    header {
      position: fixed;
      inset: 0 0 auto;
      display: flex;
      gap: 1rem;
      align-items: center;
      height: 3rem;
      padding: 0 1rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    main {
      padding: 4rem 1rem 60vh;
    }
    .intro {
      min-height: 120vh;
    }
    .strip {
      display: flex;
      gap: 1rem;
      max-width: 40rem;
      overflow-x: auto;
      padding: 1rem;
      scroll-snap-type: x mandatory;
      border: 1px solid #ddd;
    }
    .card {
      flex: 0 0 12rem;
      height: 8rem;
      padding: 1rem;
      scroll-snap-align: start;
      border-radius: 0.5rem;
      background: #fef3c7;
    }
    .card:focus {
      outline: 2px solid #78350f;
      outline-offset: 2px;
    }
    

    JavaScript

    main.js
    import { autoScrollTo } from '@samline/scroll-to'
    
    autoScrollTo({ container: 'auto', offset: '#header', inline: 'start', hash: false })
    
    const direction = document.querySelector('#direction')
    const strip = document.querySelector('#strip')
    
    direction.addEventListener('click', () => {
      const rtl = direction.getAttribute('aria-pressed') !== 'true'
      direction.setAttribute('aria-pressed', String(rtl))
      strip.dir = rtl ? 'rtl' : 'ltr'
    })
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A card in a strip, further down the page</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
            margin: 0;
          }
          html {
            scroll-padding-top: 4rem;
          }
          header {
            position: fixed;
            inset: 0 0 auto;
            display: flex;
            gap: 1rem;
            align-items: center;
            height: 3rem;
            padding: 0 1rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          main {
            padding: 4rem 1rem 60vh;
          }
          .intro {
            min-height: 120vh;
          }
          .strip {
            display: flex;
            gap: 1rem;
            max-width: 40rem;
            overflow-x: auto;
            padding: 1rem;
            scroll-snap-type: x mandatory;
            border: 1px solid #ddd;
          }
          .card {
            flex: 0 0 12rem;
            height: 8rem;
            padding: 1rem;
            scroll-snap-align: start;
            border-radius: 0.5rem;
            background: #fef3c7;
          }
          .card:focus {
            outline: 2px solid #78350f;
            outline-offset: 2px;
          }
        </style>
      </head>
      <body>
        <main>
          <header id="header">
            <a href="#card-2">Card 2</a>
            <a href="#card-7">Card 7</a>
            <a href="#card-11">Card 11</a>
            <button type="button" id="direction" aria-pressed="false">Right to left</button>
          </header>
          <main>
            <p class="intro">The shelf is further down. Its cards scroll sideways.</p>
            <section aria-labelledby="shelf-title">
              <h2 id="shelf-title">Shelf</h2>
              <div class="strip" id="strip" tabindex="0" aria-label="Cards">
                <article class="card" id="card-1">Card 1</article>
                <article class="card" id="card-2">Card 2</article>
                <article class="card" id="card-3">Card 3</article>
                <article class="card" id="card-4">Card 4</article>
                <article class="card" id="card-5">Card 5</article>
                <article class="card" id="card-6">Card 6</article>
                <article class="card" id="card-7">Card 7</article>
                <article class="card" id="card-8">Card 8</article>
                <article class="card" id="card-9">Card 9</article>
                <article class="card" id="card-10">Card 10</article>
                <article class="card" id="card-11">Card 11</article>
                <article class="card" id="card-12">Card 12</article>
              </div>
            </section>
          </main>
        </main>
        <script type="module">
          import { autoScrollTo } from '@samline/scroll-to'
    
          autoScrollTo({ container: 'auto', offset: '#header', inline: 'start', hash: false })
    
          const direction = document.querySelector('#direction')
          const strip = document.querySelector('#strip')
    
          direction.addEventListener('click', () => {
            const rtl = direction.getAttribute('aria-pressed') !== 'true'
            direction.setAttribute('aria-pressed', String(rtl))
            strip.dir = rtl ? 'rtl' : 'ltr'
          })
        </script>
      </body>
    </html>
    
    13 / Guided tour

    A tour that plays, pauses and steps

    Guided tourLive preview

    scrollSequence walks through a list of stops, staying a moment on each. Play it, pause it or step by hand; the counter is read out politely. Scroll yourself while it plays and it pauses, so the visitor stays in control. Each stop can have its own pause and options.

    Not started

    Welcome

    Start here: the tour moves on by itself.

    Search

    Find anything from the top bar.

    Reports

    Your numbers, updated every hour. This stop stays longer.

    Share

    Send a link to your team.

    Explore the code

    HTML / CDN

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A tour that plays, pauses and steps</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav class="tour" aria-label="Tour">
            <button type="button" id="prev">Previous</button>
            <button type="button" id="play" aria-pressed="false">Play</button>
            <button type="button" id="next">Next</button>
            <output id="status" aria-live="polite">Not started</output>
          </nav>
          <main>
            <section id="welcome">
              <h2>Welcome</h2>
              <p>Start here: the tour moves on by itself.</p>
            </section>
            <section id="search">
              <h2>Search</h2>
              <p>Find anything from the top bar.</p>
            </section>
            <section id="reports">
              <h2>Reports</h2>
              <p>Your numbers, updated every hour.</p>
            </section>
            <section id="share">
              <h2>Share</h2>
              <p>Send a link to your team.</p>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script src="./main.js" defer></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
      margin: 0;
    }
    .tour {
      position: fixed;
      inset: 0 0 auto;
      display: flex;
      gap: 0.5rem;
      align-items: center;
      padding: 0.75rem 1rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    main section {
      min-height: 80vh;
      padding: 4rem 1rem 1rem;
      border-top: 1px solid #eee;
    }
    [aria-pressed='true'] {
      background: #78350f;
      color: #fff;
    }
    

    JavaScript

    main.js
    const play = document.querySelector('#play')
    const status = document.querySelector('#status')
    
    const tour = ScrollTo.scrollSequence(
      ['#welcome', '#search', { target: '#reports', pause: 3500 }, '#share'],
      {
        offset: '.tour',
        pause: 2000,
        onChange: ({ index, playing }) => {
          play.textContent = playing ? 'Pause' : 'Play'
          play.setAttribute('aria-pressed', String(playing))
          if (index >= 0) status.textContent = `Stop ${index + 1} of ${tour.length}`
        },
      }
    )
    
    play.addEventListener('click', () => tour.toggle())
    document.querySelector('#next').addEventListener('click', () => tour.next())
    document.querySelector('#prev').addEventListener('click', () => tour.prev())
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A tour that plays, pauses and steps</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
            margin: 0;
          }
          .tour {
            position: fixed;
            inset: 0 0 auto;
            display: flex;
            gap: 0.5rem;
            align-items: center;
            padding: 0.75rem 1rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          main section {
            min-height: 80vh;
            padding: 4rem 1rem 1rem;
            border-top: 1px solid #eee;
          }
          [aria-pressed='true'] {
            background: #78350f;
            color: #fff;
          }
        </style>
      </head>
      <body>
        <main>
          <nav class="tour" aria-label="Tour">
            <button type="button" id="prev">Previous</button>
            <button type="button" id="play" aria-pressed="false">Play</button>
            <button type="button" id="next">Next</button>
            <output id="status" aria-live="polite">Not started</output>
          </nav>
          <main>
            <section id="welcome">
              <h2>Welcome</h2>
              <p>Start here: the tour moves on by itself.</p>
            </section>
            <section id="search">
              <h2>Search</h2>
              <p>Find anything from the top bar.</p>
            </section>
            <section id="reports">
              <h2>Reports</h2>
              <p>Your numbers, updated every hour.</p>
            </section>
            <section id="share">
              <h2>Share</h2>
              <p>Send a link to your team.</p>
            </section>
          </main>
        </main>
        <script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js"></script>
        <script>
          const play = document.querySelector('#play')
          const status = document.querySelector('#status')
    
          const tour = ScrollTo.scrollSequence(
            ['#welcome', '#search', { target: '#reports', pause: 3500 }, '#share'],
            {
              offset: '.tour',
              pause: 2000,
              onChange: ({ index, playing }) => {
                play.textContent = playing ? 'Pause' : 'Play'
                play.setAttribute('aria-pressed', String(playing))
                if (index >= 0) status.textContent = `Stop ${index + 1} of ${tour.length}`
              },
            }
          )
    
          play.addEventListener('click', () => tour.toggle())
          document.querySelector('#next').addEventListener('click', () => tour.next())
          document.querySelector('#prev').addEventListener('click', () => tour.prev())
        </script>
      </body>
    </html>
    

    Package / ESM

    HTML

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A tour that plays, pauses and steps</title>
        <link rel="stylesheet" href="./style.css" />
      </head>
      <body>
        <main>
          <nav class="tour" aria-label="Tour">
            <button type="button" id="prev">Previous</button>
            <button type="button" id="play" aria-pressed="false">Play</button>
            <button type="button" id="next">Next</button>
            <output id="status" aria-live="polite">Not started</output>
          </nav>
          <main>
            <section id="welcome">
              <h2>Welcome</h2>
              <p>Start here: the tour moves on by itself.</p>
            </section>
            <section id="search">
              <h2>Search</h2>
              <p>Find anything from the top bar.</p>
            </section>
            <section id="reports">
              <h2>Reports</h2>
              <p>Your numbers, updated every hour.</p>
            </section>
            <section id="share">
              <h2>Share</h2>
              <p>Send a link to your team.</p>
            </section>
          </main>
        </main>
        <script type="module" src="./main.js"></script>
      </body>
    </html>
    

    CSS

    style.css
    body {
      font-family: system-ui, sans-serif;
      margin: 0;
    }
    .tour {
      position: fixed;
      inset: 0 0 auto;
      display: flex;
      gap: 0.5rem;
      align-items: center;
      padding: 0.75rem 1rem;
      background: #fff;
      border-bottom: 1px solid #ddd;
    }
    main section {
      min-height: 80vh;
      padding: 4rem 1rem 1rem;
      border-top: 1px solid #eee;
    }
    [aria-pressed='true'] {
      background: #78350f;
      color: #fff;
    }
    

    JavaScript

    main.js
    import { scrollSequence } from '@samline/scroll-to/sequence'
    
    const play = document.querySelector('#play')
    const status = document.querySelector('#status')
    
    const tour = scrollSequence(
      ['#welcome', '#search', { target: '#reports', pause: 3500 }, '#share'],
      {
        offset: '.tour',
        pause: 2000,
        onChange: ({ index, playing }) => {
          play.textContent = playing ? 'Pause' : 'Play'
          play.setAttribute('aria-pressed', String(playing))
          if (index >= 0) status.textContent = `Stop ${index + 1} of ${tour.length}`
        },
      }
    )
    
    play.addEventListener('click', () => tour.toggle())
    document.querySelector('#next').addEventListener('click', () => tour.next())
    document.querySelector('#prev').addEventListener('click', () => tour.prev())
    

    Complete file

    index.html
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>A tour that plays, pauses and steps</title>
        <style>
          body {
            font-family: system-ui, sans-serif;
            margin: 0;
          }
          .tour {
            position: fixed;
            inset: 0 0 auto;
            display: flex;
            gap: 0.5rem;
            align-items: center;
            padding: 0.75rem 1rem;
            background: #fff;
            border-bottom: 1px solid #ddd;
          }
          main section {
            min-height: 80vh;
            padding: 4rem 1rem 1rem;
            border-top: 1px solid #eee;
          }
          [aria-pressed='true'] {
            background: #78350f;
            color: #fff;
          }
        </style>
      </head>
      <body>
        <main>
          <nav class="tour" aria-label="Tour">
            <button type="button" id="prev">Previous</button>
            <button type="button" id="play" aria-pressed="false">Play</button>
            <button type="button" id="next">Next</button>
            <output id="status" aria-live="polite">Not started</output>
          </nav>
          <main>
            <section id="welcome">
              <h2>Welcome</h2>
              <p>Start here: the tour moves on by itself.</p>
            </section>
            <section id="search">
              <h2>Search</h2>
              <p>Find anything from the top bar.</p>
            </section>
            <section id="reports">
              <h2>Reports</h2>
              <p>Your numbers, updated every hour.</p>
            </section>
            <section id="share">
              <h2>Share</h2>
              <p>Send a link to your team.</p>
            </section>
          </main>
        </main>
        <script type="module">
          import { scrollSequence } from '@samline/scroll-to/sequence'
    
          const play = document.querySelector('#play')
          const status = document.querySelector('#status')
    
          const tour = scrollSequence(
            ['#welcome', '#search', { target: '#reports', pause: 3500 }, '#share'],
            {
              offset: '.tour',
              pause: 2000,
              onChange: ({ index, playing }) => {
                play.textContent = playing ? 'Pause' : 'Play'
                play.setAttribute('aria-pressed', String(playing))
                if (index >= 0) status.textContent = `Stop ${index + 1} of ${tour.length}`
              },
            }
          )
    
          play.addEventListener('click', () => tour.toggle())
          document.querySelector('#next').addEventListener('click', () => tour.next())
          document.querySelector('#prev').addEventListener('click', () => tour.prev())
        </script>
      </body>
    </html>
    

    These examples show a few possibilities. The next ideas are yours: combine the package’s capabilities with your own content, design and interactions to create the experience your project needs.

    Read the example guideDownload the Scroll To skill for AI agents
    @samline/scroll-to

    Smooth scrolling to elements, anchors and positions with offsets for fixed headers, alignment, durations, easing, focus and URL hash handling, for vanilla JavaScript, any framework and plain HTML.

    v1.6.0·MIT

    Explore

    DocsInstallation guideWorking examples

    Project

    Scroll To skill for AI agentsGitHub(opens in a new tab)Changelognpm(opens in a new tab)License(opens in a new tab)

    © 2026 samline · Made for the open web.

    Back to top