1. Interactivity
  2. will-change

Interactivity

will-change

用于优化预期将要发生变化的元素的动画的工具类。

ClassStyles
will-change-auto
will-change: auto;
will-change-scroll
will-change: scroll-position;
will-change-contents
will-change: contents;
will-change-transform
will-change: transform;
will-change-<custom-property>
will-change: var(<custom-property>);
will-change-[<value>]
will-change: <value>;

示例

使用 will change 进行优化

使用 will-change-scrollwill-change-contentswill-change-transform 工具类来优化预期在不久的将来会发生变化的元素。这些工具类通过指示浏览器在动画实际开始之前准备必要的动画资源来实现优化:

<div class="overflow-auto will-change-scroll">  <!-- ... --></div>

建议您在元素即将发生变化之前应用这些工具类,并在变化完成后不久使用 will-change-auto 将其移除。

will-change 属性旨在作为处理已知性能问题的最后手段。避免过度使用这些工具类,或仅仅是为了预防性能问题而使用它们,因为这实际上可能会导致页面性能下降。

使用自定义值

Use the will-change-[<value>] syntax to set the will-change 属性 based on a completely custom value:

<div class="will-change-[top,left] ...">  <!-- ... --></div>

For CSS variables, you can also use the will-change-(<custom-property>) syntax:

<div class="will-change-(--my-properties) ...">  <!-- ... --></div>

This is just a shorthand for will-change-[var(<custom-property>)] that adds the var() function for you automatically.

版权所有 © 2025 Tailwind Labs Inc.·商标政策