Moey extras
Lightbox
A full-screen viewer for pictures, video, audio, YouTube, Vimeo, pages and any HTML — made for fingers first: the picture follows your finger to the next one, swipe down to close, pinch or double-tap to zoom, one tap to hide everything but the picture. On a computer: arrows, thumbnails, the mouse wheel and the keyboard. Every part can be switched on or off.
<div class="grid grid-cols-3 md:grid-cols-4 gap-2" data-moey-lightbox>
<a href="lisbon-big.jpg" data-caption="Lisbon at dawn"><img src="lisbon-small.jpg" alt="Lisbon at dawn"></a>
<a href="lake-big.jpg" data-caption="A lake up north"><img src="lake-small.jpg" alt="A lake up north"></a>
…
</div>
Step by step
- Link to the big version:
<a href="big.jpg"><img src="small.jpg" alt="…"></a>. Without JavaScript the link still opens it. - Mark the gallery with
data-moey-lightbox— every link inside opens the viewer, the others become the ones you swipe to. Or mark single links, or give links anywhere on the page the same name to make them one set:data-moey-lightbox="team". - Caption with
data-caption(else the figure'sfigcaption, else the image'salt). - Pick the parts you want (below), right on the gallery.
Fingers, mouse and keys
| Phone / tablet | Computer | |
|---|---|---|
| Next / previous | Swipe — the picture follows your finger | ← →, the arrows, a thumbnail |
| Zoom | Pinch, or double-tap where you want to look | Mouse wheel, double-click, + − 0, the zoom button |
| Look around (zoomed) | Drag | Drag |
| Just the picture | One tap hides the buttons and caption (tap again to bring them back) | — |
| Close | Swipe down (or up), ✕ | Esc, ✕, a click beside the picture |
| First / last · full screen · slideshow | — | Home End · F · Space |
Not just pictures
The type comes from the address (.mp4 → video, .mp3 → audio, a YouTube or Vimeo link → that player, .pdf → a page) or from data-type. Videos and embeds load only when shown, and stop when you move on or close. YouTube uses its privacy-friendly no-cookie player.
<div data-moey-lightbox>
<a href="photo.jpg"><img src="photo-small.jpg" alt="…"></a>
<a href="clip.mp4" data-poster="clip-cover.jpg">Video</a> <!-- video -->
<a href="episode-12.mp3" data-poster="cover.jpg">Listen</a> <!-- audio, with cover art -->
<a href="https://www.youtube.com/watch?v=…">Watch on YouTube</a> <!-- youtube / vimeo -->
<a href="#size-guide">Size guide</a> <!-- HTML from a <template> or element -->
<a href="menu.pdf" data-type="iframe">Menu (PDF)</a> <!-- a page, a PDF, a map -->
</div>
Pick the parts
Everything is optional. On the gallery as data-moey-lightbox-*, or as options in code:
| Part | Default | What it is |
|---|---|---|
thumbs | auto | The strip of thumbnails — auto: on bigger screens only (phones show the count instead), always, never |
counter · caption | on | "3 / 10" · the caption |
zoom · loop | on | Pinch / wheel / double-tap zoom · from the last back to the first |
fullscreen | on | The full-screen button (where the browser has it) |
download · share | off | A download button · share (the phone's own share sheet; copies the link elsewhere) |
slideshow="4000" | off | A play button that moves on every 4 seconds |
Just the picture — no thumbnails, count or captions
<div data-moey-lightbox data-moey-lightbox-thumbs="never" data-moey-lightbox-counter="false"
data-moey-lightbox-caption="false" data-moey-lightbox-fullscreen="false">…</div>
Everything — thumbnails, download, share, slideshow, no looping
<div data-moey-lightbox data-moey-lightbox-thumbs="always" data-moey-lightbox-download
data-moey-lightbox-share data-moey-lightbox-slideshow="3000" data-moey-lightbox-loop="false">…</div>
One link, or a set across the page
<a class="btn" href="https://youtu.be/…" data-moey-lightbox>Watch the film</a>
<!-- links anywhere with the same name are one set -->
<a href="shop.jpg" data-moey-lightbox="team">Our shop</a> … <a href="stall.jpg" data-moey-lightbox="team">The stall</a>
From code
Moey.lightbox.open([
{ src: 'photo-1.jpg', caption: 'Front' },
{ src: 'photo-2.jpg', caption: 'Back' },
{ src: 'unboxing.mp4', poster: 'unboxing.jpg' },
], { index: 0, thumbs: 'always', download: true });
Moey.lightbox.next(); Moey.lightbox.go(2); Moey.lightbox.close();
gallery.addEventListener('moey:lightbox', (e) => {
if (e.detail.action === 'change') history.replaceState(null, '', '#photo-' + (e.detail.index + 1));
});
Accessible
- A labelled dialog: the focus moves into it, and back to the thumbnail you opened when it closes.
- "3 of 10" and the caption are announced as you move; every button has a name.
- Swipes have buttons and keys too — nothing needs a gesture.
- With reduced motion, nothing slides.
Reference
| Attribute / API | What it does |
|---|---|
data-moey-lightbox (on a gallery or a link) · ="name" | Links open in the viewer · links with the same name are one set |
href · data-moey-lightbox-src | What to show (the big version) |
data-type="image|video|audio|youtube|vimeo|iframe|inline" | The kind (else guessed from the address) |
data-caption · data-poster · data-thumb · data-srcset | Caption · video / audio cover · thumbnail · sharper sources for big screens |
data-moey-lightbox-thumbs|counter|caption|zoom|loop|fullscreen|download|share|slideshow | The parts (see above) |
Moey.lightbox | open(items | link, options) · close() · next() · prev() · go(i) · index |
Event moey:lightbox | detail: { action: 'open' | 'change' | 'close', index, item } |