Scrollcraft
Chapter 12 of 15RO

Performance: smooth on a real phone.

A scroll site is judged in the first second and on every frame after it. Measure what visitors feel, set a budget, and keep the work off the screen when nobody is looking.

Chapter 12 of 154 min read

What you will learn

By the end of this chapter
  1. 01Explain LCP, INP and CLS and the good threshold for each.
  2. 02Set and follow a budget for images, video, fonts and scripts.
  3. 03Keep scrolling smooth: transform and opacity, batched reads, nothing off screen.
  4. 04Measure with Lighthouse, the Performance panel and a real phone, and fix the biggest item first.

01 · What to measure

Core Web Vitals
LCP

Largest Contentful Paint

How long until the biggest thing on screen, usually the hero image or heading, has appeared. Good is 2.5 seconds or less.

INP

Interaction to Next Paint

How quickly the page responds to a tap, click or key. Good is 200 milliseconds or less. Long scroll handlers and heavy loops make it worse.

CLS

Cumulative Layout Shift

How much the layout jumps while it loads. Good is 0.1 or less. Give every image and video a width and height, and reserve space for fonts and embeds.

02 · Performance and access

Smooth scrolling
Move

Animate transform and opacity

These can be handled without recalculating layout. Animating width, height, top or left makes the browser redo layout on every frame.

Batch

Read, then write

Read all measurements first, then apply all styles. Mixing them in a loop forces repeated layout work.

Hint

Use will-change sparingly

Apply it to the few elements that move constantly, such as parallax layers. On every element it costs memory.

Stop

Do nothing off screen

Pause loops and skip updates for sections that are not visible. The example stops the parallax once the hero has left the screen.

Reduce

Respect reduced motion

Check prefers-reduced-motion in CSS and in JavaScript. Provide a static layout that reads well on its own.

Keys

Keep the keyboard working

Tab order, visible focus and anchor links must behave the same as without effects. Pinned sections should not trap focus.

03 · Only work on screen

IntersectionObserver
1

Stop what nobody can see

The loop below draws only while its canvas is visible. Scroll it away and the frame counter stops: no battery spent on a scene off screen.

Browsers already pause requestAnimationFrame in a hidden tab. Off-screen sections on a visible tab are your job.

HTML
<p class="lead">The animation below only runs while you can see it.</p>
<figure class="stage"><canvas width="600" height="300" aria-hidden="true"></canvas></figure>
<p class="lead tall">Keep scrolling until it leaves the screen, and watch the counter stop.</p>
<p class="meter">Frames drawn: <b>0</b> · <span>paused</span></p>

04 · A budget

Images, video, fonts, scripts
AssetRuleWhy
Hero imageAVIF or WebP, sized with srcset, fetchpriority="high"It is usually the LCP element
Other imagesloading="lazy", width and height setNothing loads before it is needed, nothing jumps
VideoMuted, playsinline, a poster, under a few MB, preload="metadata"Autoplay is only allowed muted; the poster covers the wait
FontsTwo families at most, woff2, font-display: swapText appears at once instead of after the download
ScriptsLibraries only on pages that use them, loaded with defer or as modulesThe page can respond while scripts load
a

Images and video in HTML

index.html
<!-- the hero image: sized, modern formats, loaded first -->
<picture>
  <source srcset="hero-1600.avif 1600w, hero-800.avif 800w" type="image/avif" sizes="100vw">
  <img src="hero-1600.jpg" srcset="hero-1600.jpg 1600w, hero-800.jpg 800w" sizes="100vw"
       width="1600" height="900" alt="…" fetchpriority="high">
</picture>

<!-- images further down: loaded only when they come near the screen -->
<img src="studio.jpg" width="1200" height="800" alt="…" loading="lazy" decoding="async">

<!-- background video: silent, inline on phones, a poster while it loads -->
<video src="loop.mp4" poster="loop.jpg" autoplay muted loop playsinline preload="metadata"></video>
b

One variable font, preloaded

index.html
<link rel="preload" href="/fonts/inter-var.woff2" as="font" type="font/woff2" crossorigin>
<style>
  @font-face {
    font-family: Inter;
    src: url(/fonts/inter-var.woff2) format('woff2');
    font-weight: 100 900;
    font-display: swap; /* text shows at once in a fallback font */
  }
</style>

05 · Measure

Lab and field
a

Lighthouse

In Chrome DevTools, open the Lighthouse tab and run a mobile report. Fix the largest item first and run it again.

b

The Performance panel

Record while you scroll, with the CPU slowed down four times. Long yellow blocks are JavaScript on the main thread; purple ones are layout. Both cause dropped frames.

c

A real phone

A mid-range Android phone on mobile data tells you more than any score. Scroll the whole page twice and note every stutter.

d

Ask for evidence

Prompt
Run Lighthouse in mobile mode and give me LCP, INP or TBT, and CLS. List the three biggest opportunities with the file and line each one comes from. Do not change anything yet.

Practice · Work with AI

The same task in your tool

Ask the AI to measure before it optimises. Optimising without numbers usually moves work around without removing any.

Open a terminal in the project folder and run claude. Press Shift+Tab until plan mode is on, paste the prompt, read the plan, then approve it. Project rules go in CLAUDE.md.

Baseline

Prompt
Run Lighthouse in mobile mode on the site for [subject] and give me LCP, CLS and Total Blocking Time, plus the three biggest opportunities with the file and line each one comes from. Do not change anything yet.

Fix one thing

Prompt
Read CLAUDE.md. Fix only the first opportunity from the baseline. Then run Lighthouse again with the same settings and show me the before and after numbers side by side.

Practice · Exercises

Try first, then compare
1

Find the LCP element

In the Lighthouse report or the Performance panel, find which element is your Largest Contentful Paint.

Hint: it is usually the hero image or the hero heading.

Show the solution

If it is an image: serve AVIF or WebP at the right size, add width and height, and fetchpriority="high". If it is text: preload the font it uses and keep font-display: swap.

2

Stop the layout shift

Your CLS is 0.25. Find what moves while the page loads.

Hint: images without dimensions and fonts that swap are the usual causes.

Show the solution

Give every image and video width and height (or an aspect-ratio), reserve space for embeds, and use a fallback font with similar metrics. Measure again: good is 0.1 or less.

3

Pause off screen

Make one looping effect in your site stop when it is not visible.

Hint: the preview in section 03 does exactly this.

Show the solution

Watch the element with an IntersectionObserver; start the loop when it enters and cancel the frame when it leaves.

JavaScript
new IntersectionObserver(([e]) => {
  if (e.isIntersecting && !raf) raf = requestAnimationFrame(loop);
  if (!e.isIntersecting && raf) { cancelAnimationFrame(raf); raf = 0; }
}).observe(el);

Practice · Course project

One site, built across the course

Make the site for [subject] fast on a mid-range phone, with numbers to prove it.

  1. Run “Baseline” and save the numbers in notes.md.
  2. Go through the budget table: images, video, fonts, scripts.
  3. Run “Fix one thing” three times, for the three biggest opportunities.
  4. Pause every off-screen loop, then record the final numbers.

Practice · Test it

Check the result
  • Lighthouse mobile: LCP 2.5 s or less and CLS 0.1 or less.
  • Every image has width and height; those below the first screen load lazily.
  • The Performance panel shows no long tasks while scrolling, with the CPU slowed four times.
  • On a real phone, the whole page scrolls without stutter.

Practice · Assessment

Acceptance criteria

0 of 4 criteria met

Next Testing and QA

A release checklist, browser tests with Playwright, an accessibility audit, screenshot comparisons and real devices.

Keyboard shortcuts: [ previous chapter · ] next chapter