Get there, and land right.

Scroll To
Smooth scrolling that stops where it should.

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-to

About 6.3 KB gzipZero dependenciesMIT licensed

Scroll To previewLive preview
Easing
Duration

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.

Pick a link

ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.

JavaScript TypeScript React Vue Svelte Shopify WordPress
01 / More than scrollIntoView

The scroll you meant.
The details it skips.

Offsets that stay right

Pixels, the header itself, every sticky bar found for you, or your CSS scroll padding, measured on every frame.

Focus follows

Links move keyboard focus to their target once it arrives, so the next Tab continues from there, as a native jump would.

Lands on target

The destination is measured on every frame, so images loading on the way never leave it short or past the section.

You stay in control

The wheel, a touch or a scrolling key stops it at once; Next pressed again moves on. Reduced motion jumps.

Any container, both axes

Scroll the page, a box or boxes inside boxes in one move, with snapping paused on the way and right to left handled.

Know when it arrives

A promise, callbacks for every frame and events that bubble, with the reason it ended: arrived, interrupted or cancelled.

02 / Fit it into your stack

Links first. Script when you need it.

Your links stay plain #id links that jump without JavaScript.

Package

@samline/scroll-to

scrollTo, autoScrollTo, createScrollTo and more, as ESM or CommonJS.

One scroll

scrollTo(target, options)

Returns a promise of true (arrived) or false.

Whole page

autoScrollTo(options)

Every #id link and [data-scroll-to], with one listener.

HTML / CDN

window.ScrollTo

A classic script that starts on its own.

Guided tours

@samline/scroll-to/sequence

scrollSequence: play, pause and step through a list of stops.

Styling

No stylesheet

Nothing to style: it only moves the scroll position.

03 / Install and glide

Plain links. One call.

Make same-page links scroll smoothly below a fixed header.

Read the installation guide
1

Install

npm

Terminal
npm install @samline/scroll-to

pnpm

Terminal
pnpm add @samline/scroll-to

yarn

Terminal
yarn add @samline/scroll-to

bun

Terminal
bun add @samline/scroll-to

HTML / CDN

Terminal
<script src="https://cdn.jsdelivr.net/npm/@samline/scroll-to@1.6.0/dist/browser/scroll-to.global.js" defer></script>
2

Write your links

Plain #id links, or any element with data-scroll-to and a target: an #id, a selector or top.

index.html
<header id="header">
  <a href="#pricing">Pricing</a>
</header>

<section id="pricing">…</section>
<button type="button" data-scroll-to="top">Back to top</button>
3

Start it

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

autoScrollTo({ offset: '#header' })
Copy complete working files

Continue with every option, alignment and scroll containers.

Explore the options
04 / Beyond anchors

One function.
Every kind of scroll.

Three common jobs, without measuring a single element yourself.

data-scroll-to-back-to-top

Back to top

Shows after one screen, fills a ring, and brings focus up.

Read the API
index.html
<a class="back-to-top" href="#top" data-scroll-to-back-to-top>
  Back to top
</a>
inline: center

Center the active tab

Scroll a strip, never the page.

Read the API
main.js
scrollTo(activeTab, {
  container: tabs,
  inline: 'center',
  block: 'nearest',
})
highlight

Highlight on arrival

The target gets data-scroll-to-arrived for a moment.

Read the options
main.js
autoScrollTo({ offset: 80, highlight: true })
Explore working examples
05 / Before you build

A few details. Clearly answered.

Why not CSS scroll-behavior: smooth?
Use it when it is enough. scroll-to adds what CSS cannot: offsets from a live element, durations and easing, interruption, focus, a promise and targets that move while content loads.
Does it work without a bundler?
Yes. Load dist/browser/scroll-to.global.js; it handles #id links and [data-scroll-to] on its own. Add data-manual to the script tag to start it yourself with ScrollTo.auto(). Pin the version in production.
Can I change the speed and the easing?
Yes. duration takes milliseconds, or 'auto' for half a millisecond per pixel between 300 and 1000. easing takes ease-in-out, ease-out, linear or your own function. behavior: 'instant' jumps, and reduced motion always jumps.
What about links that open a page with a #hash?
With an offset, autoScrollTo aligns the URL hash once the page loads, so a fixed header does not cover the section the visitor came for.
Does it break the Back button?
No. Links push their hash like a native jump, so Back returns to the previous section. Use hash: replace or false to change that.
What if the visitor scrolls while it moves?
It stops at once and the promise resolves to false. Set interruptible: false for scrolls that must finish.
What happens without JavaScript?
Links still jump to their sections natively. Elements with only data-scroll-to do nothing, so prefer links where you can.
Do links added later work?
Yes. autoScrollTo listens with one delegated click handler, so links and triggers added at any time are handled.
Scrolling with intent

Every jump, landed precisely.

Try the examples or follow the first steps.

Guides, the complete API and release notes, in one place.

Read the documentation