API and events
lightbox() returns an object to open, move and close the box from your own code, and to listen for its events.
This gallery also has loop: true, so after the last photo comes the first.
live · API and events
<div id="controlled" class="row">
<a href="/media/photo-1-1600.jpg" data-dlb-width="1600" data-dlb-height="1066"><img src="/media/photo-1-480.jpg" alt="Diagonal bands of blue, orange, green and cream" /></a>
<a href="/media/photo-3-1600.jpg" data-dlb-width="1600" data-dlb-height="1066"><img src="/media/photo-3-480.jpg" alt="A thin blue line between pale blue and navy" /></a>
<a href="/media/photo-2-1600.jpg" data-dlb-width="1600" data-dlb-height="1066"><img src="/media/photo-2-480.jpg" alt="Dark teal over a thin orange stripe and orange" /></a>
</div>
<p><button type="button" class="open-last">Open the last photo</button></p>
<pre class="log" aria-live="polite"></pre>/* A row of thumbnails, 7rem high */
.row {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.row img {
display: block;
width: auto;
height: 7rem;
}
.log {
min-height: 6.5em;
max-height: 10em;
overflow-y: auto;
margin: 0;
padding: 0.5rem 0.75rem;
border: 1px solid color-mix(in srgb, currentColor 20%, transparent);
font-size: 0.8rem;
}import { lightbox } from "dialog-lightbox";
const log = document.querySelector(".log");
const box = lightbox({
gallery: document.querySelector("#controlled"),
dialog: document.querySelector("dialog.box"),
loop: true,
});
document.querySelector(".open-last").addEventListener("click", () => box.open(box.count - 1));
for (const type of ["open", "change", "close"]) {
box.on(type, (slide) => {
log.textContent += `${type.padEnd(6)} slide ${slide.index + 1} of ${box.count} (#dlb=${slide.id})\n`;
log.scrollTop = log.scrollHeight;
});
}

