概述
CSS Typed OM 将样式值类型化(长度、角度、颜色等),避免操作字符串并提升性能与可读性。通过 `element.attributeStyleMap.set/get()` 与 `CSSUnitValue` 等类型进行读写与数学计算。
示例
```js
const el = document.querySelector('.box')
el.attributeStyleMap.set('width', new CSSUnitValue(120, 'px'))
el.attributeStyleMap.set('transform', new CSSTransformValue([new CSSRotate(45, 'deg')]))
const w = el.computedStyleMap().get('width') // CSSUnitValue
console.log(w.value, w.unit)
```
工程建议
- 类型与单位:选择合适单位(`px/%/deg` 等);使用 `CSSMathValue` 组合计算。
- 性能:减少字符串拼接与解析;批量更新样式以避免重排抖动。
- 兼容:不支持时回退到 `style` 字符串接口;保持功能一致。
参考与验证
- MDN CSS Typed OM 文档:https://developer.mozilla.org/docs/Web/API/CSS_Typed_OM
- web.dev Typed OM 指南:https://web.dev/articles/css-typed-om
- Chrome 平台文档:https://developer.chrome.com/docs/web-platform/css-typed-om/

发表评论 取消回复