zoom-scroll
A picture in a pinned section zooms from its box to fill the screen as you scroll.
Day two starts at the lighthouse
Coastal walks, day one The headland at first light
Live preview needs WebGPU or WebGL 2. A photo of a grassy headland in a small rounded frame, which grows to fill the pinned frame as you scroll, with the trip title under it.
This example failed to draw.
Usage
Put it on img, video on a sticky stage inside a tall section. start() registers zoom-scroll, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add zoom-scroll<div class="trip"> <p class="after">Day two starts at the lighthouse</p> <figure class="view"> <img src="/media/coast/headland-1440.avif" srcset="/media/coast/headland-800.avif 800w, /media/coast/headland-1440.avif 1440w" sizes="100vw" width="1440" height="960" data-dio="zoom-scroll" data-dio-load="eager" /> </figure> <p class="title"> <span>Coastal walks, day one</span> The headland at first light </p></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('img', 'zoom-scroll', { style: 'zoom', start: 0, end: 0.7, zoom: 1.2, radius: 0, reverse: false, ease: 0,})import { start } from '@dioramajs/diorama'import zoomScroll from '@dioramajs/media/zoom-scroll'
const app = start()app.attach('img', zoomScroll({ style: 'zoom', start: 0, end: 0.7, zoom: 1.2, radius: 0, reverse: false, ease: 0,}))Page transitions
Fades with the page transition; a keyed flight starts from the box it was drawn at. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
stylerebuildsdata-dio-style | 'zoom' | 'mask' | 'through' | 'zoom' |
|
startdata-dio-start | number0 to 1 | 0 | Pin at which the zoom starts. |
enddata-dio-end | number0 to 1 | 0.7 | Pin at which it fills the screen, or for through has faded out. |
zoomdata-dio-zoom | number1 to 2 | 1.2 | Extra zoom of the picture: push-in for zoom, start zoom for mask, 2x for through; 1 or 0 for none. |
radiusdata-dio-radius | number0 to 64 px | 0 | Corner radius once it fills the screen. |
reversedata-dio-reverse | boolean | false | Plays it backward: full screen at the start of the pin, back in its box at end. |
easedata-dio-ease | number0 to 1 s | 0 | Smoothing behind scroll; 0 follows it at once (Lenis already smooths it). |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta
. Draws only while something on it moves, then stops. Scroll draws its own frames, ease keeps drawing until it settles. None at rest. How a frame is decided.
Reduced motion and touch
With reduced motion it is off and the element shows as plain HTML. Off: the picture stays in its box, as the DOM shows it.
Runs on every device. Works from scroll. On iOS without Lenis the full-screen copy can trail composited scroll by a frame as the stage is let go; Lenis avoids it.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { 'zoom-scroll': { reducedMotion: 'static' } } })Cost and quality
- Download
- 1.2 KB min+gzip the first time a page uses it
- GPU
- One texture read per pixel, up to the whole viewport once zoomed.
- Memory
- The image the browser picked for the box. For a sharp full screen, let sizes allow it, such as sizes="100vw".
- Frames
- Idle at rest
It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.
Recipes
Scroll into the picture
The classic travel or real estate hero: a tall section whose stage sticks to the top at the height of the screen, with the picture in a box on it. Between start and end of the pin the picture grows from its box to the stage. With sizes="100vw" the browser picks a source sharp enough for the full screen.
<section style="height: 300vh"> <div style="position: sticky; top: 0; height: 100vh; display: grid; place-items: center"> <img src="/img/coast-1920.avif" srcset="/img/coast-960.avif 960w, /img/coast-1920.avif 1920w" sizes="100vw" style="width: 40vw; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 24px" data-dio="zoom-scroll" /> </div></section>The effect finds the sticky stage on its own. For another layout, mark the tall section with data-dio-section.
A window that opens onto the scene
style="mask" keeps the picture still at its full-screen framing and opens a window onto it: the scene seems to be there all along, behind the page. zoom sets how far it is zoomed in while the window is small.
<img src="/img/coast.avif" style="object-fit: cover; border-radius: 32px" data-dio="zoom-scroll" data-dio-style="mask" data-dio-zoom="1.3"/>Fly through into the next chapter
style="through" grows the picture, flies past the screen and fades it out to show what lies behind on the stage, such as the next heading.
<img src="/img/coast.avif" style="object-fit: cover" data-dio="zoom-scroll" data-dio-style="through" data-dio-end="0.8"/>Notes
Before the pin and under reduced motion the picture is the plain <img> in its box. Scrolling back plays the zoom backward; reverse starts it at full screen and ends it in the box.
With Lenis the scroll is already smooth: keep ease at 0. With native scroll a small ease such as 0.1 hides the steps of a mouse wheel.
style rebuilds the effect; start, end, zoom, radius, reverse and ease change live.