Clean Markup

Directional Page Motion with CSS View Transition Types

Directional Page Motion with CSS View Transition Types

When the View Transitions API first brought native snapshot-based page transitions to the web, animating between states in two different directions remained unexpectedly awkward. A drill-down list-to-detail navigation should slide incoming content in from the right, whereas pressing Back or moving to an earlier pagination page should glide the previous view back from the left. Traditionally, teams solved this by mutating temporary utility classes onto document.documentElement (such as .is-back-transition), guarding against overlapping rapid clicks, and remembering to remove those classes inside transition.finished promises so future unrelated transitions did not inherit stale keyframes.

CSS View Transitions Level 2 eliminates that fragile class-toggling bookkeeping with native transition types. Instead of polluting the DOM with state classes, JavaScript passes an explicit types array to document.startViewTransition({ update, types: ['forwards'] }) (or assigns types inside pageswap and pagereveal handlers for cross-document navigations). In stylesheets, the :active-view-transition-type() pseudo-class matches the root element strictly for the duration of a transition carrying that type token. Paired with compositor-only transform and opacity keyframes and @media (prefers-reduced-motion: reduce) guards, directional transitions stay declarative, race-free, and GPU-accelerated.

Example

/* Applied only when types includes "forwards" */
:active-view-transition-type(forwards)::view-transition-old(page-stage) {
  animation: 260ms cubic-bezier(0.22, 1, 0.36, 1) both slide-out-left;
}

:active-view-transition-type(forwards)::view-transition-new(page-stage) {
  animation: 260ms cubic-bezier(0.22, 1, 0.36, 1) both slide-in-right;
}

/* Applied only when types includes "backwards" */
:active-view-transition-type(backwards)::view-transition-old(page-stage) {
  animation: 260ms cubic-bezier(0.22, 1, 0.36, 1) both slide-out-right;
}

:active-view-transition-type(backwards)::view-transition-new(page-stage) {
  animation: 260ms cubic-bezier(0.22, 1, 0.36, 1) both slide-in-left;
}

Implementation

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Directional CSS View Transition Types</title>
  <style>
    .pager {
      max-width: 36rem;
      margin: 2rem auto;
      font-family: system-ui, sans-serif;
    }

    .stage {
      view-transition-name: page-stage;
      padding: 1.5rem;
      border: 1px solid #d4d4d8;
      border-radius: 0.75rem;
      background: #ffffff;
      overflow: hidden;
    }

    .controls {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 1rem;
    }

    @keyframes slide-out-left {
      to {
        opacity: 0;
        transform: translateX(-28px);
      }
    }

    @keyframes slide-in-right {
      from {
        opacity: 0;
        transform: translateX(28px);
      }
    }

    @keyframes slide-out-right {
      to {
        opacity: 0;
        transform: translateX(28px);
      }
    }

    @keyframes slide-in-left {
      from {
        opacity: 0;
        transform: translateX(-28px);
      }
    }

    :active-view-transition-type(forwards)::view-transition-old(page-stage) {
      animation: 240ms cubic-bezier(0.22, 1, 0.36, 1) both slide-out-left;
    }

    :active-view-transition-type(forwards)::view-transition-new(page-stage) {
      animation: 240ms cubic-bezier(0.22, 1, 0.36, 1) both slide-in-right;
    }

    :active-view-transition-type(backwards)::view-transition-old(page-stage) {
      animation: 240ms cubic-bezier(0.22, 1, 0.36, 1) both slide-out-right;
    }

    :active-view-transition-type(backwards)::view-transition-new(page-stage) {
      animation: 240ms cubic-bezier(0.22, 1, 0.36, 1) both slide-in-left;
    }

    @media (prefers-reduced-motion: reduce) {
      ::view-transition-group(*),
      ::view-transition-old(*),
      ::view-transition-new(*) {
        animation: none !important;
      }
    }
  </style>
</head>
<body>
  <section class="pager" aria-label="Step walkthrough">
    <article id="stage" class="stage" tabindex="-1">
      <h1 id="step-title">Step 1 of 3</h1>
      <p id="step-body">Inspect the baseline HTML structure before enhancing motion.</p>
    </article>

    <div class="controls">
      <button id="prev-btn" type="button" disabled>Previous</button>
      <span id="step-indicator" aria-live="polite">1 / 3</span>
      <button id="next-btn" type="button">Next</button>
    </div>
  </section>

  <script>
    const steps = [
      {
        title: 'Step 1 of 3',
        body: 'Inspect the baseline HTML structure before enhancing motion.'
      },
      {
        title: 'Step 2 of 3',
        body: 'Assign directional types when advancing or stepping backward.'
      },
      {
        title: 'Step 3 of 3',
        body: 'Respect prefers-reduced-motion so transitions degrade gracefully.'
      }
    ];

    let activeIndex = 0;

    const stage = document.getElementById('stage');
    const titleEl = document.getElementById('step-title');
    const bodyEl = document.getElementById('step-body');
    const indicatorEl = document.getElementById('step-indicator');
    const prevBtn = document.getElementById('prev-btn');
    const nextBtn = document.getElementById('next-btn');

    function renderStep(index) {
      const step = steps[index];
      titleEl.textContent = step.title;
      bodyEl.textContent = step.body;
      indicatorEl.textContent = `${index + 1} / ${steps.length}`;
      prevBtn.disabled = index === 0;
      nextBtn.disabled = index === steps.length - 1;
    }

    function navigateStep(nextIndex) {
      if (nextIndex < 0 || nextIndex >= steps.length || nextIndex === activeIndex) {
        return;
      }

      const direction = nextIndex > activeIndex ? 'forwards' : 'backwards';
      const applyUpdate = () => {
        activeIndex = nextIndex;
        renderStep(activeIndex);
      };

      if (!document.startViewTransition) {
        applyUpdate();
        return;
      }

      // Feature-detect View Transitions Level 2 options object ({ update, types })
      try {
        document.startViewTransition({
          update: applyUpdate,
          types: [direction]
        });
      } catch {
        document.startViewTransition(applyUpdate);
      }
    }

    prevBtn.addEventListener('click', () => navigateStep(activeIndex - 1));
    nextBtn.addEventListener('click', () => navigateStep(activeIndex + 1));
  </script>
</body>
</html>

Main Point: CSS :active-view-transition-type() selectors scope forward and backward snapshot animations cleanly to each transition instance without mutating DOM state classes or risking cleanup race conditions.

Credits

Photo by Jen Theodore on Unsplash

← Home