carousel
Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
Live preview needs WebGPU or WebGL 2. Six coastal places on a ring seen from slightly above, the front one larger and mirrored on the floor; drag, swipe or use the arrow keys to turn it.
This example failed to draw.
Usage
Put it on a list of linked images: products, a team, projects. start() registers carousel, 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 carousel<ul class="coast" data-dio="carousel" data-dio-reflection="0.25" aria-label="Six places on the coast. Drag, swipe or use the arrow keys to turn the ring; the picture at the front opens its place." style="touch-action: pan-y"> <li> <a href="/headland"> <img src="/media/coast/headland-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/boulders"> <img src="/media/coast/boulders-600.avif" width="600" height="750" /> </a> </li> <li> <a href="/islets"> <img src="/media/coast/islets-600.avif" width="600" height="600" /> </a> </li> <li> <a href="/surf"> <img src="/media/coast/surf-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/slope"> <img src="/media/coast/slope-800.avif" width="800" height="534" /> </a> </li> <li> <a href="/shore"> <img src="/media/coast/shore-800.avif" width="800" height="534" /> </a> </li></ul>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.coast', 'carousel', { style: 'ring', motion: 'step', by: 'drag', spacing: 0.4, tilt: 8, focus: 1.15, shade: 0.5, reflection: 0.25, perspective: 1200, duration: 0.7,})import { start } from '@dioramajs/diorama'import carousel from '@dioramajs/media/carousel'
const app = start()app.attach('.coast', carousel({ style: 'ring', motion: 'step', by: 'drag', spacing: 0.4, tilt: 8, focus: 1.15, shade: 0.5, reflection: 0.25, perspective: 1200, duration: 0.7,}))Events
It dispatches these on its element; they bubble.
dio:index,detail: { index: number }. A picture reached the front after a turn; index is its place among the images, from 0. Bubbles.dio:open,detail: { index: number }. The front picture was clicked and its image is in no link or button; index is its place. Bubbles.
Parts
Its own controls take your styles through ::part().
pause: The pause button of autoplay, after the list in the page; aria-pressed says whether it holds.
Page transitions
Fades with the page transition. 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 at the front from 0; a fraction holds the ring between two, for a tween. |
stylerebuildsdata-dio-style | 'ring' | 'inside' | 'ring' |
|
byrebuildsdata-dio-by | 'drag' | 'scroll' | 'drag' |
|
motionrebuildsdata-dio-motion | 'step' | 'drift' | 'step' |
|
intervaldata-dio-interval | number0 to 10 s | 0 | Seconds per picture of autoplay, 0 for none. |
radiusrebuildsdata-dio-radius | number | 'auto'1 to 2000 px | 'auto' | Radius of the ring in px; auto sizes it so the pictures stand spacing apart. |
spacingdata-dio-spacing | number0 to 2 | 0.4 | Gap between pictures on the ring, in widths of a picture. |
tiltdata-dio-tilt | number-30 to 30° | 8 | How far from above the ring is seen; 0 at eye level. |
focusdata-dio-focus | number1 to 2 | 1.15 | Size of the front picture against the others; 1 for the same. |
shadedata-dio-shade | number0 to 1 | 0.5 | Darkening of the pictures at the back, 0 for none. |
reflectiondata-dio-reflection | number0 to 1 | 0 | Opacity of the pictures mirrored on the floor, 0 for none.At most low 0 by quality tier. |
perspectivedata-dio-perspective | number300 to 4000 px | 1200 | Distance of the eye from the ring, like CSS perspective; lower is stronger. |
durationdata-dio-duration | number0.15 to 3 s | 0.7 | Length of a turn by one picture. |
dragdata-dio-drag | boolean | true | A drag or a swipe turns the ring. |
wheeldata-dio-wheel | boolean | true | A sideways wheel or trackpad swipe turns the ring. |
clickdata-dio-click | boolean | true | A click on a picture turns it to the front. |
pausedata-dio-pause | boolean | true | Shows the pause button while autoplay runs; off, the page must give a control of its own. |
itemsrebuildsdata-dio-items | string | 'img' | Selector of the images inside the list. |
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, height, opacity, reducedMotion, size, width, x and y
. Draws only while something on it moves, then stops. While the ring turns and the whole time drift plays; with step autoplay only during each step. None at rest. 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. No autoplay and no pause button, no turning with the page scroll; changes are at once. Keys, clicks and swipes still turn the ring.
Runs on every device. A swipe turns the ring with touch-action: pan-y on the list; without it the browser scrolls the list and the ring follows that scroll. A tap on a picture turns it to the front, on the front one follows its link.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { carousel: { reducedMotion: 'off' } } })Cost and quality
- Download
- 8.3 KB min+gzip the first time a page uses it
- GPU
- A quad per picture, up to 24, one texture read per pixel; the reflection doubles the pixels of each.
- Memory
- Every picture on the ring.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | No reflection. |
Recipes
Rooms that turn by themselves, with a pause button
With interval set to 3 the ring rests on each room for 3 seconds and turns on to the next. Content that moves on its own for longer than 5 seconds needs a way to stop it (WCAG 2.2.2), so autoplay brings its own pause button over the bottom right corner of the box. The ring also waits while the pointer is over it, while focus is inside it, in a background tab, and for 4 seconds after someone turns it by hand.
Live preview needs WebGPU or WebGL 2. Five hotel rooms on a ring that turns every 3 seconds, a pause button in the corner.
This example failed to draw.
<ul class="suites" data-dio="carousel" data-dio-interval="3" data-dio-tilt="4" data-dio-focus="1.25" aria-label="Five rooms of the hotel, turning by themselves; the button in the corner pauses them." style="touch-action: pan-y"> <li> <img src="/media/rooms/ocean.avif" width="1200" height="900" /> </li> <li> <img src="/media/rooms/glass.avif" width="1200" height="900" /> </li> <li> <img src="/media/rooms/garden.avif" width="1200" height="900" /> </li> <li> <img src="/media/rooms/studio.avif" width="1200" height="900" /> </li> <li> <img src="/media/rooms/twin.avif" width="1200" height="900" /> </li></ul>Set motion to drift for a ring that turns slowly and steadily instead, a room every interval seconds. The button is a real <button> placed right after the list in the page, in the tab order after the pictures; style it from the page with ::part(pause). If the page has a control of its own, set pause to false and let that control set interval to 0.
A curved wall of projects
The inside style puts the viewer in the middle of the ring: the pictures face in and curve away at the sides, like a gallery wall around you. The ones beside and behind you fade out, so it reads well with eight pictures or more.
<ul class="wall" data-dio="carousel" data-dio-style="inside" data-dio-tilt="0" data-dio-focus="1" data-dio-spacing="0.1" aria-label="Eight projects on a curved wall. Drag, swipe or use the arrow keys to turn it." style="touch-action: pan-y"> <li> <a href="/cyanotype"> <img src="/media/work/cyanotype.webp" width="1200" height="800" /> </a> </li> <li> <a href="/atlas-of-ice"> <img src="/media/work/atlas-of-ice.webp" width="1200" height="800" /> </a> </li> <li> <a href="/hares-fur"> <img src="/media/work/hares-fur.webp" width="1200" height="800" /> </a> </li> <li> <a href="/measure-of-the-sky"> <img src="/media/work/measure-of-the-sky.webp" width="1200" height="800" /> </a> </li> <li> <a href="/polar-night"> <img src="/media/work/polar-night.webp" width="1200" height="800" /> </a> </li> <li> <a href="/outer-coast"> <img src="/media/work/outer-coast.webp" width="1200" height="800" /> </a> </li> <li> <a href="/sea-steps"> <img src="/media/work/sea-steps.webp" width="1200" height="800" /> </a> </li> <li> <a href="/tranquility"> <img src="/media/work/tranquility.webp" width="1200" height="800" /> </a> </li></ul>Turn with the page scroll
With by set to scroll the ring turns once round while the list crosses the viewport, a showcase that plays as the page goes by. Drags, keys and autoplay are off then, and under reduced motion the ring stands still at index.
<ul class="range" data-dio="carousel" data-dio-by="scroll" data-dio-tilt="12"> <li><img src="/shop/kettle.avif" /></li> <li><img src="/shop/mug.avif" /></li></ul>Notes
Write the ring as a list of images, in links if each opens a page, styled as the grid or the row you want without WebGL. Each picture takes the size of its image in that layout, so size the images for the ring (the example gives them most of the frame’s height in a row that scrolls), and its picture is cut like the image, with the image’s own object-fit and object-position; the corners follow the border-radius of the first image. The ring stands on the middle line of the list’s box; with radius at auto (the default) it is wide enough that the pictures stand spacing apart, and a ring too wide for the box is drawn smaller as a whole. Up to 24 pictures go on the ring.
The copy hides the list, its background included, so put a background on a parent. A click on a picture is caught by the effect: on a picture at the side it turns that one to the front, on the front one it follows its link, with Ctrl or Cmd in a new tab. An image without a link fires dio:open instead.
The links stay in the page in order with their alt text. Tab moves through them and the ring turns to the focused one; the arrow keys, PageUp, PageDown, Home and End turn the ring and move the focus with it. Without links give the list tabindex="0" and an aria-label that says how to turn it.
A drag with a mouse always turns the ring. On touch screens set touch-action: pan-y on the list so the ring follows the finger while the page still scrolls up and down. Only a sideways wheel or a two-finger swipe on a trackpad turns it; a wheel turned up and down scrolls the page.
style, by, motion and items rebuild the effect when they change. Set them in the markup rather than switching them while the ring is on screen.


















