返回提示词目录第 1423 页

排查并修复网页动画滚动卡顿

结合性能记录与代码定位重排、重绘及事件处理瓶颈,给出最小修复改动和前后验证结果。

完整提示词

<context>
Every frame goes through style, layout, paint and composite, and must finish in about 16.7 ms at 60 Hz (8.3 ms at 120 Hz). Animating `transform` and `opacity` can skip layout and paint and run on the compositor, so it stays smooth even when the main thread is busy. Animating `top`, `left`, `width`, `height`, `margin` or `box-shadow` re-runs layout or paint every frame. The other classic cause is forced synchronous layout ("layout thrashing"): JavaScript writes a style, then reads a geometry property such as `offsetHeight` or `getBoundingClientRect()`, forcing the browser to lay out immediately, often inside a loop or a scroll handler. Scroll handlers that do work on every event, non-passive touch and wheel listeners, and large or frequent repaints (big blurred shadows, `filter`, fixed backgrounds) are next. `will-change` helps only for elements that really animate; putting it everywhere wastes GPU memory. Modern options remove JavaScript entirely: CSS transitions, the Web Animations API, `IntersectionObserver` instead of scroll polling, and CSS scroll-driven animations where supported.
</context>

<task>
Fix the jank in: {{animation}}

1. Read the animation code, its CSS and any scroll, resize or pointer handlers involved.
2. Reproduce and record evidence: describe a Performance panel recording with 4x CPU throttling, looking for long frames, purple layout blocks marked "Forced reflow", large green paint blocks and long tasks during the animation. If you cannot record, list the code-level signs you found and what to record.
3. Identify the cause: animated properties that trigger layout or paint; read-after-write geometry in loops or handlers; per-event work in scroll handlers; non-passive listeners; expensive paints; too many or too large layers; framework re-renders during the animation.
4. Fix with the smallest change: animate `transform` and `opacity` instead; batch reads before writes and do visual updates in `requestAnimationFrame`; make listeners passive; replace scroll polling with `IntersectionObserver` or a scroll-driven animation; reduce paint area or cost; add `will-change` only to the animating element, only while it animates.
5. Respect `prefers-reduced-motion` for any motion you touch.
6. Verify in a new recording or by reasoning from the code if you cannot run a browser, and run the existing tests.
</task>

<constraints>
- Keep the visual result the same unless the user agrees to a change; say where a compositor-only version looks different.
- Do not add `will-change` or `translateZ(0)` broadly as a blanket fix.
- Say which browsers a suggested API supports when support is not universal, and keep a fallback.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Evidence
What the recording or code shows, frame times if measured.
## Cause
The specific mechanism, with file:line.
## Changes
One line per file: `path`, what changed, with the key snippet.
## Results
Frame times or long-frame count before and after, or what to check if not measured.
</output_format>

可替换内容

  • {{animation}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、前端性能、动画卡顿、滚动优化、浏览器性能分析

前往 AI 图片创作