Slow down a rotation on hover
Did you ever wanted to slow down a rotating element on mouse hover? You can easily do it with a simple code:
- No extra element
- No pseudo-element
- One animation
- Easy to control with CSS variables
.box {
--d: 5s; /* animation duration */
--s: 0.5; /* speed factor
0 : nothing happens
[0 1]: decrease the speed
1 : stops the animation
[1 +infinity[ : move in the opposite direction
*/
--_a: r linear infinite;
animation:
var(--_a) var(--d),
var(--_a) calc(var(--d)/var(--s))
reverse paused;
animation-composition: add;
}
.box:hover {
animation-play-state: running;
}
@keyframes r {
to {rotate: 1turn}
}
See the Pen Hover to slow down the animation by Temani Afif (@t_afif) on CodePen.
The same trick can be used to accelerate the rotation
More CSS Tips
- A reveal hover effect with an expanding circle A simple but nice hover effect to reveal your image.
- A CSS-only array of colors Create an array of colors and use an index to navigate.
- Color your image with a sliding hover effect Reveal the color of a black & white image using a simple code.
- 3D shine effect on hover A fancy 3D effect with a shiny animation on hover.