如何做一个webkit css无尽的旋转动画.

Ale*_*iov 126 css animation webkit

我想通过CSS轮换我的加载图标.

我有一个图标和以下代码:

<style>
#test {
    width: 32px;
    height: 32px;
    background: url('refresh.png');
}

.rotating {
    -webkit-transform: rotate(360deg);
    -webkit-transition-duration: 1s;
    -webkit-transition-delay: now;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
}
</style>

<div id='test' class='rotating'></div>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何使用CSS旋转图标?

Kir*_*eck 224

@-webkit-keyframes rotating /* Safari and Chrome */ {
  from {
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
@keyframes rotating {
  from {
    -ms-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -webkit-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  to {
    -ms-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
.rotating {
  -webkit-animation: rotating 2s linear infinite;
  -moz-animation: rotating 2s linear infinite;
  -ms-animation: rotating 2s linear infinite;
  -o-animation: rotating 2s linear infinite;
  animation: rotating 2s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)
<div 
  class="rotating"
  style="width: 100px; height: 100px; line-height: 100px; text-align: center;" 
 >Rotate</div>
Run Code Online (Sandbox Code Playgroud)

  • 一个问题,在`-webkit-keyframes`下需要`-moz -`和`-ms -`前缀?因为只有webkit会读取`-webkit-keyframes`我相信删除它们是安全的. (11认同)
  • 我是否正确理解这在理论上并不完美,因为非供应商前缀属性应始终放在最后,以免覆盖符合标准的行为?请参阅:http://css-tricks.com/ordering-css3-properties/ (2认同)

Ale*_*iov 86

工作很好:

#test {
    width: 11px;
    height: 14px;
    background: url('data:image/gif;base64,R0lGOD lhCwAOAMQfAP////7+/vj4+Hh4eHd3d/v7+/Dw8HV1dfLy8ubm5vX19e3t7fr 6+nl5edra2nZ2dnx8fMHBwYODg/b29np6eujo6JGRkeHh4eTk5LCwsN3d3dfX 13Jycp2dnevr6////yH5BAEAAB8ALAAAAAALAA4AAAVq4NFw1DNAX/o9imAsB tKpxKRd1+YEWUoIiUoiEWEAApIDMLGoRCyWiKThenkwDgeGMiggDLEXQkDoTh CKNLpQDgjeAsY7MHgECgx8YR8oHwNHfwADBACGh4EDA4iGAYAEBAcQIg0Dk gcEIQA7');
}

@-webkit-keyframes rotating {
    from{
        -webkit-transform: rotate(0deg);
    }
    to{
        -webkit-transform: rotate(360deg);
    }
}

.rotating {
    -webkit-animation: rotating 2s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)
<div id='test' class='rotating'></div>
Run Code Online (Sandbox Code Playgroud)

  • @andrej刚加了一个 (4认同)

Rok*_*jan 7

/* ENDLESS ROTATE */
.rotate{
  animation: rotate 1.5s linear infinite; 
}
@keyframes rotate{
  to{ transform: rotate(360deg); }
}


/* SPINNER JUST FOR DEMO */
.spinner{
  display:inline-block; width: 50px; height: 50px;
  border-radius: 50%;
  box-shadow: inset -2px 0 0 2px #0bf;
}
Run Code Online (Sandbox Code Playgroud)
<span class="spinner rotate"></span>
Run Code Online (Sandbox Code Playgroud)


Kar*_*eem 6

适用于所有现代浏览器

.rotate{
 animation: loading 3s linear infinite;
 @keyframes loading {
  0% { 
    transform: rotate(0); 
  }
  100% { 
    transform: rotate(360deg);
  }
 }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

@keyframes rotate {
    100% {
        transform: rotate(1turn);
    }
}

div{
   animation: rotate 4s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)

  • 您好,欢迎来到 Stack Overflow!当您回答问题时,您应该提供某种解释,例如作者做错了什么以及您如何解决它。我告诉您这一点是因为您的答案已被标记为低质量,目前正在接受审核。您可以通过单击“编辑”按钮来[编辑]您的答案。 (4认同)

Dor*_*ian 5

没有任何前缀,例如,这是最简单的:

.loading-spinner {
  animation: rotate 1.5s linear infinite;
}

@keyframes rotate {
  to {
    transform: rotate(360deg);
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

只需尝试一下即可。工作正常

@-webkit-keyframes loading {
    from {
        -webkit-transform: rotate(0deg);
    }
    to {
        -webkit-transform: rotate(360deg);
    }
}
@-moz-keyframes loading {
    from {
        -moz-transform: rotate(0deg);
    }
    to {
        -moz-transform: rotate(360deg);
    }
}

#loading {
    width: 16px;
    height: 16px;
    -webkit-animation: loading 2s linear infinite;
    -moz-animation: loading 2s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)
<div class="loading-test">
    <svg id="loading" aria-hidden="true" focusable="false" role="presentation" class="icon icon-spinner" viewBox="0 0 20 20"><path d="M7.229 1.173a9.25 9.25 0 1 0 11.655 11.412 1.25 1.25 0 1 0-2.4-.698 6.75 6.75 0 1 1-8.506-8.329 1.25 1.25 0 1 0-.75-2.385z" fill="#919EAB"/></svg>
</div>
Run Code Online (Sandbox Code Playgroud)