用例
我使用新的“ registerProperty ”函数将自定义属性“ --animation-duration ”定义为时间值:
CSS.registerProperty({
name: '--animation-duration',
syntax: '<time>',
inherits: false,
initialValue: '1s'
});
在我的 CSS 中,我现在可以将此属性添加到我的类中并调整它的值:
.my-el {
--animation-duration: 1.5s;
}
现在我想通过javascript 在我的元素上获取这个属性的值,总是在 ms 中。这可以通过以下代码行来实现:
const duration = CSSNumericValue.parse(getComputedStyle(el).getPropertyValue('--ripple-anim-duration')).to('ms').value; // 1500
问题
在我的通用 javascript 中获取此值是否有更短/更好的方法?
额外的
我知道你可以在worklet中缩短它(在paint-worklet 中测试):
const duration = properties.get('--ripple-anim-duration').to('ms').value; // 1500
下面的代码在我的通用 javascript 中不起作用:
const duration = el.attributeStyleMap.get('--ripple-anim-duration').to('ms').value; // ¯\_(ツ)_/¯