gallery
A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
Live preview needs WebGPU or WebGL 2. Five hotel rooms in one box; a click on the picture or the Next button changes it with a soft, noisy wave.
This example failed to draw.
The Garden Room
Usage
Put it on a container of images, such as a scroll-snap strip. start() registers gallery, 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 gallery<div class="rooms" data-dio="gallery" style="touch-action: pan-y"> <img src="/media/rooms/garden.avif" width="1200" height="900" /> <img src="/media/rooms/ocean.avif" width="1200" height="900" /> <img src="/media/rooms/twin.avif" width="1200" height="900" /> <img src="/media/rooms/glass.avif" width="1200" height="900" /> <img src="/media/rooms/studio.avif" width="1200" height="900" /></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.rooms', 'gallery', { style: 'displace', duration: 1.1, strength: 0.25, interval: 0, click: true, swipe: true, loop: true,})import { start } from '@dioramajs/diorama'import gallery from '@dioramajs/media/gallery'
const app = start()app.attach('.rooms', gallery({ style: 'displace', duration: 1.1, strength: 0.25, interval: 0, click: true, swipe: true, loop: true,}))Events
It dispatches these on its element; they bubble.
dio:index,detail: { index: number }. A picture is reached, at the end of a change or when a tween stops on a whole index; index is its place among the pictures, from 0. Bubbles, for captions, dots and counters.
Page transitions
Fades with the page transition; a keyed flight carries the current picture. 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 |
|---|---|---|---|
indexdata-dio-index | number0 to 10 | 0 | Picture shown from 0, counting on past the last when it loops; a fraction holds a point between two. |
durationdata-dio-duration | number0.2 to 3 s | 1.1 | Length of a change. |
stylerebuildsdata-dio-style | 'displace' | 'slide' | 'fade' | 'zoom' | 'displace' |
|
strengthdata-dio-strength | number0 to 1 | 0.25 | How far the displacement pushes the pictures. |
intervaldata-dio-interval | number0 to 10 s | 0 | Time before the next picture, 0 for none; the timer pauses on hover and with focus inside. |
clickdata-dio-click | boolean | true | A click on the container shows the next picture. |
swipedata-dio-swipe | boolean | true | A horizontal swipe changes the picture; needs touch-action: pan-y on the container. |
loopdata-dio-loop | boolean | true | After the last picture comes the first. Off, the ends hold: a click or swipe past them does nothing and the interval timer stops at the last. |
itemsrebuildsdata-dio-items | string | 'img' | Selector of the images inside the container. |
seedrebuildsdata-dio-seed | number1 to 100 | 1 | Shape of the displacement. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads motion and size
. Draws only while something on it moves, then stops. Only during a change; none at rest. The interval timer draws nothing while it waits, and it pauses under the pointer, with focus inside, off screen and in a background tab. 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. Every change is a short crossfade, and the interval timer does not advance.
Runs on every device. A tap shows the next picture. A swipe works when the container has touch-action: pan-y, so the page keeps vertical scrolling; without it the gesture belongs to the page.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { gallery: { reducedMotion: 'off' } } })Cost and quality
- Download
- 3.0 KB min+gzip the first time a page uses it
- GPU
- Two or three texture reads per pixel, four during a displacement.
- Memory
- Up to three pictures at a time, the current one and its neighbors, however long the gallery is.
- 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 product gallery with thumbnails
Set style to slide for the carousel move shoppers expect, and loop to false so the first and last views hold instead of wrapping around. The thumbnails sit under the box, not in it: the gallery’s copy covers the whole box, so anything inside would be hidden.
Live preview needs WebGPU or WebGL 2. A lounge chair in five views that slide in from the side, chosen with a row of thumbnails.
This example failed to draw.
<div class="product"> <div class="shots" data-dio="gallery" data-dio-style="slide" data-dio-loop="false" style="touch-action: pan-y" > <img src="/media/chair/front.webp" width="960" height="640" /> <img src="/media/chair/side.webp" width="960" height="640" /> <img src="/media/chair/back.webp" width="960" height="640" /> <img src="/media/chair/tuft.webp" width="960" height="640" /> <img src="/media/chair/leg.webp" width="960" height="640" /> </div> <div class="thumbs"> <button type="button" data-show="0" aria-label="Front"> <img src="/media/chair/front.webp" width="96" height="64" /> </button> <button type="button" data-show="1" aria-label="Side"> <img src="/media/chair/side.webp" width="96" height="64" /> </button> <button type="button" data-show="2" aria-label="Back"> <img src="/media/chair/back.webp" width="96" height="64" /> </button> <button type="button" data-show="3" aria-label="Tufting" > <img src="/media/chair/tuft.webp" width="96" height="64" /> </button> <button type="button" data-show="4" aria-label="Leg"> <img src="/media/chair/leg.webp" width="96" height="64" /> </button> </div></div>Each thumbnail sets index on the instance, and the dio:index event marks the view on screen. The event also fires after a click or a swipe on the picture, so the row stays in step.
const shots = document.querySelector('.shots')const thumbs = [...document.querySelectorAll('.thumbs button')]
thumbs.forEach((thumb, i) => { thumb.addEventListener('click', () => app.effects.get(shots, 'gallery')?.set({ index: i }), )})shots.addEventListener('dio:index', (e) => { thumbs.forEach((thumb, i) => thumb.setAttribute('aria-current', String(i === e.detail.index)), )})A hero slideshow with a pause button
The zoom style fades each picture in as it settles from a slight zoom, a calm move for the first screen of a hotel or a restaurant. With interval set to 5 the pictures change every 5 seconds; the timer waits while the pointer is over the box or focus is inside it. Content that moves on its own for longer than 5 seconds needs a control that stops it (WCAG 2.2.2), so give the slideshow a pause button.
Live preview needs WebGPU or WebGL 2. Four rooms of a house that change every 5 seconds, each settling from a slight zoom, with a pause button under them.
This example failed to draw.
<section class="house-hero" aria-label="The house"> <div class="house" data-dio="gallery" data-dio-style="zoom" data-dio-interval="5" style="touch-action: pan-y" > <img src="/media/house/terrace.avif" width="1200" height="515" /> <img src="/media/house/drawing-room.avif" width="1000" height="667" /> <img src="/media/house/dining-room.avif" width="1000" height="750" /> <img src="/media/house/salon.avif" width="1000" height="667" /> </div> <button type="button" class="house-pause" aria-pressed="false" >Pause</button></section>The button turns interval off and on again and keeps aria-pressed in step.
const house = document.querySelector('.house')const pause = document.querySelector('.house-pause')
pause.addEventListener('click', () => { const paused = pause.getAttribute('aria-pressed') === 'true' app.effects.get(house, 'gallery')?.set({ interval: paused ? 5 : 0 }) pause.setAttribute('aria-pressed', String(!paused))})Name the picture on screen
Nothing in the page tells a screen reader that the picture changed. Put a caption with aria-live="polite" under the box and fill it from dio:index, whose index is the place of the picture reached, from 0. The example at the top of this page names the room and counts the pictures this way.
<div class="rooms" data-dio="gallery" style="touch-action: pan-y"> <img src="/media/rooms/garden.avif" /> <img src="/media/rooms/ocean.avif" /></div><p class="room-name" aria-live="polite">The Garden Room</p>const rooms = document.querySelector('.rooms')const name = document.querySelector('.room-name')const pictures = [...rooms.querySelectorAll('img')]
rooms.addEventListener('dio:index', (e) => { name.textContent = pictures[e.detail.index].alt.split(':')[0]})Scrub the pictures with the scroll
A fraction of index holds the exact point between two pictures, so a scrubbed tween plays each change at the pace of the scroll: the rooms of a house pass by as the reader moves down the page. Turn click and swipe off, so the scroll is the only thing that moves the pictures. GSAP and Lenis covers tweens of params.
import gsap from 'gsap'import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
const rooms = app.attach('.rooms', 'gallery', { click: false, swipe: false,})gsap.to(rooms.params, { index: 4, ease: 'none', scrollTrigger: { trigger: '.rooms', start: 'top 70%', end: 'bottom 30%', scrub: true, },})Notes
The copy covers the container’s whole box and frames each picture by that image’s own object-fit and object-position. Give every image the size of the box, as a scroll-snap strip with flex: 0 0 100% does, and keep captions, arrows and dots outside it. Without WebGL the strip is what visitors see and swipe.
The images stay in the page in their order, each with its alt text, so a screen reader can reach all of them. The gallery takes no keys and the box takes no focus: give keyboard users buttons that set index, as the recipes do.
style, items and seed rebuild the effect when they change. Set them in the markup rather than switching them while the gallery is on screen.