Home / Website Sections / Lightbox Gallery with Zoom

Lightbox Gallery with Zoom in JavaScript, Free with Live Demo

Free lightbox gallery in plain JavaScript. Click a photo to open a full screen viewer with zoom, pan, arrow keys, swipe, captions and a counter. Built on the native dialog element.

Open live demoDownload HTML fileView code on GitHub
Lightbox Gallery with Zoom JavaScript project: a photo grid that opens a full screen viewer with zoom and pan

Runs on: dialog element and pointer events. Works in all modern browsers. The dialog element is supported in Chrome, Edge, Firefox and Safari 15.4+.

What is the Lightbox Gallery with Zoom?

A photo grid where any image opens in a full screen viewer. You can zoom in up to five times, drag around the zoomed photo and move between photos.

It uses the built in dialog element, so keyboard focus, Escape to close and returning focus to the photo you clicked all work without extra code.

Good for

  • Photography and portfolio sites
  • Property and hotel galleries
  • Product photos in shops
  • Event and wedding albums

What this project does

How it works

  1. Use the native dialogshowModal() gives a real modal: focus is trapped, the page behind is inert and Escape closes it for free.
  2. Zoom with transformZoom and pan are one CSS transform, translate then scale, so it stays sharp and fast.
  3. Swipe when not zoomedWhen the photo is at 100 percent a horizontal drag changes photo. When zoomed, dragging pans instead.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

grid.onclick = (e) => {
  const btn = e.target.closest("button");
  show(+btn.dataset.i);
  dialog.showModal(); // focus trap + Escape for free
};
view.addEventListener("wheel", (e) => {
  e.preventDefault();
  zoom = Math.min(5, Math.max(1, zoom * (e.deltaY < 0 ? 1.15 : 0.87)));
  img.style.transform = `translate(${x}px,${y}px) scale(${zoom})`;
}, { passive: false });

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Why use the dialog element?

It handles the hard parts of a modal for you: focus trapping, Escape to close and making the page behind inert.

Does it load full size photos up front?

No. Thumbnails load in the grid, and the large version loads only when you open a photo. The next one is preloaded.

Can I pinch to zoom on phones?

This version uses buttons and double tap to zoom on phones. You can add pinch by tracking two pointers.

More Website Sections projects