# zoom-scroll

A picture in a pinned section zooms from its box to fill the screen as you scroll.

While the section is pinned, between start and end, a copy of the picture grows from its box to the box of the sticky stage, usually the whole screen. zoom scales the picture up with its box and pushes in a little more; mask keeps the picture still at its full-screen framing and opens a rounded window onto it, the picture easing back from zoom as the window opens; through grows it, flies past the screen and fades out to show the content behind. Corners go from the element radius to radius. Scrolling up plays it backward; reverse plays it from the full screen into the box.

Module `media`, category Scroll scenes. Import `@dioramajs/media/zoom-scroll`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/zoom-scroll.ts)

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.

```html title="index.html"
<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" alt="A grassy headland dropping into the sea under a pale sky" 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>
```

## 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.

```html
<section style="height: 300vh"><div style="position: sticky; top: 0; height: 100vh; display: grid; place-items: center"><img src="/img/coast.avif" alt="Cliffs above a turquoise bay" style="width: 40vw; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 24px" data-dio="zoom-scroll" /></div></section>
```

In page transitions: Fades with the page transition; a keyed flight starts from the box it was drawn at.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'zoom' \| 'mask' \| 'through'` | `'zoom'` | zoom grows with its box; mask opens a window on a still picture; through flies past. |
| `start` | `data-dio-start` | `number` 0 to 1 | `0` | Pin at which the zoom starts. |
| `end` | `data-dio-end` | `number` 0 to 1 | `0.7` | Pin at which it fills the screen, or for through has faded out. |
| `zoom` | `data-dio-zoom` | `number` 1 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. |
| `radius` | `data-dio-radius` | `number` 0 to 64 px | `0` | Corner radius once it fills the screen. |
| `reverse` | `data-dio-reverse` | `boolean` | `false` | Plays it backward: full screen at the start of the pin, back in its box at end. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0` | Smoothing behind scroll; 0 follows it at once (Lenis already smooths it). |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `zoom`, `mask`, `through`
  - `zoom` grows with its box.
  - `mask` opens a window on a still picture.
  - `through` flies past.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta). Draws only while something on it moves, then stops. Scroll draws its own frames, ease keeps drawing until it settles. None at rest. See [how a frame is decided](/docs/concepts/frames).

## 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.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { 'zoom-scroll': { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 1.2 KB 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".

See [the quality plugin](/docs/effects/quality).

## 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.

```html
<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.

```html
<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.

```html
<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.

## Related

- [curtain](/docs/effects/curtain.md): A cover picture opens from behind a curtain as you scroll: strips slide away, halves part or a liquid edge drains past.
- [expand](/docs/effects/expand.md): The image of a card grows to fill its sticky stage as the section scrolls.
- [horizontal](/docs/effects/horizontal.md): A row of cards on a pinned stage slides sideways as the page scrolls down, its pictures bending with the speed.
- [sequence](/docs/effects/sequence.md): An image sequence scrubbed by scroll, frame by frame, from a poster <img>.
- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [How a layer follows scroll](/docs/concepts/scroll.md)
- [GSAP and Lenis](/docs/guides/gsap-lenis.md)
