Backdrop parallax in Firefox: a passive scroll fallback
Where scroll-driven animations are missing, a passive scroll handler writes the scroll position to --pb-y at most once a frame and the CSS moves each layer by its share, matching the scroll-timeline version. Off under reduced motion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2df908f7c4
commit
6506f65b1e
2 changed files with 41 additions and 3 deletions
|
|
@ -46,6 +46,13 @@
|
|||
}
|
||||
}
|
||||
|
||||
/* The same rise where scroll-driven animations are missing: the
|
||||
scroll position arrives as --pb-y from PageBackdrop.tsx. */
|
||||
.page-backdrop[data-parallax="script"] .page-backdrop__circle {
|
||||
transform: translateY(calc(var(--pb-y, 0px) * var(--pb-speed) * -1));
|
||||
will-change: transform;
|
||||
}
|
||||
|
||||
@keyframes page-backdrop-rise {
|
||||
to {
|
||||
transform: translateY(calc(var(--pb-scroll, 0px) * var(--pb-speed) * -1));
|
||||
|
|
@ -53,8 +60,10 @@
|
|||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.page-backdrop__circle {
|
||||
.page-backdrop__circle,
|
||||
.page-backdrop[data-parallax="script"] .page-backdrop__circle {
|
||||
animation: none;
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@
|
|||
small and quicker at the front, none as fast as the page itself.
|
||||
The rise is a CSS scroll-driven animation (PageBackdrop.css) over
|
||||
--pb-scroll, the page's scrollable height, which is measured here
|
||||
whenever the page changes size (no scroll listener). Without
|
||||
scroll-driven animations, or under reduced motion, they stay put.
|
||||
whenever the page changes size. Browsers without scroll-driven
|
||||
animations (Firefox, for now) get the same motion from a passive
|
||||
scroll handler instead, which writes the scroll position to one
|
||||
CSS variable, --pb-y, at most once a frame; the CSS does the rest.
|
||||
Under reduced motion they stay put either way.
|
||||
|
||||
Each page gets its own arrangement: sizes and places come from a
|
||||
generator seeded with the path, so a page looks the same every
|
||||
|
|
@ -95,6 +98,32 @@ export default function PageBackdrop() {
|
|||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
// Where scroll-driven animations are missing: the scroll position,
|
||||
// once a frame at most, into --pb-y (PageBackdrop.css moves each
|
||||
// layer by its share of it).
|
||||
useEffect(() => {
|
||||
const backdrop = ref.current
|
||||
if (!backdrop || CSS.supports('animation-timeline: scroll()')) return undefined
|
||||
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return undefined
|
||||
|
||||
backdrop.dataset.parallax = 'script'
|
||||
let frame = 0
|
||||
const update = () => {
|
||||
frame = 0
|
||||
backdrop.style.setProperty('--pb-y', `${window.scrollY}px`)
|
||||
}
|
||||
const onScroll = () => {
|
||||
if (!frame) frame = requestAnimationFrame(update)
|
||||
}
|
||||
update()
|
||||
window.addEventListener('scroll', onScroll, { passive: true })
|
||||
return () => {
|
||||
window.removeEventListener('scroll', onScroll)
|
||||
cancelAnimationFrame(frame)
|
||||
delete backdrop.dataset.parallax
|
||||
}
|
||||
}, [])
|
||||
|
||||
// A new top-level section reaching the window's middle moves the tone on.
|
||||
useEffect(() => {
|
||||
const main = document.getElementById('main')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue