How to play animation on hover?

前端 未结 4 1401
说谎
说谎 2021-02-15 18:35

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

4条回答
  •  太阳男子
    2021-02-15 19:23

    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); }           
    }

提交回复
热议问题