要让CSS动画完成后反之亦然,可以通过以下几种方式实现:
示例代码:
.animation {
animation-name: myAnimation;
animation-duration: 2s;
animation-direction: alternate;
}
@keyframes myAnimation {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
示例代码:
.transition {
width: 100px;
height: 100px;
background-color: red;
transition-property: width;
transition-duration: 2s;
transition-direction: alternate;
}
.transition:hover {
width: 200px;
}
示例代码:
<div id="myElement"></div>
<script>
var element = document.getElementById("myElement");
element.addEventListener("animationend", function() {
element.classList.toggle("reverse");
});
</script>
<style>
@keyframes myAnimation {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
#myElement {
width: 100px;
height: 100px;
background-color: red;
animation-name: myAnimation;
animation-duration: 2s;
}
#myElement.reverse {
animation-direction: reverse;
}
</style>
以上是三种常见的实现方式,具体选择哪种方式取决于具体的需求和场景。
领取专属 10元无门槛券
手把手带您无忧上云