Public site: smoother scrolling
Scrolling the home page dropped frames on a desktop-sized window. Measured in Firefox and Chromium, the cost came from blocks fading in while scrolling and from the blurred (frosted) header and calendar cards, which are redrawn on every frame. - Header and upcoming-calendar cards: nearly solid background, no blur. - Fade-in animations are shorter (0.4s / 0.5s, a smaller slide). - They are off for people whose device asks for reduced motion, and on devices that can't keep up: very few cores / little memory straight away, otherwise when more than 1 in 10 of the first scrolled frames run late (scrollReveal.ts adds `plain-motion` to <html>, remembered for the visit). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
parent
2c0f54b1f7
commit
a6f9e6c6e3
7 changed files with 144 additions and 14 deletions
|
|
@ -18,12 +18,12 @@
|
|||
{data.heading || 'Holistic and Experiential Learning about Happiness'}
|
||||
</h1>
|
||||
{#if data.subheading}
|
||||
<p class="animate-fade-in mx-auto mt-6 max-w-2xl text-lg leading-relaxed text-fg-subheading/70 md:text-xl" style="animation-delay: 0.15s;">
|
||||
<p class="animate-fade-in mx-auto mt-6 max-w-2xl text-lg leading-relaxed text-fg-subheading/70 md:text-xl" style="animation-delay: 0.1s;">
|
||||
{data.subheading}
|
||||
</p>
|
||||
{/if}
|
||||
{#if data.buttonLabel}
|
||||
<div class="animate-fade-in mt-10" style="animation-delay: 0.3s;">
|
||||
<div class="animate-fade-in mt-10" style="animation-delay: 0.2s;">
|
||||
<a class="btn rounded-button border-0 bg-sage px-10 py-3 text-lg text-primary-content shadow-lg transition hover:bg-sage-dark hover:shadow-xl" href={data.buttonUrl || shopLinks(page.data.header).booking}>
|
||||
{data.buttonLabel}
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@
|
|||
{:else}
|
||||
<div class="isolate grid gap-8 lg:grid-cols-2">
|
||||
{#each months as month}
|
||||
<div class="relative z-0 overflow-visible rounded-panel border border-sage/15 bg-white/80 p-3 shadow-sm sm:p-5 backdrop-blur hover:z-[9000] focus-within:z-[9000]">
|
||||
<div class="relative z-0 overflow-visible rounded-panel border border-sage/15 bg-white/90 p-3 shadow-sm sm:p-5 hover:z-[9000] focus-within:z-[9000]">
|
||||
<h3 class="font-display mb-5 text-center text-3xl text-charcoal">{month.label}</h3>
|
||||
<div class="mb-2 grid grid-cols-7 gap-1 text-center text-xs font-semibold uppercase tracking-wide text-charcoal/45">
|
||||
{#each weekdays as weekday}<div>{weekday}</div>{/each}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,88 @@
|
|||
* Scroll reveal Svelte action — applies a CSS class when the element enters the viewport.
|
||||
* Usage: <div use:scrollReveal class="reveal">...</div>
|
||||
* The `reveal` class is hidden initially; `reveal-visible` is added on intersection.
|
||||
*
|
||||
* On a device that can't keep up, the animations are switched off for the rest
|
||||
* of the visit (`plain-motion` on <html>, see app.css): one with very few cores
|
||||
* or little memory straight away, any other once scrolling is seen to stutter.
|
||||
*/
|
||||
|
||||
const PLAIN = 'plain-motion';
|
||||
const STORAGE_KEY = 'plain-motion';
|
||||
/** Scrolled frames to look at before deciding, and how many may run late. */
|
||||
const SAMPLE_FRAMES = 120;
|
||||
const LATE_SHARE = 0.1;
|
||||
/** A frame this long missed at least one refresh of a 60 Hz screen. */
|
||||
const LATE_MS = 25;
|
||||
|
||||
let watching = false;
|
||||
|
||||
function usePlainMotion() {
|
||||
document.documentElement.classList.add(PLAIN);
|
||||
try {
|
||||
sessionStorage.setItem(STORAGE_KEY, '1');
|
||||
} catch {
|
||||
// Storage blocked: decided again on the next page.
|
||||
}
|
||||
}
|
||||
|
||||
function weakHardware() {
|
||||
const memory = (navigator as Navigator & { deviceMemory?: number }).deviceMemory;
|
||||
return (navigator.hardwareConcurrency || 8) <= 2 || (memory !== undefined && memory <= 2);
|
||||
}
|
||||
|
||||
/** Times frames while the page scrolls; too many late ones switch the animations off. */
|
||||
function watchScrolling() {
|
||||
if (watching || typeof window === 'undefined') return;
|
||||
watching = true;
|
||||
|
||||
let stored = false;
|
||||
try {
|
||||
stored = sessionStorage.getItem(STORAGE_KEY) === '1';
|
||||
} catch {
|
||||
// Storage blocked: measure instead.
|
||||
}
|
||||
if (stored || weakHardware()) {
|
||||
usePlainMotion();
|
||||
return;
|
||||
}
|
||||
|
||||
let frames = 0;
|
||||
let late = 0;
|
||||
let last = 0;
|
||||
let lastScroll = 0;
|
||||
let running = false;
|
||||
|
||||
function tick(now: number) {
|
||||
if (last) {
|
||||
frames++;
|
||||
if (now - last > LATE_MS) late++;
|
||||
}
|
||||
last = now;
|
||||
if (frames >= SAMPLE_FRAMES) {
|
||||
window.removeEventListener('scroll', onScroll);
|
||||
if (late / frames > LATE_SHARE) usePlainMotion();
|
||||
return;
|
||||
}
|
||||
// Only frames while scrolling count; stop shortly after it ends.
|
||||
if (now - lastScroll < 300) requestAnimationFrame(tick);
|
||||
else running = false;
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
lastScroll = performance.now();
|
||||
if (running) return;
|
||||
running = true;
|
||||
last = 0;
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
window.addEventListener('scroll', onScroll, { passive: true });
|
||||
}
|
||||
|
||||
export function scrollReveal(node: HTMLElement, delay: number = 0) {
|
||||
watchScrolling();
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue