所以我在CSS动画中很新,我想问一个问题--我创建了这个盒子,它作为一个小盒子,从上到下,从右到左,交替旋转,但是我想知道怎样才能使它从原点一直旋转。我尝试将transform translateX设置为-232%,一旦它回到原点,但最终不会回到原点作为意图。
<div class="parent">
<div class="child"></div>
</div>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 62.5%;
font-family: Arial, Helvetica, sans-serif;
}
body {
font-size: 1.22rem;
line-height: 1.2;
}
.parent {
background: #aed4ff;
height: 300px;
width: 300px;
}
.child {
background-color: rgb(143, 36, 36);
width: 90px;
height: 90px;
display: block;
}
.parent:hover .child {
animation: left-to-right 2s ease-in-out forwards;
animation-play-state: paused;
cursor: pointer;
}
@keyframes left-to-right {
0% {
transform: translateX(0);
color: red;
}
33% {
transform: translateY(232%);
}
50% {
background-color: blue;
}
66% {
transform: translateX(232%) translateY(232%);
}
100% {
transform: translateX(232%);
background-color: black;
}
}
</style>发布于 2022-10-19 23:31:40
您需要添加另一个关键帧,translateX(0)在100%时才能将元素移回原来的位置,而对于其他“转换-关键帧”,则需要使用25%、50%和75%,而不是33%、67%和100%。
当然还有animation-iteration-count: infinite;来保持它的运转。
所以那会是
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 62.5%;
font-family: Arial, Helvetica, sans-serif;
}
body {
font-size: 1.22rem;
line-height: 1.2;
}
.parent {
background: #aed4ff;
height: 300px;
width: 300px;
}
.child {
background-color: rgb(143, 36, 36);
width: 90px;
height: 90px;
display: block;
}
.parent:hover .child {
animation: left-to-right 3s ease-in-out forwards;
animation-iteration-count: infinite;
cursor: pointer;
}
@keyframes left-to-right {
0% {
transform: translateX(0);
color: red;
}
25% {
transform: translateY(232%);
}
50% {
transform: translateX(232%) translateY(232%);
background-color: blue;
}
75% {
transform: translateX(232%);
}
100% {
transform: translateX(0);
background-color: black;
}
}<div class="parent">
<div class="child"></div>
</div>
发布于 2022-10-19 23:22:39
通过将动画添加到css中,可以将动画设置为连续运行。
animation-iteration-count: infinite;https://stackoverflow.com/questions/74132865
复制相似问题