简体   繁体   English

如何在光标悬停时旋转图像并保持旋转状态?

[英]how can i rotate an image and keep rotated while the cursor is hovering?

It's an issue with the circular flag icon in the screenshot below. 下面的屏幕截图中的圆形标记图标存在问题。 I want it to rotate 45 degrees clockwise and keep it rotated in + shape as long as the cursor is hovering on the image; 我希望它可以顺时针旋转45度,并且只要光标悬停在图像上就可以使其呈+形状旋转; then rotate back ccw when the cursor leaves the image. 然后在光标离开图像时逆时针旋转。

在此处输入图片说明

This is the code I wrote so far. 这是我到目前为止编写的代码。 When hovering the image rotates 45' cw correctly but it goes back to X shape immediately. 悬停时,图像会正确旋转45'cw,但会立即恢复为X形状。

#icon{
    display: inline-block;
    margin: 0 auto 0 0;
    width: 2.4em;
    height: 2.4em;
}

#icon:hover{
    -webkit-animation:cw 0.4s linear 1;
    -moz-animation:cw 0.4s linear 1;
    animation:cw 0.4s linear 1;
}

@-moz-keyframes cw { 100% { -moz-transform: rotate(45deg); } }
@-webkit-keyframes cw { 100% { -webkit-transform: rotate(45deg); } }
@keyframes cw { 100% { -webkit-transform: rotate(45deg); transform:rotate(45deg); } }

@-moz-keyframes ccw { 100% { -moz-transform: rotate(-45deg); } }
@-webkit-keyframes ccw { 100% { -webkit-transform: rotate(-45deg); } }
@keyframes ccw { 100% { -webkit-transform: rotate(-45deg); transform:rotate(-45deg); } }

So how can I solve this problem? 那么我该如何解决这个问题呢? I think the #icon should be modified a bit because it might keep its original state even after rotation. 我认为#icon应该进行一些修改,因为即使旋转后它也可能保持其原始状态。

Use a transition instead of an keyframes animation: 使用transition而不是keyframes动画:

 img { transition: all .2s ease; transform: rotate(0deg) } img:hover { transform: rotate(45deg) } 
 <img src="https://placehold.it/100x100"> 

Try using animation-fill-mode: forwards; 尝试使用animation-fill-mode: forwards; or use animation:cw 0.4s linear 1 forwards; 或使用animation:cw 0.4s linear 1 forwards;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM