我已经尝试了transitionend
and在动画结束animationend
后更改我的 CSS transition
。keyframes
我用两个变体做了一个例子,它们按预期工作:当 a或动画结束时,我可以toggle
a 。悬停时,我开始转换/动画,并在转换/动画更改后切换一个类,该类会更改 。class
transition
JavaScript
background-color
唯一的区别是,当我执行 mouseout 并且 div 回到原始状态时,使用transition
和 transitionend,类将被删除并且原始background-color
是可见的。对于keyframes
动画和animationend
,类和background-color
停留,当我做鼠标悬停时也是如此。我怎样才能得到同样的animationend
行为transition
?
var boxTransition = document.getElementById("transition");
var boxAnimation = document.getElementById("animation");
/* add class after transition ends */
boxTransition.addEventListener("transitionend", changeBackground);
/* add class after keyframes animation ends */
boxAnimation.addEventListener("animationend", changeBackground);
function changeBackground() {
this.classList.toggle("box--end");
}
.box {
height: 100px;
margin-bottom: 30px;
width: 100px;
}
.box--transition {
background-color: lightcoral;
transition: width 0.5s ease-in-out;
}
.box--transition:hover {
width: 300px;
}
.box--animation {
background-color: lightblue;
}
.box--animation:hover {
animation: animateWidth 0.5s ease-in-out;
animation-fill-mode: forwards;
}
.box--end {
background-color: gray;
}
@keyframes animateWidth {
from {
width: 100px;
}
to {
width: 300px;
}
}
<div id="transition" class="box box--transition"></div>
<div id="animation" class="box box--animation"></div>