我有一个使用 JS 声明和播放的动画,但是当动画播放时,我的事件监听器不会触发。
这是 HTML 文件:
let anim = document.getElementById("anim");
const animkeyFrames = new KeyframeEffect(
anim,
[{
transform: `translate3d(0px, 0px, 0px)`
}, // keyframe
{
transform: `translate3d(0px, ${200}px, 0px)`
},
], {
// keyframe options
duration: 1000,
iterations: "1",
},
);
let animation = new Animation(animkeyFrames, document.timeline);
anim.addEventListener('animationstart', () => {
console.log("STARTED");
})
animation.play();
#anim {
top: 10px;
left: 10px;
width: 100px;
height: 100px;
background-color: black;
position: absolute;
}
<div id="anim"></div>
我希望动画播放时记录“STARTED”,但什么也没发生。
沒有 的
animationstart
活動Animation
。你可以做这样的事情
并在动画期间使用循环或异步循环或其他方法来获取
Animation
实例的各种属性https://drafts.csswg.org/web-animations/#the-animation-interface。