Sticky, eased
changes speed instantly at both ends of the pin: from scroll
speed to still, and back. This eases both ends.
It does not replace sticky. The element stays
and the
browser pins it. One offset is added on
, as a scroll-driven
animation, and it is zero except in a short stretch of scroll at each end of
the pin. Where it does not run, the element is plain sticky and the layout is
the same.
The whole implementation is in sticky.ts, next to this file: one
file, no dependencies, about 25 kB with its comments. You do not install
anything. You copy that file into the project. Read its header comment before
changing it.
1. Decide what to ease
Ease these:
- a sidebar or rail that sticks beside longer content
- a sticky heading or label inside a section
- a card or media box pinned while text scrolls past
Leave these alone. They stay plain sticky, and the code refuses them anyway:
- a site header whose container is the whole page (nothing to ease into)
- table headers (, )
- a or sticky
- a sticky inside its own scroll container
Do not use it for a pin written in script (
toggled on
scroll, GSAP ScrollTrigger
, a transform written from the scroll
position). Those are not sticky. Either convert them to
first, or leave them.
2. Check the element can be eased
All four must hold. Fix the markup or CSS first if one does not.
- The element's computed is and is a length, not
.
- Its parent is the box it sticks within, and the parent is taller than
the element. The room between them is the pin's length.
- No ancestor between the element and the page has other than
or . (That also breaks native sticky. is
the fix when the ancestor only needs to hide overflow.)
- Nothing else sets on the element. The offset owns that
property while it runs. is free; so is a wrapper.
3. Add the code
Copy
sticky.ts into the project, where its own helpers live
(
,
,
), and import
from there:
ts
import { sticky } from '@/lib/sticky'
- Copy it whole and unchanged, header comment included. Its first line names
the version.
- In a JavaScript project, compile it once
(
npx tsc sticky.ts --target es2022 --module esnext --declaration false
)
or strip the types by hand.
- If the project already has a copy, compare the version on its first
line with this one. Replace it if it is older, and keep any change the
project made to it deliberately.
- Fit it to the project only at the edges: its lint and formatting rules, its
import alias. Leave the measuring and the maths alone.
The same code is on npm as
, for a project that would rather
have a dependency it can update:
The examples below import from
. With the package, import from
instead; nothing else changes.
4. Call it
Client only. It reads layout, so never during server rendering.
One element:
ts
const stick = sticky(document.querySelector('.rail')!)
stick.refresh() // measure again, for a change that resizes neither box
stick.freeze() // keep the current offset and stop following the scroll
stick.destroy() // back to plain sticky
Every sticky on the page, in one call:
ts
const all = sticky.all() // finds them from the stylesheets' own rules
const some = sticky.all('.rail') // or only the ones named
all.scan() // look again after content was added or removed
all.destroy()
reads the page's CSS for rules that set
, so a
sticky that only applies from a breakpoint up is found at any width and turns
on when the rule applies. Prefer it over a hand-kept list of selectors.
Per element, in the markup:
html
<aside data-sticky-ease="off">…</aside>
<aside data-sticky-ease="expo" data-sticky-distance="0.3">…</aside>
Options
| option | default | |
|---|
| | How much scroll the ease in, and the ease out, takes. A fraction of the viewport height. |
| | , , , , or any ease-out function. |
| pointer devices | Media query for where it runs. runs it everywhere. |
| | (scroll-driven animation) or (script). |
| | , the frame loop the script engine uses. |
Start from the defaults. If the project has motion tokens, take
and
from them rather than inventing values.
5. Wire it to the framework
Always destroy on unmount. An element that goes away with its stick still
attached leaks observers.
Vue / Nuxt
ts
import { onMounted, onBeforeUnmount, type Ref } from 'vue'
import { sticky, type Sticky, type StickyOptions } from '@/lib/sticky'
export const useSticky = (target: Readonly<Ref<HTMLElement | null>>, options?: StickyOptions) => {
let stick: Sticky | null = null
onMounted(() => {
if (target.value) stick = sticky(target.value, options)
})
onBeforeUnmount(() => stick?.destroy())
return { freeze: () => stick?.freeze() }
}
React
tsx
import { useEffect, useRef } from 'react'
import { sticky, type StickyOptions } from '@/lib/sticky'
export function useSticky<T extends HTMLElement>(options?: StickyOptions) {
const ref = useRef<T>(null)
useEffect(() => {
if (!ref.current) return
const stick = sticky(ref.current, options)
return () => stick.destroy()
}, [])
return ref
}
Whole site, once. Call
in the root layout's mount, and
after each route change so new pages' stickies are picked up.
Page transitions. If a transition resets the scroll while the old page is
still on screen, call
on its sticks first, or the element jumps as
the scroll goes to zero.
A smooth-scroll library (Lenis and the like). Nothing to do where the
compositor engine runs. For the script engine, pass the library's frame as
so the write lands in the frame the page moves in:
ts
sticky(el, {
ticker: (fn) => {
gsap.ticker.add(fn)
return () => gsap.ticker.remove(fn)
},
})
A virtual scroll has no
at all. Use the maths alone:
ts
import { curve } from '@/lib/sticky'
const c = curve({ travel: 1200, span: innerHeight * 0.4, ease: 'power2' })
const offset = c.clamp(scroll - engage) // where a plain clamp would go
6. Verify
Do not report it done from the code alone. In a browser:
- is or . means it is not
running: go back to section 2, or the device is touch, or reduced motion is
on.
- Scroll to where the element reaches its line.
getComputedStyle(el).translate
is a non-zero just before and
after that point.
- Scroll to the middle of the pin. It is or : the element sits
exactly on its line.
- No console errors, in Firefox as well as Chrome. They take different code
paths.
What to know before changing anything
- Touch is off by default. On a phone the offset can run out of step with
the scroll, depending on the browser. forces it on. Do not
turn that on unless asked, and say it needs checking on a real phone.
- Reduced motion turns it off. Leave that.
- Engines. Chrome, Edge, Safari 26+ and Firefox run it as a scroll-driven
animation; older browsers write the same offset from script.
- Range offsets are strings (), not Typed OM
values. Firefox has scroll-driven animations without the Typed OM.
- looks again on . A module script can run before its
stylesheet has loaded, and then there are no rules to read yet.
- It measures again when the element, its parent or the viewport resizes,
and only rebuilds if the numbers moved.
- Supported: stickies on the block axis of the root scroller.
How it works, in four lines
Sticky's own offset is
clamp(scroll − engage, 0, travel)
. The ease in takes
of scroll (
× viewport height) on an ease-out
with starting
slope
. To start at exactly scroll speed the element may only
cover
while it eases, so the offset is
for
from 0 to 1, and the ease out is the same mirrored.