CSS3旋转动画

iam*_*eed 146 css3 css-animations

我已经回顾了很多演示,并且不知道为什么我不能让CSS3旋转功能.我正在使用Chrome的最新稳定版本.

小提琴:http: //jsfiddle.net/9Ryvs/1/

div {
  margin: 20px;
  width: 100px;
  height: 100px;
  background: #f00;
  -webkit-animation-name: spin;
  -webkit-animation-duration: 40000ms;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: linear;
  -moz-animation-name: spin;
  -moz-animation-duration: 40000ms;
  -moz-animation-iteration-count: infinite;
  -moz-animation-timing-function: linear;
  -ms-animation-name: spin;
  -ms-animation-duration: 40000ms;
  -ms-animation-iteration-count: infinite;
  -ms-animation-timing-function: linear;
  -o-transition: rotate(3600deg);
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
Run Code Online (Sandbox Code Playgroud)

Gab*_*oli 277

要使用CSS3动画,您还必须定义实际的动画关键帧(您命名spin)

阅读https://developer.mozilla.org/en-US/docs/CSS/Tutorials/Using_CSS_animations了解更多信息

一旦配置了动画的时序,就需要定义动画的外观.这是通过使用@keyframesat-rule 建立两个或多个关键帧来完成的.每个关键帧描述动画元素在动画序列期间在给定时间应如何呈现.


演示http://jsfiddle.net/gaby/9Ryvs/7/

@-moz-keyframes spin {
    from { -moz-transform: rotate(0deg); }
    to { -moz-transform: rotate(360deg); }
}
@-webkit-keyframes spin {
    from { -webkit-transform: rotate(0deg); }
    to { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
    from {transform:rotate(0deg);}
    to {transform:rotate(360deg);}
}
Run Code Online (Sandbox Code Playgroud)

  • 这是超级挑剔,但你真的应该有359度的动画.360度和0度在径向上是相同的,因此动画会在整圈时暂停. (46认同)
  • 澄清所有提供错误信息的评论...所选答案是正确无需修改.0和360度实际上在浏览器的眼中是不同的,但仍然是相同的点.例如,如果您尝试将其从0deg旋转到0deg(或360deg到360deg),则它根本不会旋转.将其从0deg旋转到360deg告诉浏览器在完成动画之前将对象旋转360度.设置`animation-iteration-count:infinite;`,你将在动画中拥有无限的帧.即使是20分钟的旋转也会看起来完美无瑕. (11认同)
  • 你得到✓因为你解释得最好而且你是唯一包含所有前缀版本的答案. (7认同)
  • 你想要动画为359.9999999999度,而不是359.旋转度是[0,360]的连续范围,如果你旋转到359.0,当它从359扭转到0时,你会在每次旋转开始时有1度刻度. (4认同)

Jez*_*mas 26

您尚未指定任何关键帧.我在这里工作了.

div {
    margin: 20px;
    width: 100px; 
    height: 100px;    
    background: #f00;
    -webkit-animation: spin 4s infinite linear;
}

@-webkit-keyframes spin {
    0%  {-webkit-transform: rotate(0deg);}
    100% {-webkit-transform: rotate(360deg);}   
}
Run Code Online (Sandbox Code Playgroud)

你可以用这个做很多非常酷的东西.这是我之前制作的.

:)

注意如果您使用-prefix-free,则可以跳过必须写出所有前缀.


ori*_*dam 15

从最新的Chrome/FF和IE11开始,不需要-ms/-moz/-webkit前缀.这是一个较短的代码(基于以前的答案):

div {
    margin: 20px;
    width: 100px;
    height: 100px;
    background: #f00;

    /* The animation part: */
    animation-name: spin;
    animation-duration: 4000ms;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}
@keyframes spin {
    from {transform:rotate(0deg);}
    to {transform:rotate(360deg);}
}
Run Code Online (Sandbox Code Playgroud)

现场演示:http://jsfiddle.net/9Ryvs/3057/

  • 将动画规则与速记`动画:旋转4s线性无限`相结合. (5认同)

Per*_*ges 8

带有字体令人敬畏的glyphicon的HTML.

<span class="fa fa-spinner spin"></span>
Run Code Online (Sandbox Code Playgroud)

CSS

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

.spin {
    animation: spin 1000ms linear infinite;
}
Run Code Online (Sandbox Code Playgroud)


dan*_*y74 6

给出正确 359deg 的唯一答案:

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(359deg); }
}

&.active {
  animation: spin 1s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)

这是一个有用的渐变,因此您可以证明它正在旋转(如果它是一个圆圈):

background: linear-gradient(to bottom, #000000 0%,#ffffff 100%);
Run Code Online (Sandbox Code Playgroud)