# hover-preview

Over a list of projects, the picture of the item under the pointer follows the pointer, stands beside the item or holds a fixed place.

While the pointer is over an item, its picture floats at the pointer: it appears from a slight shrink, trails behind with a stretch along the motion, a soft bow and a tilt from the speed, and crossfades into the next item’s picture with a small settle. Leaving the list fades it out. In beside mode the picture stands next to the hovered item’s text and glides from row to row; in fixed mode it holds one place by the list and only crossfades. The list stays as you wrote it: text, links, focus and selection are the DOM, nothing is hidden.

Module `media`, category Galleries and viewers. Import `@dioramajs/media/hover-preview`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/hover-preview.ts)

An index of six design projects; the picture of the project under the pointer floats at the pointer and changes from row to row.

```html title="index.html"
<ol class="work" data-dio="hover-preview" data-dio-width="240">
  <li data-dio-src="/media/work/tranquility.webp"><a href="/work/tranquility">Tranquility</a> <span>Exhibition, 2025</span></li>
  <li data-dio-src="/media/work/polar-night.webp"><a href="/work/polar-night">Polar Night</a> <span>Identity, 2025</span></li>
  <li data-dio-src="/media/work/hares-fur.webp"><a href="/work/hares-fur">Hare’s Fur</a> <span>Catalogue, 2024</span></li>
  <li data-dio-src="/media/work/atlas-of-ice.webp"><a href="/work/atlas-of-ice">An Atlas of Ice</a> <span>Book, 2024</span></li>
  <li data-dio-src="/media/work/measure-of-the-sky.webp"><a href="/work/measure-of-the-sky">The Measure of the Sky</a> <span>Exhibition, 2023</span></li>
  <li data-dio-src="/media/work/cyanotype.webp"><a href="/work/cyanotype">Cyanotype Impressions</a> <span>Book, 2023</span></li>
</ol>
```

## Usage

Put it on: a list of links, such as an index of projects.

`start()` registers `hover-preview`, so the attribute alone puts it on an element.

```html
<ol class="index" data-dio="hover-preview" data-dio-width="360"><li data-dio-src="/work/north.avif"><a href="/work/north">North</a><span>2026</span></li><li data-dio-src="/work/tide.avif"><a href="/work/tide">Tide</a><span>2025</span></li></ol>
```

- `data-dio-src`: URL of the item’s picture; without it, the current source of the first <img> inside the item. An item with neither shows nothing.

In page transitions: The picture fades with the page as it leaves.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `mode` (rebuilds) | `data-dio-mode` | `'follow' \| 'beside' \| 'fixed'` | `'follow'` | follow floats the picture at the pointer; beside places it after the item’s text, gliding between rows, for a wide list; fixed keeps it at the middle of the list moved by offset, for a preview column next to the list. |
| `items` (rebuilds) | `data-dio-items` | `string` | `':scope > *'` | Selector of the items inside the list. |
| `width` | `data-dio-width` | `number` 80 to 800 px | `320` | Width of the picture. |
| `ratio` (rebuilds) | `data-dio-ratio` | `number \| 'auto'` 0.1 to 3 | `'auto'` | Width over height of the picture frame; auto takes the proportions of the picture. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0.15` | How far the picture trails the pointer or the row, as a time constant. |
| `stretch` | `data-dio-stretch` | `number` 0 to 1 | `0.4` | Stretch and bow from the pointer’s speed; 0 keeps the picture flat. |
| `tilt` | `data-dio-tilt` | `number` 0 to 30° | `6` | Most tilt from horizontal speed. |
| `duration` | `data-dio-duration` | `number` 0 to 1.5 s | `0.35` | Length of the appearance and the crossfade. |
| `offset` | `data-dio-offset` | `[number, number]` -400 to 400 px | `[0, 0]` | Shift of the picture’s center from the pointer, the item or the list’s middle, x and y down as in CSS. |
| `radius` | `data-dio-radius` | `number` 0 to 64 px | `0` | Corner radius of the picture. |

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

### Variants

- `mode`: `follow`, `beside`, `fixed`
  - `follow` floats the picture at the pointer.
  - `beside` places it after the item’s text, gliding between rows, for a wide list.
  - `fixed` keeps it at the middle of the list moved by offset, for a preview column next to the list.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion). Draws only while something on it moves, then stops. Only while the picture appears, moves, crossfades or leaves; none at rest, and pointer moves elsewhere on the page draw nothing. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The picture sits at its place without stretch, tilt or trailing; it appears and changes by a 0.15 s fade.

Skipped on touch devices, which never load the engine for it. Not loaded on touch screens: the effect only runs where (hover: hover) and (pointer: fine) match, and the list stays a list.

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

start({ effects: { 'hover-preview': { when: 'all', reducedMotion: 'off' } } })
```

## Cost and quality

Loads 3.5 KB the first time a page uses it. GPU: One small plane of 16 × 16 cells over everything, two texture reads during a crossfade. Memory: Up to eight pictures at a time: the item, its neighbors and the latest ones. Keep them at 800 px or less on the long side.

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

## Recipes

### A preview column beside the list

In `fixed` mode the picture holds one place: the middle of the list moved by `offset`, in CSS pixels, x and then y down. Lay the page out with a column for the preview and set `offset` to the distance between the list’s middle and the column’s. Here the list is 300 px wide and the column starts 40 px after it, so the picture moves 330 px to the right.

Five project names on the left; the picture of the name under the pointer appears in the frame on the right and crossfades from one project to the next.

```html title="index.html"
<div class="studio">
  <ul class="names" data-dio="hover-preview" data-dio-mode="fixed" data-dio-offset="330 0" data-dio-width="280">
    <li data-dio-src="/media/work/sea-steps.webp"><a href="/work/sea-steps">Sea Steps</a></li>
    <li data-dio-src="/media/work/outer-coast.webp"><a href="/work/outer-coast">Outer Coast</a></li>
    <li data-dio-src="/media/work/atlas-of-ice.webp"><a href="/work/atlas-of-ice">An Atlas of Ice</a></li>
    <li data-dio-src="/media/work/polar-night.webp"><a href="/work/polar-night">Polar Night</a></li>
    <li data-dio-src="/media/work/hares-fur.webp"><a href="/work/hares-fur">Hare’s Fur</a></li>
  </ul>
  <div class="preview-column" aria-hidden="true">Point at a name</div>
</div>
```

```ts title="script.ts"
// The project pages do not exist on this site: a click on a name stays on the page.
export default function mount(example: HTMLElement): () => void {
  const onClick = (e: MouseEvent) => {
    if ((e.target as Element).closest('.ex-root a')) e.preventDefault()
  }
  example.addEventListener('click', onClick)
  return () => example.removeEventListener('click', onClick)
}
```

### Keep srcset with an image inside the item

Without `data-dio-src` the picture is the current source of the first `<img>` inside the item, so the browser picks the file from `srcset` and `sizes` as usual. Hide the image with the `hidden` attribute and give it an empty alt, since the name next to it says the same. Set `ratio` when every picture should share one frame.

```html
<ul
  class="index"
  data-dio="hover-preview"
  data-dio-ratio="1.25"
>
  <li>
    <a href="/work/outer-coast">Outer Coast
      <img
        src="/work/outer-coast-480.webp"
        srcset="/work/outer-coast-480.webp 480w, /work/outer-coast-800.webp 800w"
        sizes="320px"
        hidden
      />
    </a>
  </li>
</ul>
```

### Names over the picture

To put the names on top of the picture, draw the canvas under the page with `canvas: 'behind'` and give the names `mix-blend-mode: difference`, so they stay readable over light and dark pictures alike. The page has to be transparent where the picture shows: move its color from `body` to the `background` option.

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

const app = start({ canvas: 'behind', background: '#f3e9d8' })
```

```html
<ol
  class="index"
  data-dio="hover-preview"
  data-dio-offset="40 -20"
  data-dio-radius="8"
>
  <li data-dio-src="/media/work/polar-night.webp">
    <a
      href="/work/polar-night"
      style="color: #fff; mix-blend-mode: difference"
    >Polar Night</a>
  </li>
</ol>
```

## Notes

The list stays the list you wrote: links, text selection and focus work as before, and on touch screens nothing loads. Keyboard focus shows the picture beside the focused item in every mode, so Tab through the example to see it. The pictures repeat what the names say, so they carry no alt text of their own.

Each picture is decoded once and kept while it is near the item under the pointer, up to eight at a time. Files of 800 px or less on the long side keep the first appearance quick; the example uses 480 px pictures.

`mode` and `items` rebuild the effect when they change. Set them in the markup rather than switching them while the pointer is over the list.

## Related

- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [coverflow](/docs/effects/coverflow.md): A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.
- [flipbook](/docs/effects/flipbook.md): The page images of a container become a book whose pages bend as they turn.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [lightbox](/docs/effects/lightbox.md): A click opens an image full screen: its copy flies out of the grid, pages and zooms.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [stack](/docs/effects/stack.md): The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.
- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [Performance and quality](/docs/guides/performance.md)
