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.
- Tranquility Exhibition, 2025
- Polar Night Identity, 2025
- Hare’s Fur Catalogue, 2024
- An Atlas of Ice Book, 2024
- The Measure of the Sky Exhibition, 2023
- Cyanotype Impressions Book, 2023
Live preview needs WebGPU or WebGL 2. An index of six design projects; the picture of the project under the pointer floats at the pointer and changes from row to row.
This example failed to draw.
Move the pointer over the list, or press Tab to walk the links. The links lead nowhere in this example.
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. 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 hover-preview<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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.work', 'hover-preview', { mode: 'follow', width: 240, ease: 0.15, stretch: 0.4, tilt: 6, duration: 0.35, offset: [0, 0], radius: 0,})import { start } from '@dioramajs/diorama'import hoverPreview from '@dioramajs/media/hover-preview'
const app = start()app.attach('.work', hoverPreview({ mode: 'follow', width: 240, ease: 0.15, stretch: 0.4, tilt: 6, duration: 0.35, offset: [0, 0], radius: 0,}))Markup it reads
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.
Page transitions
The picture fades with the page as it leaves. 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 |
|---|---|---|---|
moderebuildsdata-dio-mode | 'follow' | 'beside' | 'fixed' | 'follow' |
|
itemsrebuildsdata-dio-items | string | ':scope > *' | Selector of the items inside the list. |
widthdata-dio-width | number80 to 800 px | 320 | Width of the picture. |
ratiorebuildsdata-dio-ratio | number | 'auto'0.1 to 3 | 'auto' | Width over height of the picture frame; auto takes the proportions of the picture. |
easedata-dio-ease | number0 to 1 s | 0.15 | How far the picture trails the pointer or the row, as a time constant. |
stretchdata-dio-stretch | number0 to 1 | 0.4 | Stretch and bow from the pointer’s speed; 0 keeps the picture flat. |
tiltdata-dio-tilt | number0 to 30° | 6 | Most tilt from horizontal speed. |
durationdata-dio-duration | number0 to 1.5 s | 0.35 | Length of the appearance and the crossfade. |
offsetdata-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. |
radiusdata-dio-radius | number0 to 64 px | 0 | Corner radius of the picture. |
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, opacity and 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. How a frame is decided.
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.
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: { 'hover-preview': { when: 'all', reducedMotion: 'off' } } })Cost and quality
- Download
- 3.5 KB min+gzip 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.
- 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
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.
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
<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>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.
<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.
import { start } from '@dioramajs/diorama'
const app = start({ canvas: 'behind', background: '#f3e9d8' })<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.