View Transitions API example

View Transitions API example. document.startViewTransition() names an element and the browser animates the DOM update.

The problem

A DOM update is instant. The old nodes are gone. The new ones appear. The browser has noea that Fe in this cell is the same Fe in that cell.

The method

startViewTransition() snapshots the document, runs your update, then interpolates. Without names, that interpolation is a crossfade of the whole page. With view-transition-name, each named box can move.

The stage

    Nothing matches.

    The example

    • Iron
    • Gold
    • Copper
    #list {
      display: grid;
      gap: 0.5rem;
      margin: 1rem 0 0;
      padding: 0;
      list-style: none;
    }
    
    #list li {
      padding: 0.75rem 1rem;
      border: 1px solid #ddd;
      border-radius: 8px;
      background: #f6f6f6;
    }
    
    #reverse {
      font: inherit;
      padding: 0.45rem 0.8rem;
      border: 1px solid #ccc;
      border-radius: 8px;
      background: #fff;
      cursor: pointer;
    }
    
    ::view-transition-group(*) {
      animation-duration: 0.5s;
    }
    const list = document.querySelector("#list")
    
    list.querySelectorAll("li").forEach((item) => {
      item.style.viewTransitionName = item.dataset.name
    })
    
    document.querySelector("#reverse").onclick = () => {
      const update = () => {
        const items = [...list.children].reverse()
        list.append(...items)
      }
    
      if (!document.startViewTransition) {
        update()
        return
      }
    
      document.startViewTransition(update)
    }

    All Web API labs

    • CSS overflow carousel example
    • CSS Anchor Positioning example
    • Customizable select example