我有一个齿轮图标,当被点击激活(class="cards__cog cards__cog-active")时,我希望它顺时针旋转180度,再顺时针旋转另外180度回到未激活状态(class="cards__cog cards__cog-inactive")。我正在使用React,在齿轮被点击时触发状态改变。
以下代码可以实现上述效果,但存在以下问题:
1)它会在页面加载时进行动画(这很合理,因为它具有cards__cog-inactive类,但有没有其他方法?)
2)它比较丑陋,一定有更简单的方式。
谢谢。
以下代码可以实现上述效果,但存在以下问题:
1)它会在页面加载时进行动画(这很合理,因为它具有cards__cog-inactive类,但有没有其他方法?)
2)它比较丑陋,一定有更简单的方式。
谢谢。
.cards {
&__cog {
position: absolute;
right: 20px;
top: 20px;
width: 10vh;
cursor: pointer;
&-active {
animation: rotate180 1s ease;
animation-fill-mode: forwards;
}
&-inactive {
animation: rotate180to359to0 1s ease;
animation-fill-mode: forwards;
}
}
}
@keyframes rotate180 {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(180deg);
}
}
@keyframes rotate180to359to0 {
0% {
transform: rotate(180deg);
}
99% {
transform: rotate(359deg);
}
100% {
transform: rotate(0deg);
}
}