ShadowRoot: getAnimations() 方法

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上使用。自 2020 年 9 月起,所有浏览器均已提供此功能。

getAnimations() 方法属于 ShadowRoot 接口,它返回一个数组,其中包含当前生效的所有 Animation 对象,这些对象的 target 元素是 shadow tree 的后代。这个数组包括 CSS AnimationsCSS TransitionsWeb Animations

语法

js
getAnimations()

参数

无。

返回值

一个 Array,其中包含 Animation 对象,每个对象代表当前与调用它的 ShadowRoot 的后代元素相关联的一个动画。

示例

以下代码片段将通过将其 Animation.playbackRate 减半来减慢 shadow tree 中所有动画的速度。

js
let customElem = document.querySelector("my-shadow-dom-element");
let shadow = customElem.shadowRoot;
shadow.getAnimations().forEach((animation) => {
  animation.playbackRate *= 0.5;
});

规范

规范
Web 动画
# dom-documentorshadowroot-getanimations

浏览器兼容性

另见