video-hover
A thumbnail plays a short muted video preview while the pointer rests on it or its link has keyboard focus.
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
Rest the pointer on a film, or press Tab to reach one. The links lead nowhere in this example.
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. 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 video-hover<ul class="vh-grid"> <li> <a href="/films/aurum"> <img src="/media/previews/aurum.webp" 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" 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" 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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('img', 'video-hover', { scrub: 'off', duration: 0.3, delay: 0.15,})import { start } from '@dioramajs/diorama'import videoHover from '@dioramajs/media/video-hover'
const app = start()app.attach('img', videoHover({ scrub: 'off', duration: 0.3, delay: 0.15,}))Page transitions
The copy fades with the page while it plays; at rest the image is the DOM. 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 |
|---|---|---|---|
srcrebuildsdata-dio-src | string | '' | Video URL, or several by spaces such as WebM then MP4: the first playable one plays. |
scrubrebuildsdata-dio-scrub | 'off' | 'pointer' | 'off' |
|
durationdata-dio-duration | number0 to 1.5 s | 0.3 | Length of the crossfade between image and video. |
delaydata-dio-delay | number0 to 1 s | 0.15 | Hover time before the video loads and plays, so a passing pointer loads nothing. |
fromdata-dio-from | number0 to 60 s | 0 | Time in the video the preview starts from. |
loopdata-dio-loop | boolean | true | Plays the video again from the from time; off fades back to the image at its end. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
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. How a frame is decided.
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.
To run it on touch devices too, or to change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { 'video-hover': { when: 'all', reducedMotion: 'static' } } })Cost and quality
- Download
- 2.4 KB min+gzip 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.
- 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
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.
<div class="vh-clip"> <a href="/clips/plain-at-night"> <img src="/media/previews/blue-signal.webp" 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>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.
<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.
Aurum
Lounge chair
Blue Signal