# carousel

Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.

The images stand around a ring seen a little from above, the front one larger, the ones at the back smaller and darker, with an optional reflection on the floor. A drag, a sideways wheel or the arrow keys turn the ring and it settles with a picture at the front; a click on a picture turns it to the front, a click on the front one follows its link. In inside the viewer stands at the middle of the ring and the pictures curve around like a wall. Autoplay steps or drifts round, with a pause button over the corner of the box.

Module `media`, category Galleries and viewers. Import `@dioramajs/media/carousel`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/carousel.ts)

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.

```html title="index.html"
<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" alt="The headland: a grassy point dropping into the sea under a pale sky" width="800" height="534" /></a></li>
  <li><a href="/boulders"><img src="/media/coast/boulders-600.avif" alt="The boulder beach: rounded stones at the edge of the water" width="600" height="750" /></a></li>
  <li><a href="/islets"><img src="/media/coast/islets-600.avif" alt="The islets: dark rocks in a blue bay, a flat-topped mountain in the haze" width="600" height="600" /></a></li>
  <li><a href="/surf"><img src="/media/coast/surf-800.avif" alt="The point break: surf over brown rocks at the foot of the cliffs" width="800" height="534" /></a></li>
  <li><a href="/slope"><img src="/media/coast/slope-800.avif" alt="The cliff path: a green slope falling to the sea" width="800" height="534" /></a></li>
  <li><a href="/shore"><img src="/media/coast/shore-800.avif" alt="The white shore: pale rocks and low bushes above a grey sea" width="800" height="534" /></a></li>
</ul>
```

## 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.

```html
<ul class="shelf" data-dio="carousel" data-dio-reflection="0.25" aria-label="New arrivals" style="touch-action: pan-y"><li><a href="/p/kettle"><img src="/shop/kettle.avif" alt="Enamel kettle" /></a></li><li><a href="/p/mug"><img src="/shop/mug.avif" alt="Stoneware mug" /></a></li><li><a href="/p/tray"><img src="/shop/tray.avif" alt="Oak tray" /></a></li></ul>
```

In page transitions: Fades with the page transition.

- Event `dio:index`, detail `{ index: number }`: A picture reached the front after a turn; index is its place among the images, from 0. Bubbles.

- Event `dio:open`, detail `{ index: number }`: The front picture was clicked and its image is in no link or button; index is its place. Bubbles.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `data-dio-index` | `number` 0 to 10 | `0` | Picture at the front from 0; a fraction holds the ring between two, for a tween. |
| `style` (rebuilds) | `data-dio-style` | `'ring' \| 'inside'` | `'ring'` | ring shows the ring from outside, the pictures facing out; inside puts the viewer at its middle, the pictures facing in and curving away at the sides. |
| `by` (rebuilds) | `data-dio-by` | `'drag' \| 'scroll'` | `'drag'` | drag turns the ring by pointer, wheel, keys, clicks and autoplay; scroll turns it once round as the list crosses the viewport, for a showcase in a long page. |
| `motion` (rebuilds) | `data-dio-motion` | `'step' \| 'drift'` | `'step'` | How autoplay turns it: step rests on each picture for interval seconds and moves on; drift turns slowly and steadily, a picture every interval seconds. |
| `interval` | `data-dio-interval` | `number` 0 to 10 s | `0` | Seconds per picture of autoplay, 0 for none. |
| `radius` (rebuilds) | `data-dio-radius` | `number \| 'auto'` 1 to 2000 px | `'auto'` | Radius of the ring in px; auto sizes it so the pictures stand spacing apart. |
| `spacing` | `data-dio-spacing` | `number` 0 to 2 | `0.4` | Gap between pictures on the ring, in widths of a picture. |
| `tilt` | `data-dio-tilt` | `number` -30 to 30° | `8` | How far from above the ring is seen; 0 at eye level. |
| `focus` | `data-dio-focus` | `number` 1 to 2 | `1.15` | Size of the front picture against the others; 1 for the same. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `0.5` | Darkening of the pictures at the back, 0 for none. |
| `reflection` | `data-dio-reflection` | `number` 0 to 1 | `0` | Opacity of the pictures mirrored on the floor, 0 for none. At most low 0. |
| `perspective` | `data-dio-perspective` | `number` 300 to 4000 px | `1200` | Distance of the eye from the ring, like CSS perspective; lower is stronger. |
| `duration` | `data-dio-duration` | `number` 0.15 to 3 s | `0.7` | Length of a turn by one picture. |
| `drag` | `data-dio-drag` | `boolean` | `true` | A drag or a swipe turns the ring. |
| `wheel` | `data-dio-wheel` | `boolean` | `true` | A sideways wheel or trackpad swipe turns the ring. |
| `click` | `data-dio-click` | `boolean` | `true` | A click on a picture turns it to the front. |
| `pause` | `data-dio-pause` | `boolean` | `true` | Shows the pause button while autoplay runs; off, the page must give a control of its own. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the images inside the list. |

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

### Variants

- `style`: `ring`, `inside`
  - `ring` shows the ring from outside, the pictures facing out.
  - `inside` puts the viewer at its middle, the pictures facing in and curving away at the sides.
- `motion`: `step`, `drift`
  - `step` rests on each picture for interval seconds and moves on.
  - `drift` turns slowly and steadily, a picture every interval seconds.
- `by`: `drag`, `scroll`
  - `drag` turns the ring by pointer, wheel, keys, clicks and autoplay.
  - `scroll` turns it once round as the list crosses the viewport, for a showcase in a long page.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-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. See [how a frame is decided](/docs/concepts/frames).

## 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.

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

start({ effects: { carousel: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 8.3 KB 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.

- low: No reflection.

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

## 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.

Five hotel rooms on a ring that turns every 3 seconds, a pause button in the corner.

```html title="index.html"
<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" alt="The Ocean Room: a king bed with a red throw, doors open to a balcony and the sea" width="1200" height="900" /></li>
  <li><img src="/media/rooms/glass.avif" alt="The Glass Room: a bed under steel-framed windows looking over dry hills" width="1200" height="900" /></li>
  <li><img src="/media/rooms/garden.avif" alt="The Garden Room: a double bed in white linen beside a tall window" width="1200" height="900" /></li>
  <li><img src="/media/rooms/studio.avif" alt="The Loft: a low bed against a brick wall and wide wooden floorboards" width="1200" height="900" /></li>
  <li><img src="/media/rooms/twin.avif" alt="The Twin Room: a garden window and a tea tray on a wooden stand" 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.

Eight project covers on a wall that curves around the viewer.

```html title="index.html"
<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" alt="Cyanotype Impressions: white fronds of seaweed on deep blue" width="1200" height="800" /></a></li>
  <li><a href="/atlas-of-ice"><img src="/media/work/atlas-of-ice.webp" alt="Atlas of Ice: snow-covered ridges under a pale sky" width="1200" height="800" /></a></li>
  <li><a href="/hares-fur"><img src="/media/work/hares-fur.webp" alt="Hare’s Fur: a dark tea bowl with a streaked glaze" width="1200" height="800" /></a></li>
  <li><a href="/measure-of-the-sky"><img src="/media/work/measure-of-the-sky.webp" alt="The Measure of the Sky: the brass face of an astrolabe" width="1200" height="800" /></a></li>
  <li><a href="/polar-night"><img src="/media/work/polar-night.webp" alt="Polar Night: green aurora over the night side of the Earth" width="1200" height="800" /></a></li>
  <li><a href="/outer-coast"><img src="/media/work/outer-coast.webp" alt="Outer Coast: dark cliffs above a grey sea under clouds" width="1200" height="800" /></a></li>
  <li><a href="/sea-steps"><img src="/media/work/sea-steps.webp" alt="Sea Steps: concrete steps into a grey sea" width="1200" height="800" /></a></li>
  <li><a href="/tranquility"><img src="/media/work/tranquility.webp" alt="Tranquility: boulders on grey lunar ground" 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`.

```html
<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.

## Related

- [coverflow](/docs/effects/coverflow.md): A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.
- [flipbook](/docs/effects/flipbook.md): The page images of a container become a book whose pages bend as they turn.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [hover-preview](/docs/effects/hover-preview.md): 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.
- [lightbox](/docs/effects/lightbox.md): A click opens an image full screen: its copy flies out of the grid, pages and zooms.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [stack](/docs/effects/stack.md): The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [Reduced motion](/docs/guides/reduced-motion.md)
