---
title: "Image Gallery - Image"
description: "Auto-advancing image gallery composed from figure.image slides - per-slide data-duration dwell times, an abortable setTimeout chain, and every DOM touch routed through defuss-query (df$)."
slug: "image-gallery"
---

import { DocPage, PageHeader, SourceFiles } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="image-gallery" title="Image Gallery" component="image">
  Compositions on top of the Image component: a hover gallery that shows the next photo as the pointer crosses it (CSS only), and a crossfading, auto-advancing gallery where each slide declares its own dwell time in markup (<code>data-duration</code>) and a small defuss-query controller owns selection, state attributes, and the timer - no raw <code>querySelector</code>/<code>classList</code> anywhere.
</PageHeader>

```html example label="Auto-advancing gallery" schema="none" height="26" hint="Five figure.image slides crossfade on a re-armed setTimeout chain: each slide's data-duration is its dwell (the last slide has none and falls back to the Default dwell field). Prev/next reschedule the timer, the toggle pauses it - and every DOM read/write in the controller goes through df$ (defuss-query)."
<section
  class="image-gallery"
  id="igal"
  aria-label="Auto-advancing image gallery"
  style="max-width: 44rem; margin-inline: auto"
>
  <div class="igal-viewport">
    <div class="image-gallery-track">
      <figure class="image igal-slide" data-duration="3000" data-active>
        <img src="images/photos/1015-960x540.webp" alt="River winding through a mountain valley" />
        <figcaption class="image-caption igal-caption">River valley - dwell 3000ms (data-duration)</figcaption>
      </figure>
      <figure class="image igal-slide" data-duration="6000">
        <img src="images/photos/1016-960x540.webp" alt="Sunlit canyon walls" />
        <figcaption class="image-caption igal-caption">Canyon - dwell 6000ms (data-duration)</figcaption>
      </figure>
      <figure class="image igal-slide" data-duration="4000">
        <img src="images/photos/1018-960x540.webp" alt="Mountain lake under a clear sky" />
        <figcaption class="image-caption igal-caption">Mountain lake - dwell 4000ms (data-duration)</figcaption>
      </figure>
      <figure class="image igal-slide" data-duration="5000">
        <img src="images/photos/1025-960x540.webp" alt="Pug wrapped in a knitted blanket" />
        <figcaption class="image-caption igal-caption">Studio pug - dwell 5000ms (data-duration)</figcaption>
      </figure>
      <figure class="image igal-slide">
        <img src="images/photos/1035-960x540.webp" alt="Dark mountain ridge in fog" />
        <figcaption class="image-caption igal-caption"
          >Foggy ridge - no data-duration, uses the default dwell below</figcaption
        >
      </figure>
    </div>
  </div>
  <div class="igal-controls">
    <button type="button" class="btn" data-variant="outline" id="igal-prev">← Prev</button>
    <button type="button" class="btn" data-variant="outline" id="igal-play" aria-pressed="true">Pause</button>
    <button type="button" class="btn" data-variant="outline" id="igal-next">Next →</button>
    <output class="igal-counter" id="igal-counter" for="igal-prev igal-next igal-play" aria-live="polite">1 / 5</output>
  </div>
  <div class="igal-config">
    <label class="label" for="igal-dwell">Default dwell (ms)</label>
    <input
      class="input"
      type="number"
      id="igal-dwell"
      value="4000"
      min="500"
      max="20000"
      step="500"
      aria-describedby="igal-dwell-hint"
    />
    <p class="field-description" id="igal-dwell-hint"
      >Fallback for slides without their own data-duration - here: the foggy ridge.</p
    >
  </div>
</section>
<style>
  /* demo-scoped styles - the gallery is a docs pattern, not a shipped component;
     .image already supplies position/overflow/radius/object-fit, the data-active
     crossfade and the demo chrome are all that is left */
  .igal-viewport {
    position: relative;
    aspect-ratio: 16 / 9;
    border-radius: var(--radius-lg);
    background: var(--muted);
  }
  .igal-slide {
    position: absolute;
    inset: 0;
    opacity: 0;
    transform: scale(1.03);
    transition:
      opacity 480ms ease,
      transform 480ms ease;
    pointer-events: none;
  }
  .igal-slide[data-active] {
    opacity: 1;
    transform: none;
    pointer-events: auto;
  }
  .igal-caption {
    position: absolute;
    inset-inline: 0;
    bottom: 0;
    padding: 0.6rem 0.9rem;
    background: color-mix(in oklch, #000 55%, transparent);
    color: #fff;
  }
  .igal-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-block-start: 0.75rem;
  }
  .igal-counter {
    margin-inline-start: auto;
    font-variant-numeric: tabular-nums;
    font-size: 0.875rem;
    color: var(--muted-foreground);
  }
  .igal-config {
    display: grid;
    gap: 0.375rem;
    margin-block-start: 1rem;
    max-width: 16rem;
  }
  @media (prefers-reduced-motion: reduce) {
    .igal-slide {
      transition: none;
    }
  }
</style>
<script type="module">
  // all.js (df$) is injected into the sandbox as a classic script at end of
  // body; module scripts run after parsing, so df$ is already live here at
  // the top level. EVERY DOM touch goes through defuss-query - the point of
  // the demo: no querySelector, no classList, no setAttribute.
  const track = df$('#igal').find('.image-gallery-track');
  const slides = track.children('.igal-slide'); // snapshot selection; .length is fine to read
  const counter = df$('#igal-counter');
  const playBtn = df$('#igal-play');
  const dwellInput = df$('#igal-dwell');

  let index = 0;
  let playing = true;
  let timer = 0; // stored timeout id - cleared and re-armed on every nav, pause, or config change

  // dwell of slide i: its own data-duration, else the configured default
  const dwellFor = (i) => {
    const own = Number(slides.eq(i).data('duration'));
    const fallback = Number(dwellInput.val());
    return own > 0 ? own : fallback > 0 ? fallback : 4000;
  };

  function render(next) {
    index = ((next % slides.length) + slides.length) % slides.length; // wrap around both ways
    slides.attr('data-active', null); // null REMOVES the attribute…
    slides.eq(index).attr('data-active', ''); // …and the CSS crossfade keys on it
    counter.text(index + 1 + ' / ' + slides.length);
  }

  function schedule() {
    clearTimeout(timer);
    if (!playing) return; // pausing is just clearing the pending advance
    timer = setTimeout(() => {
      render(index + 1);
      schedule(); // the next slide waits its own dwell - no fixed interval
    }, dwellFor(index));
  }

  df$('#igal-prev').on('click', () => {
    render(index - 1);
    schedule();
  });
  df$('#igal-next').on('click', () => {
    render(index + 1);
    schedule();
  });
  playBtn.on('click', () => {
    playing = !playing;
    playBtn.text(playing ? 'Pause' : 'Play').attr('aria-pressed', String(playing));
    schedule();
  });
  // editing the fallback re-arms the CURRENT slide when it has no own dwell
  dwellInput.on('input', () => schedule());

  schedule(); // start auto-sliding
</script>
```

<p class="text-sm text-muted-foreground m-0 mb-3">The pattern: <code>df$</code> (defuss-query) drives every DOM touch - selection (<code>find</code> / <code>children</code> / <code>eq</code>), attribute reads and writes (<code>data</code> / <code>attr</code>), label text (<code>text</code>), control values (<code>val</code>) and events (<code>on</code>) - while the crossfade itself is pure CSS keyed on the <code>data-active</code> attribute. Auto-advance is one re-armed <code>setTimeout</code> instead of a fixed interval: each slide's dwell is its own <code>data-duration</code>, falling back to the "Default dwell" field, and every manual navigation, pause, or config edit clears and re-arms the timer - user control always wins over the schedule.</p>

<h2 id="hover-gallery" class="h2-display">Hover gallery</h2>

<p class="text-sm text-muted-foreground m-0 mb-3">daisyUI's hover gallery, CSS only: a <code>.hover-gallery</code> holds 2 - 10 images in one frame. The first shows; moving the pointer across the frame shows the others - the frame is sliced into as many invisible strips as there are images. Typical for product cards: glance through the photos without clicking. On touch screens (no hover) the same markup becomes a swipeable strip.</p>

```html example label="Hover gallery" schema="none" height="24" hint="Move the pointer from left to right across the picture - each fourth of the frame shows the next photo; back at the left edge the first returns."
<figure class="hover-gallery" style="max-width: 24rem; margin: 0">
  <img src="images/photos/1015-800x600.webp" alt="A fjord between steep cliffs" />
  <img src="images/photos/1016-800x600.webp" alt="A red canyon at sunset" />
  <img src="images/photos/1018-800x600.webp" alt="Green hills under clouds" />
  <img src="images/photos/1050-800x600.webp" alt="Sea stacks off a rocky coast" />
</figure>
```

```html example label="Product card" schema="none" height="30" hint="Inside a link card: the gallery previews the product, the whole card stays one link. data-indicator draws the position bar on hover."
<a class="card" href="#product" style="max-width: 18rem">
  <figure class="hover-gallery" data-indicator data-ratio="1/1" style="margin: 0; border-radius: 0">
    <img src="images/photos/0-600x600.webp" alt="The notebook open on a wooden desk" />
    <img src="images/photos/2-600x600.webp" alt="The notebook next to a coffee cup" />
    <img src="images/photos/48-600x600.webp" alt="The notebook lid, closed" />
    <img src="images/photos/201-600x600.webp" alt="The notebook with glasses and a mouse" />
  </figure>
  <div class="card-header">
    <h3 class="card-title">Notebook Air 13″</h3>
    <p class="card-description">Silver · 3 colors</p>
  </div>
  <div class="card-footer"><strong>€1,199</strong></div>
</a>
```

```html example label="Position bar" schema="none" height="24" hint="data-indicator shows n segments on hover (the bright one is the photo in view); data-indicator='always' keeps them visible - a hint that there is more to see."
<div class="grid gap-4" style="grid-template-columns: repeat(2, minmax(0, 18rem))">
  <figure class="hover-gallery" data-indicator style="margin: 0">
    <img src="images/photos/30-800x600.webp" alt="A patterned red mug" />
    <img src="images/photos/42-800x600.webp" alt="A café table by the window" />
    <img src="images/photos/431-800x600.webp" alt="A cappuccino with latte art" />
    <img src="images/photos/1-800x600.webp" alt="Coffee beside a laptop at work" />
  </figure>
  <figure class="hover-gallery" data-indicator="always" style="margin: 0">
    <img src="images/photos/1025-800x600.webp" alt="A pug wrapped in a blanket" />
    <img src="images/photos/1062-800x600.webp" alt="A pug under a blanket on a bed" />
    <img src="images/photos/1074-800x600.webp" alt="A lioness looking into the camera" />
    <img src="images/photos/1084-800x600.webp" alt="Walruses resting on a beach" />
  </figure>
</div>
```

```html example label="Zoom" schema="none" height="24" hint="data-effect='zoom' eases the photo in view slightly larger - a livelier preview. Reduced motion turns it off."
<figure class="hover-gallery" data-effect="zoom" data-indicator style="max-width: 24rem; margin: 0">
  <img src="images/photos/1035-800x600.webp" alt="A waterfall with a rainbow" />
  <img src="images/photos/1039-800x600.webp" alt="A waterfall in a green forest" />
  <img src="images/photos/1043-800x600.webp" alt="Pines below granite cliffs" />
  <img src="images/photos/1036-800x600.webp" alt="Snowy mountains in winter" />
</figure>
```

```html example label="Vertical zones" schema="none" height="30" hint="data-direction='vertical' slices the frame into rows instead of columns - for tall portraits, move the pointer top to bottom."
<figure class="hover-gallery" data-direction="vertical" data-ratio="3/4" style="max-width: 14rem; margin: 0">
  <img src="images/photos/64-600x800.webp" alt="A woman with sunglasses in a field" />
  <img src="images/photos/65-600x800.webp" alt="Hair lit by the evening sun" />
  <img src="images/photos/91-600x800.webp" alt="Hands holding a twin-lens camera" />
  <img src="images/photos/338-600x800.webp" alt="A hooded figure at the shore" />
</figure>
```

```html example label="Up to ten images" schema="none" height="24" hint="Ten photos, ten strips - the frame adapts to the count by itself (:has() counts the children)."
<figure class="hover-gallery" data-indicator="always" data-ratio="16/9" style="max-width: 32rem; margin: 0">
  <img src="images/photos/20-960x540.webp" alt="A desk with notebooks and a phone" />
  <img src="images/photos/26-960x540.webp" alt="Travel gear laid out on grey" />
  <img src="images/photos/30-960x540.webp" alt="A patterned red mug" />
  <img src="images/photos/96-960x540.webp" alt="A game controller on a table" />
  <img src="images/photos/160-960x540.webp" alt="A phone on a desk, close up" />
  <img src="images/photos/250-960x540.webp" alt="A vintage camera on piano keys" />
  <img src="images/photos/367-960x540.webp" alt="An e-reader held in the grass" />
  <img src="images/photos/403-960x540.webp" alt="The keys of an old typewriter" />
  <img src="images/photos/431-960x540.webp" alt="A cappuccino with latte art" />
  <img src="images/photos/91-960x540.webp" alt="A man holding a twin-lens camera" />
</figure>
```

<SourceFiles component="image" />

</DocPage>
