Communitygithub.com

高性能网页动画

此技能用于解决用户提出的“修复卡顿的CSS动画”、“让动画达到60fps”、“消除布局抖动”、“优化 width/height/top/left 动画”、“将动画转换为 transform 实现”、“高性能实现 box-shadow 动画”、“高度 auto 的动画”、“FLIP 动画”或“滚动/悬停动画卡顿”等问题。

高性能网页动画 是什么?

该技能旨在消除网页动画卡顿的首要原因:动画属性强制浏览器在每一帧重新计算布局(回流)或重绘。浏览器渲染分为布局 → 绘制 → 合成三个阶段。动画化 `width`、`height`、`top`、`left`、`margin`、`padding`、`box-shadow` 或 `filter` 会在每一帧重新触发布局和/或绘制,阻塞主线程。而动画化 `transform` 和 `opacity` 则完全在合成器(通常由GPU处理)上运行,跳过布局和绘制,从而实现流畅的60/120fps动画。技能提供了核心规则“仅动画化 transform 和 opacity”,并针对尺寸与位置变化、box-shadow 动画、高度 auto 动画等常见场景给出了具体解决方案(如 FLIP 技术、伪元素 opacity 技巧、CSS grid 1fr→0fr 技巧)。此外,还涵盖了避免布局抖动(批量读取/写入)、谨慎使用 will-change、以及交付与验证流程(生成独立HTML并通过性能工具确认仅使用合成器)等关键实践。该技能适用于 Claude Code、Cursor、Codex 等 AI 编码代理,帮助开发者编写高性能、无卡顿的网页动画。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/nehakanjamala/personal_website/tree/HEAD/.agents/skills/60fps-animation

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

高性能网页动画 是做什么的?

消除网页动画卡顿的首要原因:动画化那些强制浏览器在每一帧重新计算布局(回流)或重绘的属性。浏览器按阶段渲染——布局 → 绘制 → 合成。动画化 width、height、top、left、margin、padding、box-shadow 或 filter 会在每一帧重新运行布局和/或绘制,阻塞主线程。动画化 transform 和 opacity 则完全在合成器(通常是 GPU)上运行,跳过布局和绘制,这就是动画能够在 60/120fps 下保持流畅的原因。

何时使用

当动画出现卡顿或掉帧时,当悬停/滚动效果感觉沉重时,当动画化尺寸/位置/阴影时,当布局更改需要平滑动画时(卡片重新排序、元素在容器间移动),当动画化 height: auto 时,或者在审查动画代码以优化性能时使用。

核心规则:仅动画化 transform 和 opacity

将每一个“昂贵的”动画映射为一个廉价的等效方案。

动画化(昂贵)触发替换为(廉价)
width / height布局 + 绘制transform: scaleX()/scaleY()(+ 用于真实尺寸的 FLIP)
top / left / margin布局 + 绘制transform: translate()
box-shadow绘制动画化持有阴影的伪元素的 opacity
filter: blur()绘制(沉重)通过 opacity 交叉淡入淡出两个图层,或谨慎接受
background-position绘制在子图层上使用 transform: translate()
color / background绘制通常可接受;或交叉淡入淡出图层

通过 transform 实现位置和尺寸

/* 不好:每一帧都动画化布局 */
.box { transition: left 300ms, width 300ms; left: 0; width: 100px; }
.box:hover { left: 200px; width: 200px; }

/* 好:仅由合成器处理 */
.box {
  transition: transform 300ms ease;
  transform: translateX(0) scaleX(1);
  transform-origin: left center;
}
.box:hover { transform: translateX(200px) scaleX(2); }

scaleX 会扭曲内部内容(文字拉伸)。如需无扭曲的真实调整大小,请使用 FLIP。

通过伪元素 opacity 实现廉价的 box-shadow

动画化 box-shadow 会在每一帧重绘一个大的模糊区域。相反,在 ::after 上一次性地绘制阴影,然后仅动画化其 opacity。

.card { position: relative; }
.card::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: 0 12px 28px rgba(0,0,0,0.35);
  opacity: 0;
  transition: opacity 300ms ease;
  pointer-events: none;
}
.card:hover::after { opacity: 1; }

模糊阴影被光栅化一次;悬停仅改变合成器的透明度——在任何帧率下都流畅。

FLIP:廉价地动画化布局更改

FLIP(First, Last, Invert, Play)仅使用 transform 来动画化 布局 更改(重新排序、调整大小、在容器间移动)。测量元素所在位置(First)和将到位置(Last),应用一个反向变换使其视觉上看起来未移动,然后将变换动画化回原始状态(Play)。DOM 最终处于其真实的最终布局;运动完全是合成器的工作。

function flip(el, mutate) {
  const first = el.getBoundingClientRect();   // First
  mutate();                                    // 更改 DOM/布局
  const last = el.getBoundingClientRect();     // Last

  const dx = first.left - last.left;
  const dy = first.top - last.top;
  const sx = first.width  / last.width;
  const sy = first.height / last.height;

  el.animate(
    [
      { transformOrigin: 'top left',
        transform: `translate(${dx}px, ${dy}px) scale(${sx}, ${sy})` }, // Invert
      { transformOrigin: 'top left', transform: 'none' },               // Play
    ],
    { duration: 300, easing: 'cubic-bezier(0.2, 0, 0, 1)' }
  );
}

// 使用:动画化一个元素移动到新的网格位置
flip(card, () => targetContainer.appendChild(card));

Web Animations API 的 animate() 在合成器上运行 transform。对于多个元素,在进行任何更改(参见布局抖动)之前,批量执行所有 getBoundingClientRect() 读取。

动画化 height: auto

历史上 height: auto 是不可插值的。现代和备用方法:

/* 现代(Chrome 129+/支持浏览器):将尺寸关键字纳入动画 */
.panel {
  interpolate-size: allow-keywords;   /* 允许动画化到/从 auto */
  height: 0;
  overflow: clip;
  transition: height 300ms ease;
}
.panel.open { height: auto; }
/* calc-size() 也适用:height: calc-size(auto, size); */
/* 当今健壮的备用方案:CSS grid 1fr -> 0fr */
.wrapper {
  display: grid;
  grid-template-rows: 0fr;            /* 折叠 */
  transition: grid-template-rows 300ms ease;
}
.wrapper.open { grid-template-rows: 1fr; }
.wrapper > .content { overflow: hidden; min-height: 0; }

Grid 技巧动画化轨道尺寸(对合成器友好且广泛支持),并且不需要 JS 测量高度。在目标浏览器支持的情况下使用 interpolate-size: allow-keywords / calc-size(auto, size);保留 grid 技术作为跨浏览器的默认方案。

避免布局抖动(先批量读取,再写入)

在写入后读取布局属性(offsetWidth、getBoundingClientRect、scrollTop、getComputedStyle)会强制进行 同步 回流。在循环中交错读写(“布局抖动”)可能在每一帧导致数十次强制回流。

// 不好:读取,写入,读取,写入……每次迭代强制回流
items.forEach((el) => {
  const w = el.offsetWidth;          // 读取(强制布局)
  el.style.width = w * 1.5 + 'px';   // 写入(使布局失效)
});

// 好:先批量读取所有,再批量写入所有
const widths = items.map((el) => el.offsetWidth);  // 所有读取
items.forEach((el, i) => {                          // 所有写入
  el.style.width = widths[i] * 1.5 + 'px';
});

对于帧同步的工作,在 requestAnimationFrame 回调中读取并应用写入;像 fastdom 这样的库形式化了这种读/写调度。

will-change:谨慎使用

will-change: transform 会提前将元素提升到其自己的合成器图层,避免动画开始时的卡顿。但每个提升的图层都会消耗 GPU 内存,过度使用会降低性能。

.menu { will-change: transform; }   /* 仅对即将动画化的元素使用 */

规则:仅在动画开始前应用(例如在悬停/父元素状态上),在空闲时移除它(will-change: auto),切勿对大量元素不加选择地应用,也切勿永久地保留在大型/众多节点上。单个 transform: translateZ(0) hack 执行相同的提升,但更难撤销——优先使用 will-change。

交付与验证(独立 HTML)

打包的帮助脚本(scripts/):scripts/seek-shot.sh anim.html 0 1.5 3 冻结 ?t=N 挂钩并在每个时刻截图;scripts/contact-sheet.sh sheet.png frame-*.png 将它们平铺以便一目了然地审查。参见 scripts/README.md。

交付物是一个自包含的 .html,可直接在浏览器中打开——标记、CSS/JS 动画以及一个冻结挂钩都在一个文件中。对于此技能,验证是双重的:帧必须看起来正确 并且 产生成本低廉(仅合成器)。

输出合约:

  • 一个 .html,如果有依赖项则通过 CDN 引入,动画由 CSS transitions/@keyframes 或 Web Animations API 驱动。
  • 一个冻结机制,使截图落在确定的帧上:
    • CSS @keyframes → ?t=N 设置 el.style.animationDelay = (-N)+'s'; el.style.animationPlayState = 'paused'。
    • WAAPI / JS → 保留动画对象并执行 anim.pause(); anim.currentTime = N*1000。

验证循环 — 冻结 → 截图,然后分析卡顿:

  1. 在开始/中间/结束(?t=0、中间、结束)处打开无头浏览器,分别截图;确认运动在视觉上是正确的(没有因 scaleX 导致的裁剪或拉伸文本,FLIP 落在真实布局上,阴影/高度过渡看起来正确)。
  2. 确认它是仅合成器——这是本技能的全部意义。要么:
    • DevTools → 性能:录制动画,检查每一帧没有紫色“布局”或绿色“绘制”条(只有“复合图层”)。
    • 或者无头追踪:npx playwright screenshot 用于视觉,加上 CDP/tracing 捕获,并断言在动画窗口期间没有触发 Layout/Paint 事件。
  3. 观察 FPS/“帧渲染统计”叠加层保持在 60——掉帧意味着昂贵的属性悄悄地溜了回来。
npx playwright screenshot --wait-for-timeout=500 "file://$PWD/anim.html?t=0.3" frame.png

在完成之前:

  1. 独立打开——没有控制台错误。
  2. 每帧只有 transform / opacity 动画;DevTools 显示没有逐帧布局/绘制。
  3. 在开始/中间/结束进行了截图——正确,没有 scaleX 文本失真,FLIP 真实落地。
  4. 保持60fps;will-change 即时应用并在空闲时移除(没有残留图层)。
  5. 遵守 prefers-reduced-motion。

快速参考

目标请这样做
移动元素transform: translate()
无失真调整大小使用 transform 的 FLIP
悬停时的阴影动画化阴影伪元素的 opacity
展开到内容高度grid 0fr→1fr,或 interpolate-size: allow-keywords
大量元素移动先批量 getBoundingClientRect 读取,再写入
平滑动画开始即时 will-change,空闲时移除
验证仅合成器DevTools 性能:每帧没有紫色“布局”/绿色“绘制”

注意事项

  • scaleX/scaleY 会拉伸文本和子元素;当内容必须保持清晰时使用 FLIP。
  • transform 的百分比相对于元素自身的盒子,而不是父元素——与 left: % 不同。
  • 过度使用 will-change 或 translateZ(0) 会创建过多图层并 损害 性能;只提升将要动画化的元素。
  • filter 和 backdrop-filter 与合成器相关但仍然昂贵;通过透明度交叉淡入淡出它们的出现,而不是动画化模糊半径。
  • grid 1fr→0fr 的内容必须有 overflow: hidden 和 min-height: 0,否则不会折叠。
  • 始终将非必要的运动置于 @media (prefers-reduced-motion: reduce) 之后。

参考文件

  • references/patterns-and-profiling.md — 完整可运行示例(手风琴、重新排序列表、视差效果),DevTools 分析演练以确认仅合成器帧,减少运动模式,以及属性成本速查表。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

nehakanjamala/personal_website

This skill should be used when the user asks to "respect prefers-reduced-motion", "honor reduced motion", "make my animations accessible", "fix vestibular / motion-sickness issues", "add a useReducedMotion hook", "gate GSAP / Framer Motion / Lenis for reduced motion", or "meet WCAG 2.3.3 / C39". Provides tiered (not all-or-nothing) reduced-motion patterns in CSS and JS.

nehakanjamala/personal_website

Create, modify, debug, and deploy Apify Actors, and write their input and output schemas. Use when building an Actor from scratch, changing or troubleshooting Actor code, generating or updating .actor schema files, or pushing an Actor to the Apify platform. To wrap an existing non-Actor project, use apify-actorization instead.

nehakanjamala/personal_website

Convert existing projects into Apify Actors - serverless cloud programs. Actorize JavaScript/TypeScript (SDK with Actor.init/exit), Python (async context manager), or any language (CLI wrapper). Use when migrating code to Apify, wrapping CLI tools as Actors, or adding Actor SDK to existing projects.

nehakanjamala/personal_website

Deprecated. Output schema generation moved to the apify-actor-development skill; prefer that skill whenever it is available.

nehakanjamala/personal_website

Design and build an official Apify integration for a company's product - workflow-automation apps (Zapier/n8n-style), AI agent plugins (coding-agent skills+MCP bundles or OpenClaw/Hermes-style harnesses), AI framework packages (LangChain/LlamaIndex-style), or direct application clients via apify-client. Use when planning, creating, or reviewing an integration that exposes Apify Actors, runs, datasets, or key-value stores inside another product.

nehakanjamala/personal_website

Universal AI-powered web scraper for any platform. Scrape data from Instagram, Facebook, TikTok, YouTube, LinkedIn, X/Twitter, Google Maps, Google Search, Google Trends, Reddit, Airbnb, Yelp, and 15+ more platforms. Use for lead generation, brand monitoring, competitor analysis, influencer discovery, trend research, content analytics, audience analysis, review analysis, SEO intelligence, recruitment, or any data extraction task.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a glassmorphism effect", "frosted glass UI", "Apple liquid glass style", "frosted blur card", "translucent glass panel animation", "make a frosted nav bar", "build a glass modal/dialog", "animate a glass card on hover", or "add a refracting liquid-glass hero". Covers frosted translucent panels with backdrop-filter blur, edge/specular highlights, SVG liquid-glass refraction, motion on hover/scroll/enter, and accessible reduced-transparency fallbacks.

nehakanjamala/personal_website

This skill should be used when the user asks to "build a scroll animation", "add ScrollTrigger", "pin a section while scrolling", "scrub an animation to scroll", "create a hero timeline", "do a horizontal scroll section", "split text and animate words", "animate a layout change with GSAP Flip", "sync ScrollTrigger with Lenis", "Lenis smooth scroll jittery", "ScrollTrigger markers drift", "ScrollTrigger pins break with Lenis/Locomotive", or "set up smooth scroll with GSAP pinning". Covers GSAP timelines, ScrollTrigger, SplitText, Flip, and Lenis/Locomotive smooth-scroll sync for code-driven web motion.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a Lottie animation", "play a .lottie or .json animation", "integrate a Bodymovin/After Effects animation on web or mobile", "control Lottie playback / segments", "make a scroll-driven Lottie", "recolor a Lottie at runtime", or "export an AE animation to Lottie". Covers dotLottie/lottie-web integration, playback control, interactivity, theming, and the AE export checklist.

nehakanjamala/personal_website

This skill should be used when the user asks to "add a hover/press effect", "animate a toggle or switch", "build an animated like button", "make a toast/snackbar slide in", "animate a drawer or modal", "animate list reordering or add/remove", "do a shared-element layout transition", or "polish UI feedback". Covers UI motion with Framer Motion (motion/react) and modern CSS.

nehakanjamala/personal_website

This skill should be used when the user asks to "animate page or route transitions", "add a page transition or crossfade between views", "animate route changes in Next.js App Router", "use the View Transitions API", "AnimatePresence exit doesn't fire on navigation", or "Framer Motion exit animation not working / exit animation before unmount in Next.js". Covers enter/exit page-transition animation with the View Transitions API and Framer Motion, for Next.js App Router and general SPA routing.

nehakanjamala/personal_website

Scrape web pages using Scrapling with anti-bot bypass (like Cloudflare Turnstile), stealth headless browsing, spiders framework, adaptive scraping, and JavaScript rendering. Use when asked to scrape, crawl, or extract data from websites; web_fetch fails; the site has anti-bot protections; write Python code to scrape/crawl; or write spiders.

nehakanjamala/personal_website

This skill should be used when the user asks to "animate an SVG", "make a line draw itself on", "do a stroke draw-on / signature animation", "morph one shape into another", "move an element along a path", "animate an icon/logo", or "animate an SVG gradient or filter". Covers stroke-dashoffset draw-on, path morphing, motion-along-path, and animated icons/gradients/filters via CSS, SMIL, and GSAP.

相关技能