Sticky
position: sticky with its corners rounded off. Sticky still pins the element.
This adds a small offset on top, so it slows into its line and eases away from it.
Scroll
Two cards on the same sticky. The right one also runs through sticky().
Watch the moment each one reaches its line, and the moment it leaves.
Both cards are plain sticky here. The correction runs on pointer devices and steps aside for reduced motion, so nothing about the layout depends on it.
A short container. The two windows shrink until they meet.
Sticky does the pinning
This is not a pin written in script. Nothing is set to fixed, nothing is measured and
moved every frame, and the scroll is left alone. The element is
position: sticky and the browser pins it, the way it always has.
This only adds an offset on top.
So wherever the offset does not run, what is left is plain sticky: on touch, under
reduced motion, at a breakpoint that turns the sticky off, in a browser with nothing to
run it on, before the script loads and after destroy(). Nothing jumps,
because nothing about the layout ever depended on it.
How it works
Sticky moves an element on a ramp: nothing, then exactly the scroll, then nothing again. The two sharp corners are what the eye reads as the hard stick. The offset is the difference between that ramp and one with its corners rounded, run as a scroll-driven animation, so the compositor applies it from the same scroll as sticky and no script runs while the page moves.
Install
npm i @alpacka/sticky
Usage
Keep the sticky you already have. The element's parent is the box it sticks within.
import { sticky } from '@alpacka/sticky'
const stick = sticky(document.querySelector('.rail'))
stick.freeze() // hold the pose, e.g. before a page transition
stick.destroy() // back to plain sticky
A virtual scroll has no sticky to correct. curve is the same maths on its own,
and clamp is the rounded ramp to use in place of a clamp.
import { curve } from '@alpacka/sticky'
const c = curve({ travel: 1200, span: innerHeight * 0.4, ease: 'power2' })
const offset = c.clamp(scroll - engage)
Every sticky at once
Or hand it the whole page. With no selector, sticky.all() finds the
stickies itself, from the rules in your stylesheets that declare
position: sticky. It goes by the rules and not by what is sticky right now,
so one that only exists from a breakpoint up is found at any width and switches on when
it becomes one.
import { sticky } from '@alpacka/sticky'
const sticks = sticky.all() // every sticky on the page
const rails = sticky.all('.rail') // or the ones you name
sticks.scan() // look again, after content arrived or left
sticks.freeze()
sticks.destroy()
Running it blind is safe for the same reason everything here is: the offset only ever sits on top of sticky. What it cannot round stays plain sticky, whether that is a table header pinned inside its row, a site header with the whole page to stick within, or a sticky inside a scroll container of its own. An attribute sets one element apart.
<aside data-sticky-ease="off">…</aside> <!-- leave this one alone -->
<aside data-sticky-ease="expo" data-sticky-distance="0.3">…</aside>