Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css: prevent reset of animation on mouse out?

Tags:

css

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.

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?

like image 717
CuriousGeorge Avatar asked Feb 22 '15 21:02

CuriousGeorge


People also ask

How do you keep an animation in CSS?

The only way to truly pause an animation in CSS is to use the animation-play-state property with a paused value. In JavaScript, the property is “camelCased” as animationPlayState and set like this: element.

How do you stop hover transition in CSS?

By setting the animation-play-state to paused on hover, the animation will pause, your click targets will stop moving, and your users will be happy.

Why is my CSS animation not working?

Animation Duration Not Set By default, a CSS animation cycle is zero seconds long. To override this, add an animation-duration rule to your targeted element with a seconds value, in the same block as animation-name. Below, try removing the comments around animation-duration to fix the animation.


1 Answers

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;
}
like image 104
Dan Ovidiu Boncut Avatar answered Sep 30 '22 14:09

Dan Ovidiu Boncut