[英]Animation complete not called JavaScript DOM
I am trying to animate an image with a completion function.我正在尝试使用完成功能为图像设置动画。
The animation works fine but the complete property is not called.动画工作正常,但没有调用 complete 属性。
I tried looking around and didn't find any result on the matter that says that what i'm doing is wrong.我试着环顾四周,但没有发现任何关于我所做的事情的结果是错误的。
I tried on Chrome
and Firefox
我在
Chrome
和Firefox
上试过
I am using the code below我正在使用下面的代码
document.getElementById("myImageId").animate([{
transform: 'translateY(0px)'
},
{
transform: 'translateY(-300px)'
}
], {
duration: 300,
complete: function () {
alert('end ani')
}
});
Element.animate()
returns a Animation
object, and you can attach an event handler for finish
to the object: Element.animate()
返回一个Animation
对象,您可以为该对象附加一个用于finish
的事件处理程序:
var animation = document.querySelector('#myImageId').animate([{ transform: 'translateY(0px)' }, { transform: 'translateY(-300px)' } ], { duration: 300, delay: 300, fill: 'forwards', }); animation.addEventListener('finish', () => alert('end ani'));
<div id="myImageId"><img src="https://picsum.photos/200"></div>
Another option, which is only supported by FireFox currently, is the Animation.finished
promise:另一个选项,目前仅由 FireFox 支持,是
Animation.finished
承诺:
var animation = document.querySelector('#myImageId').animate([{ transform: 'translateY(0px)' }, { transform: 'translateY(-300px)' } ], { duration: 300, delay: 300, fill: 'forwards', }); animation.finished.then(() => alert('end ani'));
<div id="myImageId"><img src="https://picsum.photos/200"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.