大网数据广告活动
Web 标准与前端开发

ViewTransition: types property

· 52CE 我爱测

The **`types`** read-only property of the ViewTransition interface is a ViewTransitionTypeSet that allows the types set on the view transition to be accessed and modified.

View Transition API

The types read-only property of the ViewTransition interface is a ViewTransitionTypeSet that allows the types set on the view transition to be accessed and modified.

Value

A ViewTransitionTypeSet. This is a Set-like object, which means you can modify the types applied to a view transition on-the-fly using methods available on it such as clear(), add(), and delete().

Examples

Basic usage

This example includes a basic document that transitions between two different pieces of content. We provide three buttons, each of which triggers the transition, but with a different type to apply a different kind of animation to the transition.

HTML

The markup includes a single p element to contain the content and three button elements to trigger the view transition.

<p>This is my first piece of content. I hope you like it!</p>
<div>
  <button id="default">Default</button>
  <button id="slide">Slide</button>
  <button id="flip">Flip</button>
</div>

CSS

In our styles, we start off by creating a set of nested rules using the :active-view-transition pseudo-class. These styles will be applied whenever a view transition is active, regardless of the their types. We apply a view-transition-name of none to the :root, as we don't want any elements captured and animated on transition except for the <p> element, which is given a view-transition-name of content.

html,
body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

div {
  display: flex;
  width: 60%;
  justify-content: space-between;
}

p {
  font-size: 1.7em;
  width: 60%;
  color: blue;
  background-color: white;
  margin-top: 0;
}
html:active-view-transition {
  :root {
    view-transition-name: none;
  }
  p {
    view-transition-name: content;
  }
}

Next, we use the :active-view-transition-type() pseudo-class to create two blocks of nested styles, the first of which is only applied when the active view transition has a type of slide, and the second of which is only applied when the active view transition has a type of flip. In each block, we use the ::view-transition-old() and ::view-transition-new() pseudo-elements to apply custom animation-name values to the slide capture group's outgoing and incoming views.

As a result:

  • When the transition type is slide, the old content view slides out to the left, and the new content view slides in from the right.
  • When the transition type is flip, the old content view rotates horizontally to 90 degrees so it is no longer visible, and the new content view rotates back in.
  • In any other case, the default cross-fade transition animations are used.
html:active-view-transition-type(slide) {
  &::view-transition-old(content) {
    animation-name: slide-out-to-left;
  }
  &::view-transition-new(content) {
    animation-name: slide-in-from-right;
  }
}

html:active-view-transition-type(flip) {
  &::view-transition-old(content) {
    animation-name: flip-out;
  }
  &::view-transition-new(content) {
    animation-name: flip-in;
    animation-delay: 0.6s;
  }
}

Finally, we use @keyframes animation blocks to define the animations referenced previously. We also set a custom animation-duration on all capture groups, to slow the transition animations down slightly.

@keyframes slide-out-to-left {
  to {
    translate: -100vw 0;
  }
}
@keyframes slide-in-from-right {
  from {
    translate: 100vw 0;
  }
}

@keyframes flip-out {
  to {
    rotate: x 90deg;
  }
}
@keyframes flip-in {
  from {
    rotate: x -90deg;
  }
}

::view-transition-group(*) {
  animation-duration: 0.6s;
}

JavaScript

In our script, we create references to the buttons and the content paragraph, and then store our two different pieces of content in two constants.

const defaultBtn = document.getElementById("default");
const slideBtn = document.getElementById("slide");
const flipBtn = document.getElementById("flip");
const content = document.querySelector("p");

const first = "This is my first piece of content. I hope you like it!";
const second =
  "This is my second piece of content. Is it better than the first?";

Next, we add click event listeners to the buttons; when they are clicked, the changeContent() function is run.

defaultBtn.addEventListener("click", changeContent);
slideBtn.addEventListener("click", changeContent);
flipBtn.addEventListener("click", changeContent);

Finally, we define the changeContent() function. We start by invoking the Document.startViewTransition method to update the content and start the transition, saving the returned ViewTransition object in the vt constant. Inside startViewTransition(), the update callback checks whether the paragraph textContent is equal to the first string. If so, we set it to the second string. If not, we set it to the first string.

In the second part of the changeContent() function, we check the value of the click event target:

  • If it is the "Slide" button, we add a slide type to the active view transition's types using vt.types.add("slide").
  • If it is the "Flip" button, we add a flip type to the active view transition's types using vt.types.add("flip").
  • We don't do anything if the "Default" button was pressed. In this case, we want to use the default view transition cross-fade animation.
function changeContent(e) {
  const vt = document.startViewTransition({
    update() {
      content.textContent === first
        ? (content.textContent = second)
        : (content.textContent = first);
    },
  });

  if (e.target === slideBtn) {
    vt.types.add("slide");
  } else if (e.target === flipBtn) {
    vt.types.add("flip");
  }
}

Result

The example is rendered like so:

basic_usage

Try clicking each button and note how the DOM changes are identical in each case, but the animation is different. This is because a different transition type is set depending on which button is pressed (or no transition type in the "Default" case).

Applied example

Check out our MPA multiple transition types example (source code), which demonstrates how to apply different animations to cross-document view transitions depending on the navigation type, represented by different transition types. The transition type is determined on-the-fly with JavaScript during the navigation.

Applying different cross-document types using pageswap and pagereveal events provides a walkthrough of this example.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。