可以使用addEventListener()方法替代onanimationend属性来添加事件监听器,并结合removeEventListener()方法在动画结束后立即将事件侦听器从元素中移除。下面是一个示例代码:
const element = document.querySelector('#animatedElement');
// 添加动画结束事件监听器
element.addEventListener('animationend', handleAnimationEnd);
function handleAnimationEnd() {
// 处理动画结束事件
console.log('动画结束');
// 从元素中移除动画结束事件监听器
element.removeEventListener('animationend', handleAnimationEnd);
}