Offsets that stay right
Pixels, the header itself, every sticky bar found for you, or your CSS scroll padding, measured on every frame.
Same-page links and data-scroll-to triggers glide to their target, stop below your fixed header and hand focus to what they reveal.
Install it, try the live preview, then read the full reference.
npm install @samline/scroll-toAbout 6.3 KB gzipZero dependenciesMIT licensed
Overview
Plain #id links, scrolled smoothly.
Offset
Each section stops just below the sticky bar.
Focus
Keyboard focus follows, so the next Tab starts here.
Interrupt
Scroll the box while it moves and it lets go.
Arrival
Every scroll is a promise that tells you how it ended.
ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.
Pixels, the header itself, every sticky bar found for you, or your CSS scroll padding, measured on every frame.
Links move keyboard focus to their target once it arrives, so the next Tab continues from there, as a native jump would.
The destination is measured on every frame, so images loading on the way never leave it short or past the section.
The wheel, a touch or a scrolling key stops it at once; Next pressed again moves on. Reduced motion jumps.
Scroll the page, a box or boxes inside boxes in one move, with snapping paused on the way and right to left handled.
A promise, callbacks for every frame and events that bubble, with the reason it ended: arrived, interrupted or cancelled.
Your links stay plain #id links that jump without JavaScript.
Package
scrollTo, autoScrollTo, createScrollTo and more, as ESM or CommonJS.
One scroll
Returns a promise of true (arrived) or false.
Whole page
Every #id link and [data-scroll-to], with one listener.
HTML / CDN
A classic script that starts on its own.
Guided tours
scrollSequence: play, pause and step through a list of stops.
Styling
Nothing to style: it only moves the scroll position.
Make same-page links scroll smoothly below a fixed header.
Read the installation guideInstall
npm
npm install @samline/scroll-topnpm
pnpm add @samline/scroll-toyarn
yarn add @samline/scroll-tobun
bun add @samline/scroll-toHTML / CDN
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js" defer></script>Write your links
Plain #id links, or any element with data-scroll-to and a target: an #id, a selector or top.
<header id="header">
<a href="#pricing">Pricing</a>
</header>
<section id="pricing">…</section>
<button type="button" data-scroll-to="top">Back to top</button>Start it
import { autoScrollTo } from '@samline/scroll-to'
autoScrollTo({ offset: '#header' })Continue with every option, alignment and scroll containers.
Explore the optionsThree common jobs, without measuring a single element yourself.
Shows after one screen, fills a ring, and brings focus up.
Read the API<a class="back-to-top" href="#top" data-scroll-to-back-to-top>
Back to top
</a>scrollTo(activeTab, {
container: tabs,
inline: 'center',
block: 'nearest',
})autoScrollTo({ offset: 80, highlight: true })Try the examples or follow the first steps.
Guides, the complete API and release notes, in one place.
Read the documentation