我有一个带有动画的div。
我已经将一个animationend事件监听器附加到了这个div上。
这个div还有一个具有动画效果的子元素。
由于某种原因,当子元素的动画结束时,animationend事件也会被触发。
为什么会这样?有没有办法解决?我想让animationend事件只在我附加事件监听器的元素完成时运行。
我已经将一个animationend事件监听器附加到了这个div上。
这个div还有一个具有动画效果的子元素。
由于某种原因,当子元素的动画结束时,animationend事件也会被触发。
为什么会这样?有没有办法解决?我想让animationend事件只在我附加事件监听器的元素完成时运行。
document.querySelector('.outer').addEventListener('animationend',function () {
console.log('done')
})
body {
height:100vh;
display:grid;
place-items:center;
}
.outer {
display:grid;
place-items:center;
height:200px;
width:200px;
background:black;
animation:spin 2s
}
.inner {
height:50px;
width:50px;
background:red;
animation:spin 2s 2s
}
@keyframes spin {
from {
transform:rotate(0)
}
to {
transform:rotate(360deg)
}
}
<div class="outer">
<div class="inner">
</div>
</div>
尝试使用ID而不是其他方式,但仍然没有成功
document.querySelector('#outer').addEventListener('animationend',function () {
console.log('done')
})
body {
height:100vh;
display:grid;
place-items:center;
}
#outer {
display:grid;
place-items:center;
height:200px;
width:200px;
background:black;
animation:spin 2s
}
.inner {
height:50px;
width:50px;
background:red;
animation:spin 2s 2s
}
@keyframes spin {
from {
transform:rotate(0)
}
to {
transform:rotate(360deg)
}
}
<div id="outer">
<div class="inner">
</div>
</div>