How can I change the speed of a CSS animation?

前端 未结 2 1343
一生所求
一生所求 2020-12-06 05:44

I have animation:

img.rotate {
  animation-name: rotate;
  animation-duration: 1.5s;
  animation-iteration-count: inf         


        
2条回答
  •  暖寄归人
    2020-12-06 06:13

    You can do that by setting a wrapper, and counter-rotating it.

    CSS

    .wrapper, .inner {
        position: absolute;
        width: 100px;
        height: 100px;
      -webkit-animation-name: rotate;
      -webkit-animation-iteration-count: infinite;
      -webkit-animation-timing-function: linear;
      animation-name: rotate;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
    }
    .wrapper {
        left: 40px;
        top: 40px;
      -webkit-animation-duration: 2.5s;
        -webkit-animation-play-state: paused;
        -webkit-animation-direction: reverse;
      animation-duration: 2.5s;
        animation-play-state: paused;
        animation-direction: reverse;
    }
    .wrapper:hover {
        -webkit-animation-play-state: running;
        animation-play-state: running;
    }
    .inner {
        display: block;
        background-color: red;
      -webkit-animation-duration: 1.5s;
      animation-duration: 1.5s;
    }
    @keyframes rotate {
      from { transform: rotate(0deg);  }
      to {    transform: rotate(360deg);  }
    }    
    @-webkit-keyframes rotate {
      from { -webkit-transform: rotate(0deg);  }
      to {    -webkit-transform: rotate(360deg);  }
    }    
    

    demo

    (hover the square to slow it)

提交回复
热议问题