css:防止动画重置鼠标外?

时间:2021-07-26 20:22:57

I have an animation that I'm using to make an image scroll:

我有一个动画用来制作一个图像滚动:

.my-image:hover {
    -webkit-animation: my-image-anim 20s linear 0s infinite;
    animation: my-image-anim 20s linear 0s infinite;
}

@-webkit-keyframes my-image-anim {
    0%  { margin-left: 0%; }
    100% { margin-left: -100%; }
}

@keyframes my-image-anim {
    0%  { margin-left: 0%; }
    100% { margin-left: -100%; }
}

When I hover the mouse over the image, is scrolls to the left. When the mouse leaves the image, the animation resets to margin-left=0.

当我将鼠标悬停在图像上时,是滚动到左边。当鼠标离开图像时,动画重新设置为margin-left=0。

I would like the animation stop wherever it is on mouse exit, then resume when the mouse enters again. Is this possible to do using only css?

我希望动画在鼠标退出时停止,然后在鼠标再次进入时恢复。这能只用css实现吗?

1 个解决方案

#1


3  

Yes. You want to use animation-play-state.

是的。你想要使用动画-播放-状态。

.my-image{
-webkit-animation: my-image-anim 20s linear 0s infinite;
animation: my-image-anim 20s linear 0s infinite;
animation-play-state: paused;
-webkit-animation-play-state: paused;
}

.my-image:hover{
animation-play-state: running;
-webkit-animation-play-state: running;
}

#1


3  

Yes. You want to use animation-play-state.

是的。你想要使用动画-播放-状态。

.my-image{
-webkit-animation: my-image-anim 20s linear 0s infinite;
animation: my-image-anim 20s linear 0s infinite;
animation-play-state: paused;
-webkit-animation-play-state: paused;
}

.my-image:hover{
animation-play-state: running;
-webkit-animation-play-state: running;
}