Home / Modern UI / View Transitions Gallery

View Transitions Gallery in JavaScript, Free with Live Demo

Free View Transitions API gallery in plain JavaScript. Click a photo and it morphs into the detail view, with filters, back button support and a fallback.

Open live demoDownload HTML fileView code on GitHub
View Transitions Gallery JavaScript project: a photo gallery where images grow smoothly into the detail page

Runs on: View Transitions. Chrome and Edge 111+, Safari 18+ and recent Firefox. Older browsers switch views instantly.

What is the View Transitions Gallery?

Click a photo in the grid and it grows into the big detail view instead of just popping in. Press back and it shrinks home again. This kind of animation used to need a heavy library, and now the browser does it with one function call.

The View Transitions API takes a picture of the page before and after a change, then animates between them. Give the small photo and the big photo the same view-transition-name and the browser moves one into the other.

Good for

  • Photo galleries and portfolios
  • Online shops, from product list to product page
  • Single page apps that feel smoother
  • Filters that rearrange a grid

What this project does

How it works

  1. Name the elementThe clicked photo gets view-transition-name: hero, and so does the big photo on the detail view.
  2. Swap the DOMThe page calls startViewTransition and changes the DOM inside the callback. The browser snapshots before and after.
  3. Let CSS animateThe browser morphs the named element between its two positions. Everything else cross-fades.

The key JavaScript

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

// Swap the view inside a transition
function open(photo) {
  const img = document.querySelector(`[data-id="${photo.id}"] img`);
  img.style.viewTransitionName = "hero"; // name it before

  document.startViewTransition(() => {
    img.style.viewTransitionName = "";
    renderDetail(photo); // big <img> also has view-transition-name: hero
  });
}
// CSS: ::view-transition-group(hero) { animation-duration: .45s; }

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

What is the View Transitions API?

It is a browser feature that animates between two states of a page. You call document.startViewTransition() and change the page inside it, and the browser handles the animation.

Does it work in Firefox and Safari?

Safari 18 and newer support it, and recent Firefox versions do too. In older browsers the page changes instantly, which is a safe fallback.

Can I control the animation?

Yes. Use the ::view-transition pseudo elements in CSS to change the timing, easing or effect, just like normal CSS animations.

More Modern UI projects