如何提示开发者用 transform 代替 position 动画?
解读
国内一线团队面试时,这道题考察的是“性能规则落地”能力。
- 业务痛点:很多前端习惯用 left/top/margin 做位移,导致浏览器每帧都要走 Layout → Paint → Composite,移动端掉帧明显。
- 规则定位:Stylelint 的插件体系允许自定义“性能规则”,把“是否用 transform 做动画”变成可量化、可阻断、可修复的 lint 问题。
- 工程闭环:不仅要写出规则,还要让规则在 VS Code 保存时自动标红、在 GitHub Actions 阻断合并、在 Webpack 热更新阶段提示,真正让开发者“无痛”改掉旧习惯。
- 面试区分度:能把“规则实现 + 自动修复 + 性能数据 + 工程集成”串成故事,就能从“会用 Stylelint”进阶到“用 Stylelint 解决性能问题”。
知识点
- PostCSS AST 节点类型:Declaration(属性)、Rule(选择器)、AtRule(媒体查询等)。
- Stylelint 插件三件套:
- createPlugin(name, function) 注册插件;
- root.walkDecls() 遍历声明;
- utils.report() 上报错误。
- 自动修复:在插件返回的 fix 函数里,用 PostCSS 的 decl.cloneBefore / remove 改写 AST,Stylelint --fix 即可落地。
- 性能指标:
- 触发 Layout 的属性:left、top、width、height、margin、padding…
- 仅触发 Composite 的属性:transform、opacity。
- 国内常用工程链:
- VS Code 插件 stylelint-plus 保存时触发;
- GitHub Actions 用 reviewdog 把报告以 PR comment 形式回写;
- Webpack 用 stylelint-webpack-plugin 在 dev 阶段 overlay 提示。
- 规则命名惯例:公司私有规则以 @company/stylelint-plugin-performance 发布,规则名 performance/use-transform-for-animation。
答案
“我会分三步把这条提示做成开发者无法忽视的强约束。
第一步,写一条私有 Stylelint 规则。思路是:
- 遍历所有 Declaration,若属性在 ['left','top','right','bottom','margin-left','margin-top'] 且包含 transition / animation 或父节点带 @keyframes,就命中;
- 同时检测是否已同时写了 transform,若已写则降级为 warning,否则报 error。
核心代码(已在国内项目落地):
// rules/use-transform-for-animation.js
const { createPlugin, utils } = require('stylelint');
const ruleName = 'plugin/use-transform-for-animation';
const messages = utils.ruleMessages(ruleName, {
expected: (prop) => `用 transform 替代 ${prop} 做动画,避免触发 Layout,提升移动端帧率`,
});
const plugin = createPlugin(ruleName, (primary, _, context) => {
return (root, result) => {
const badProps = ['left','top','right','bottom','margin-left','margin-top','margin-right','margin-bottom'];
root.walkDecls(decl => {
if (!badProps.includes(decl.prop)) return;
const hasAnimation = decl.parent.some(child =>
child.type === 'decl' && /transition|animation/.test(child.prop)
);
const inKeyframes = decl.parent.type === 'rule' && decl.parent.selector.includes('@keyframes');
if (!hasAnimation && !inKeyframes) return;
const hasTransform = decl.parent.some(child =>
child.type === 'decl' && child.prop === 'transform'
);
if (hasTransform) return; // 已同时写 transform,只警告
utils.report({
message: messages.expected(decl.prop),
node: decl,
result,
ruleName,
line: decl.source.start.line,
fix: context.fix ? () => {
// 简单自动修复:把 left: 10px 改成 transform: translateX(10px)
const value = decl.value;
if (decl.prop === 'left') {
decl.cloneBefore({ prop: 'transform', value: `translateX(${value})` });
decl.remove();
}
// 其余属性同理
} : null,
});
});
};
});
module.exports = plugin;
第二步,把规则接入工程链。
- 在 .stylelintrc.js 里 extends: ['@company/stylelint-plugin-performance'];
- VS Code 保存触发:settings.json 里 "stylelint.validate": ["css","scss","less"];
- GitHub Actions 用 stylelint --formatter github-actions-reporter,error 级别直接阻断 MR;
- Webpack 开发阶段用 stylelint-webpack-plugin,overlay 展示,开发者几乎实时看到提示。
第三步,用数据说话。
- 发版前跑 Lighthouse,把‘Avoid non-compositing animations’作为 MR 模板必检项;
- 统计规则命中次数,每月前端例会公示,把‘从 left 改成 transform 带来的帧率提升’写成内部技术月报,形成正向循环。
这样,开发者一写 left: 10px 动画就会看到红线,--fix 一键替换成 transform,性能问题在编码阶段就被消灭。”
拓展思考
- 规则粒度:如果业务里需要动态计算像素(如 left: calc(50% - 20px)),自动修复会失效,可降级为只提示并给出文档链接,引导开发者手写 translate(calc(...))。
- 多端差异:小程序 WebView 对 will-change 支持度不一,规则可追加“若用 transform 动画,必须同时写 will-change: transform”子规则,进一步减少 composite layer 创建耗时。
- 设计协同:把规则文档直接嵌到 Figma 插件里,设计师标注“位移动画”时自动提示“请用 transform”,从源头减少错误实现。
- 性能预算:把“动画属性”纳入 Bundle Size 之外的“Runtime Budget”,用 Stylelint 结合 Puppeteer 做 MR 阶段的帧率回归,若低于 55 fps 直接打回,真正实现“性能门禁”。