Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play an animation on hover and pause when hover is not active

I have a button that I want to rotate when the mouse hovers over it. However, while the spinning works when the mouse enters, it also spins when the mouse leaves. Here is what I mean:

button {
  transition: transform 1.2s linear;
}

button:hover {
  transform: rotate(360deg);
}
<button>X</button>

Is there any way to just make the mouse spin when the mouse enters?

like image 690
MarksCode Avatar asked Aug 11 '16 06:08

MarksCode


People also ask

How do you pause hover?

Default. The typical use case for pausing a slideshow on a hover event is to pause when the mouse is over the slideshow. This is accomplished by setting the data-cycle-pause-on-hover attribute value to true .


1 Answers

Use animation instead of transition and make use of animation-play-stateMDN

button{
  animation: rotate360 1.2s linear infinite;  /* animation set */
  animation-play-state: paused;               /* but paused */
}
button:hover{
  animation-play-state: running;              /* trigger on hover */
}
@keyframes rotate360 {
  to { transform: rotate(360deg); }           
}
<button>X</button>
like image 152
Roko C. Buljan Avatar answered Jan 02 '23 09:01

Roko C. Buljan