view-transitions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseView Transitions
View Transitions
Part of Agent Skills™ by googleadsagent.ai™
属于 Agent Skills™ 的一部分,由 googleadsagent.ai™ 提供
Description
描述
View Transitions implements the React View Transition API for seamless animated transitions between UI states and routes. This skill covers the component, for classifying transitions, CSS pseudo-elements for animation control, shared element transitions across routes, and integration with Next.js App Router.
<ViewTransition>addTransitionTypeView transitions eliminate the jarring jump between pages that makes web applications feel inferior to native apps. By capturing a snapshot of the outgoing state and cross-fading it with the incoming state, the browser creates a smooth visual continuity that preserves user context during navigation. React's implementation hooks into the concurrent rendering model, enabling transitions to be interruptible and prioritized.
Shared element transitions take this further: a thumbnail on a list page morphs into a full-size hero image on the detail page, maintaining visual identity across route changes. The CSS property establishes the relationship between elements, and the browser handles the interpolation automatically.
view-transition-nameView Transitions 实现了 React View Transition API,用于在 UI 状态和路由之间提供无缝的动画过渡。该 skill 涵盖 组件、用于对过渡进行分类的 、用于控制动画的 CSS 伪元素、跨路由的共享元素过渡,以及与 Next.js App Router 的集成。
<ViewTransition>addTransitionTypeView Transition 可以消除页面切换时令人不适的跳跃感,让 Web 应用不再显得逊于原生应用。浏览器会捕获离开状态的快照,并将其与进入状态交叉淡入淡出,从而形成平滑的视觉连续性,在导航过程中保留用户上下文。React 的实现接入了并发渲染模型,使过渡可以被中断并获得优先级。
共享元素过渡更进一步:列表页面中的缩略图会变形为详情页面中的全尺寸主视觉图,在路由变化过程中保持视觉标识的一致。CSS 的 属性用于建立元素之间的关联,浏览器会自动处理插值。
view-transition-nameUse When
使用时机
- Navigating between routes in a React or Next.js application
- Animating list-to-detail transitions with shared elements
- Adding entrance/exit animations to page content
- Implementing back/forward navigation with directional animations
- The user requests smooth page transitions or "native-like" feel
- Replacing manual FLIP animation implementations
- 在 React 或 Next.js 应用中进行路由导航
- 使用共享元素实现列表到详情的过渡动画
- 为页面内容添加进入/退出动画
- 为前进/后退导航添加方向动画
- 用户需要平滑的页面过渡或“原生应用般”的体验
- 替换手动 FLIP 动画实现
How It Works
工作原理
mermaid
sequenceDiagram
participant User
participant React
participant Browser
participant CSS
User->>React: Navigate to new route
React->>Browser: startViewTransition()
Browser->>Browser: Capture old state snapshot
React->>React: Update DOM (concurrent)
Browser->>Browser: Capture new state snapshot
Browser->>CSS: Apply ::view-transition pseudo-elements
CSS->>Browser: Animate old → new
Browser->>User: Smooth transition renderedThe browser orchestrates the transition lifecycle: snapshot capture, DOM update, and cross-fade animation. React's component manages the timing and classification, while CSS pseudo-elements control the visual animation.
<ViewTransition>mermaid
sequenceDiagram
participant User
participant React
participant Browser
participant CSS
User->>React: Navigate to new route
React->>Browser: startViewTransition()
Browser->>Browser: Capture old state snapshot
React->>React: Update DOM (concurrent)
Browser->>Browser: Capture new state snapshot
Browser->>CSS: Apply ::view-transition pseudo-elements
CSS->>Browser: Animate old → new
Browser->>User: Smooth transition rendered浏览器负责编排整个过渡生命周期,包括快照捕获、DOM 更新和交叉淡入淡出动画。React 的 组件负责管理时机和分类,而 CSS 伪元素用于控制视觉动画。
<ViewTransition>Implementation
实现
Basic Route Transition
基础路由过渡
tsx
import { ViewTransition } from "react";
import { useRouter } from "next/navigation";
function PageLayout({ children }: { children: React.ReactNode }) {
return (
<ViewTransition>
{children}
</ViewTransition>
);
}tsx
import { ViewTransition } from "react";
import { useRouter } from "next/navigation";
function PageLayout({ children }: { children: React.ReactNode }) {
return (
<ViewTransition>
{children}
</ViewTransition>
);
}Shared Element Transition
共享元素过渡
tsx
function ProductCard({ product }: { product: Product }) {
return (
<Link href={`/products/${product.id}`}>
<ViewTransition name={`product-${product.id}`}>
<img src={product.image} alt={product.name} />
</ViewTransition>
<h3>{product.name}</h3>
</Link>
);
}
function ProductDetail({ product }: { product: Product }) {
return (
<article>
<ViewTransition name={`product-${product.id}`}>
<img src={product.image} alt={product.name} />
</ViewTransition>
<h1>{product.name}</h1>
<p>{product.description}</p>
</article>
);
}tsx
function ProductCard({ product }: { product: Product }) {
return (
<Link href={`/products/${product.id}`}>
<ViewTransition name={`product-${product.id}`}>
<img src={product.image} alt={product.name} />
</ViewTransition>
<h3>{product.name}</h3>
</Link>
);
}
function ProductDetail({ product }: { product: Product }) {
return (
<article>
<ViewTransition name={`product-${product.id}`}>
<img src={product.image} alt={product.name} />
</ViewTransition>
<h1>{product.name}</h1>
<p>{product.description}</p>
</article>
);
}Directional Navigation Animation
方向导航动画
tsx
import { addTransitionType } from "react";
function handleNavigation(direction: "forward" | "back") {
addTransitionType(direction);
router.push(targetUrl);
}css
::view-transition-old(root) {
animation: slide-out 200ms ease-in;
}
::view-transition-new(root) {
animation: slide-in 200ms ease-out;
}
@keyframes slide-out { to { transform: translateX(-100%); opacity: 0; } }
@keyframes slide-in { from { transform: translateX(100%); opacity: 0; } }
/* Reverse for back navigation */
[data-transition-type="back"]::view-transition-old(root) {
animation: slide-in 200ms ease-in reverse;
}
[data-transition-type="back"]::view-transition-new(root) {
animation: slide-out 200ms ease-out reverse;
}tsx
import { addTransitionType } from "react";
function handleNavigation(direction: "forward" | "back") {
addTransitionType(direction);
router.push(targetUrl);
}css
::view-transition-old(root) {
animation: slide-out 200ms ease-in;
}
::view-transition-new(root) {
animation: slide-in 200ms ease-out;
}
@keyframes slide-out { to { transform: translateX(-100%); opacity: 0; } }
@keyframes slide-in { from { transform: translateX(100%); opacity: 0; } }
/* Reverse for back navigation */
[data-transition-type="back"]::view-transition-old(root) {
animation: slide-in 200ms ease-in reverse;
}
[data-transition-type="back"]::view-transition-new(root) {
animation: slide-out 200ms ease-out reverse;
}Best Practices
最佳实践
- Assign unique values—duplicates cause silent failures
view-transition-name - Keep transition durations under 300ms to feel responsive, not sluggish
- Respect by disabling or shortening animations
prefers-reduced-motion - Use to differentiate forward, back, and lateral navigation
addTransitionType - Test transitions with slow network and CPU throttling enabled
- Provide fallback behavior for browsers that do not support the View Transition API
- 为 分配唯一值——重复值会导致静默失败
view-transition-name - 将过渡时长控制在 300ms 以下,以保持响应迅速,避免感觉迟缓
- 遵循 ,禁用或缩短动画
prefers-reduced-motion - 使用 区分前进、后退和横向导航
addTransitionType - 在启用慢速网络和 CPU 节流的情况下测试过渡
- 为不支持 View Transition API 的浏览器提供回退行为
Platform Compatibility
平台兼容性
| Platform | Support | Notes |
|---|---|---|
| Cursor | Full | React + Next.js aware |
| VS Code | Full | CSS pseudo-element support |
| Windsurf | Full | Frontend framework support |
| Claude Code | Full | Code generation |
| Cline | Full | React component generation |
| aider | Partial | Limited CSS context |
| 平台 | 支持情况 | 备注 |
|---|---|---|
| Cursor | 完全支持 | 支持 React + Next.js |
| VS Code | 完全支持 | 支持 CSS 伪元素 |
| Windsurf | 完全支持 | 支持前端框架 |
| Claude Code | 完全支持 | 代码生成 |
| Cline | 完全支持 | React 组件生成 |
| aider | 部分支持 | CSS 上下文有限 |
Related Skills
相关 Skills
- Composition Patterns
- React Best Practices
- Web Design Guidelines
- Edge Rendering
- Composition Patterns
- React Best Practices
- Web Design Guidelines
- Edge Rendering
Keywords
关键词
view-transitionsreactshared-elementspage-transitionsanimationnext-jscss-pseudo-elementscross-faderoute-animation© 2026 googleadsagent.ai™ | Agent Skills™ | MIT License
view-transitionsreactshared-elementspage-transitionsanimationnext-jscss-pseudo-elementscross-faderoute-animation© 2026 googleadsagent.ai™ | Agent Skills™ | MIT License