我遇到了这个问题,不理解这种行为.
这里我们有一个简单的矩形,它应该在按下切换按钮时进行动画输入和输出.
当按钮被点击时,矩形类名称将在"animation-in"和"animation-out"之间切换.
"Scale-easeInOutBoss"和"Scale-easeInOutBoss-Out"的KeyFrame对象是相同的,只是名称引用不同.
一切都很完美.
当在css规则‘.Animation-Out’内的属性‘Animation-name:’从‘Scale-easeInOutBlip-Out’更改为‘Scale-easeInOutBounch’时,动画中断.
这里的问题是,为什么会发生这种情况?代码中是否存在逻辑错误,或者动画的实现是否不正确?
会很乐意理解这种行为.
这一行为在Windows 11上使用Chrome进行了复制
<!DOCTYPE html>
<html>
<head>
<title>Rectangle Animation</title>
<style>
#myRectangle {
width: 100px;
height: 150px;
background-color: green;
margin: 20px;
transform: scale(0);
}
.animation-out {
animation-duration: 4s;
animation-timing-function: ease; /*linear*/
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: forwards;
animation-play-state: running;
animation-name: scale-easeInOutBounce;
animation-timeline: auto;
animation-range-start: normal;
animation-range-end: normal;
}
.animation-in {
animation-duration: 4s;
animation-timing-function: ease;
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: reverse;
animation-fill-mode: forwards;
animation-play-state: running;
animation-name: scale-easeInOutBounce;
animation-timeline: auto;
animation-range-start: normal;
animation-range-end: normal;
}
@keyframes scale-easeInOutBounce {
0% { transform: scale(1); }
2% { transform: scale(0.99); }
4% { transform: scale(1); }
10% { transform: scale(0.97); }
14% { transform: scale(0.99); }
22% { transform: scale(0.88); }
32% { transform: scale(0.99); }
42% { transform: scale(0.6); }
50% { transform: scale(0.5); }
58% { transform: scale(0.4); }
68% { transform: scale(0.01); }
78% { transform: scale(0.12); }
86% { transform: scale(0.01); }
90% { transform: scale(0.03); }
96% { transform: scale(0); }
98% { transform: scale(0.01); }
100% { transform: scale(0); }
}
@keyframes scale-easeInOutBounce-out {
0% { transform: scale(1); }
2% { transform: scale(0.99); }
4% { transform: scale(1); }
10% { transform: scale(0.97); }
14% { transform: scale(0.99); }
22% { transform: scale(0.88); }
32% { transform: scale(0.99); }
42% { transform: scale(0.6); }
50% { transform: scale(0.5); }
58% { transform: scale(0.4); }
68% { transform: scale(0.01); }
78% { transform: scale(0.12); }
86% { transform: scale(0.01); }
90% { transform: scale(0.03); }
96% { transform: scale(0); }
98% { transform: scale(0.01); }
100% { transform: scale(0); }
}
</style>
</head>
<body>
<div id="myRectangle"></div>
<button id="toggleButton">Toggle Animation</button>
<script>
document.getElementById('toggleButton').addEventListener('click', function() {
var rectangle = document.getElementById('myRectangle');
if (rectangle.classList.contains('animation-in')) {
rectangle.classList.remove('animation-in');
rectangle.classList.add('animation-out');
} else {
rectangle.classList.remove('animation-out');
rectangle.classList.add('animation-in');
}
});
</script>
</body>
</html>
https://jsfiddle.net/midnightstudios/vajqdcLo/2/个
以下是被破解的代码:
<!DOCTYPE html>
<html>
<head>
<title>Rectangle Animation</title>
<style>
#myRectangle {
width: 100px;
height: 150px;
background-color: green;
margin: 20px;
transform: scale(0);
}
.animation-out {
animation-duration: 4s;
animation-timing-function: ease; /*linear*/
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: forwards;
animation-play-state: running;
animation-name: scale-easeInOutBounce;
animation-timeline: auto;
animation-range-start: normal;
animation-range-end: normal;
}
.animation-in {
animation-duration: 4s;
animation-timing-function: ease;
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: reverse;
animation-fill-mode: forwards;
animation-play-state: running;
animation-name: scale-easeInOutBounce;
animation-timeline: auto;
animation-range-start: normal;
animation-range-end: normal;
}
@keyframes scale-easeInOutBounce {
0% { transform: scale(1); }
2% { transform: scale(0.99); }
4% { transform: scale(1); }
10% { transform: scale(0.97); }
14% { transform: scale(0.99); }
22% { transform: scale(0.88); }
32% { transform: scale(0.99); }
42% { transform: scale(0.6); }
50% { transform: scale(0.5); }
58% { transform: scale(0.4); }
68% { transform: scale(0.01); }
78% { transform: scale(0.12); }
86% { transform: scale(0.01); }
90% { transform: scale(0.03); }
96% { transform: scale(0); }
98% { transform: scale(0.01); }
100% { transform: scale(0); }
}
</style>
</head>
<body>
<div id="myRectangle"></div>
<button id="toggleButton">Toggle Animation</button>
<script>
document.getElementById('toggleButton').addEventListener('click', function() {
var rectangle = document.getElementById('myRectangle');
if (rectangle.classList.contains('animation-in')) {
rectangle.classList.remove('animation-in');
rectangle.classList.add('animation-out');
} else {
rectangle.classList.remove('animation-out');
rectangle.classList.add('animation-in');
}
});
</script>
</body>
</html>