# video-hover

A thumbnail plays a short muted video preview while the pointer rests on it or its link has keyboard focus.

At rest the thumbnail is your image. Hovered for a moment, it crossfades into a muted video that plays in the same frame, cropped like the image, and fades back to the image when the pointer leaves. With scrub, the pointer’s position across the thumbnail picks the moment of the video instead, and a thin bar along the bottom edge shows where you are. The video downloads only when someone hovers or focuses the thumbnail.

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

Three film thumbnails in a grid; resting the pointer on one crossfades it into a muted preview of the film, and leaving fades back to the still.

```html title="index.html"
<ul class="vh-grid">
  <li>
    <a href="/films/aurum">
      <img src="/media/previews/aurum.webp" alt="" width="640" height="360" data-dio="video-hover" data-dio-src="/media/previews/aurum.webm /media/previews/aurum.mp4" />
      <strong>Aurum</strong>
      <span>Product film, 0:12</span>
    </a>
  </li>
  <li>
    <a href="/films/lounge">
      <img src="/media/previews/lounge.webp" alt="" width="640" height="360" data-dio="video-hover" data-dio-src="/media/previews/lounge.webm /media/previews/lounge.mp4" />
      <strong>Lounge chair</strong>
      <span>Interior, 0:12</span>
    </a>
  </li>
  <li>
    <a href="/films/blue-signal">
      <img src="/media/previews/blue-signal.webp" alt="" width="640" height="360" data-dio="video-hover" data-dio-src="/media/previews/blue-signal.webm /media/previews/blue-signal.mp4" />
      <strong>Blue Signal</strong>
      <span>Festival trailer, 0:18</span>
    </a>
  </li>
</ul>
```

## Usage

Put it on: an img thumbnail, usually inside a link: course and product cards, portfolio grids, stock footage.

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

```html
<a href="/courses/ceramics"><img src="/courses/ceramics.webp" alt="Throwing a bowl on the wheel" data-dio="video-hover" data-dio-src="/courses/ceramics.webm /courses/ceramics.mp4"><span>Ceramics for beginners</span></a>
```

In page transitions: The copy fades with the page while it plays; at rest the image is the DOM.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `src` (rebuilds) | `data-dio-src` | `string` | `''` | Video URL, or several by spaces such as WebM then MP4: the first playable one plays. |
| `scrub` (rebuilds) | `data-dio-scrub` | `'off' \| 'pointer'` | `'off'` | off plays from the from time while hovered; pointer seeks by the pointer’s x, for footage. |
| `duration` | `data-dio-duration` | `number` 0 to 1.5 s | `0.3` | Length of the crossfade between image and video. |
| `delay` | `data-dio-delay` | `number` 0 to 1 s | `0.15` | Hover time before the video loads and plays, so a passing pointer loads nothing. |
| `from` | `data-dio-from` | `number` 0 to 60 s | `0` | Time in the video the preview starts from. |
| `loop` | `data-dio-loop` | `boolean` | `true` | Plays the video again from the from time; off fades back to the image at its end. |

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

### Variants

- `scrub`: `off`, `pointer`
  - `off` plays from the from time while hovered.
  - `pointer` seeks by the pointer’s x, for footage.

## Inputs and frames

Draws only while something on it moves, then stops. Only while the video plays or crossfades, at the video’s own frame rate; none at rest, and none for pointer moves while the image shows. 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 image stays as it is and no video loads.

Skipped on touch devices, which never load the engine for it. Not loaded on touch screens: the effect runs only where (hover: hover) and (pointer: fine) match, so a tap opens the link as usual and no video downloads on mobile data.

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

start({ effects: { 'video-hover': { when: 'all', reducedMotion: 'static' } } })
```

## Cost and quality

Loads 2.4 KB the first time a page uses it. GPU: One texture read of the video per pixel of the thumbnail while it shows. Memory: One video frame on the GPU while the preview shows, freed when it fades out. Keep previews short and small: 5 to 10 seconds at 640 px wide.

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

## Recipes

### Footage you browse by the pointer

With `data-dio-scrub="pointer"` the video does not play. The pointer’s position across the thumbnail picks the moment, from `from` at the left edge to the end at the right, and a thin bar along the bottom shows where you are. Stock footage libraries and video editors use it so a visitor can see the whole clip in a second. `delay` is 0 here, so the first move already loads the clip.

One clip of a plain at night with light beams; moving the pointer across it from left to right steps through the clip, and a bar at the bottom follows.

```html title="index.html"
<div class="vh-clip">
  <a href="/clips/plain-at-night">
    <img src="/media/previews/blue-signal.webp" alt="The plain at night" width="640" height="360" data-dio="video-hover" data-dio-src="/media/previews/blue-signal.webm /media/previews/blue-signal.mp4" data-dio-scrub="pointer" data-dio-delay="0" />
  </a>
  <p><strong>The plain at night</strong> <span>HD, 25 fps, 0:05</span></p>
</div>
```

```ts title="script.ts"
// The film pages do not exist on this site: a click on a card 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)
}
```

Seeking jumps to the nearest keyframe and decodes from there, so encode scrub clips with a keyframe every half second or so, for example with `-g 12` in ffmpeg at 25 fps. The preview files in these examples are encoded that way.

### WebM first, MP4 as a fallback

`src` takes several URLs separated by spaces, and the browser plays the first one it supports, as with `<source>` elements. Put the smaller WebM first and an H.264 MP4 after it for browsers without WebM.

```html
<img
  src="/films/trailer.webp"
  data-dio="video-hover"
  data-dio-src="/films/trailer.webm /films/trailer.mp4"
  data-dio-from="12"
/>
```

`from` skips an intro or a black opening: here the preview begins 12 seconds in.

## Notes

The thumbnail stays a plain `<img>`, so its `alt`, `srcset`, lazy loading and the link around it work as without the effect. The preview repeats what the image shows, so it adds no text of its own. Hovering the link or button around the image plays the preview too, and keyboard focus on that link plays it as well.

Nothing about the video is downloaded until the pointer has rested on the card for `delay`, so a pointer passing over a grid of thirty cards loads nothing. After that the browser keeps what it has buffered, and the second hover starts at once. Keep preview files short and small: the films here are 5 seconds at 640 pixels wide, about 300 KB each.

A video from another origin needs CORS headers: the effect loads it in CORS mode, and without `Access-Control-Allow-Origin` it fails to load. A development warning `EFFECT_MEDIA` names the URL, and the image stays as it is.

`src` and `scrub` rebuild the effect when they change. Set them in the markup.

## Related

- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [chroma](/docs/effects/chroma.md): A video shot on a green or blue screen, played over the page with the screen keyed out.
- [scrub](/docs/effects/scrub.md): A video scrubbed by scroll: its time follows the sticky section it plays in, forward and back.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [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.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [Performance and quality](/docs/guides/performance.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
