【问题标题】:CSS3 Rotate AnimationCSS3 旋转动画
【发布时间】:2013-05-22 04:40:55
【问题描述】:
<img class="image" src="" alt="" width="120" height="120">

无法让这个动画图像工作,它应该进行 360 度旋转。

我猜下面的 CSS 有问题,因为它只是静止不动。

.image {
    float: left;
    margin: 0 auto;
    position: absolute;
    top: 50%;
    left: 50%;
    width: 120px;
    height: 120px;
    margin-top: -60px;
    margin-left: -60px;

    -webkit-animation-name: spin;
    -webkit-animation-duration: 4000ms;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;

    -moz-animation-name: spin;
    -moz-animation-duration: 4000ms;
    -moz-animation-iteration-count: infinite;
    -moz-animation-timing-function: linear;

    -ms-animation-name: spin;
    -ms-animation-duration: 4000ms;
    -ms-animation-iteration-count: infinite;
    -ms-animation-timing-function: linear;

    animation-name: spin;
    animation-duration: 4000ms;
    animation-iteration-count: infinite;
    animation-timing-function: linear;

    @-ms-keyframes spin { 
        from { 
            -ms-transform: rotate(0deg); 
        } to { 
            -ms-transform: rotate(360deg); 
        }
    }
    @-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); 
        }
    }
}

【问题讨论】:

    标签: image css animation rotation


    【解决方案1】:

    使用 css3 在后台旋转对象的另一种方法,在此处查看以下 css3 代码:

    .floating-ball-model-3 > span {
        animation-name: floating-ball-model-3;
        animation-duration: 7s;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
        -webkit-animation-name: floating-ball-model-3;
        -webkit-animation-duration: 7s;
        -webkit-animation-iteration-count: infinite;
        -webkit-animation-timing-function: linear;
        -moz-animation-name: floating-ball-model-3;
        -moz-animation-duration: 7s;
        -moz-animation-iteration-count: infinite;
        -moz-animation-timing-function: linear;
        -ms-animation-name: floating-ball-model-3;
        -ms-animation-duration: 7s;
        -ms-animation-iteration-count: infinite;
        -ms-animation-timing-function: linear;
        -o-animation-name: floating-ball-model-3;
        -o-animation-duration: 7s;
        -o-animation-iteration-count: infinite;
        -o-animation-timing-function: linear;
    }
    
    
    @keyframes floating-ball-model-3 {
        from {
            transform: rotate(0deg);
        }
        to { 
            transform: rotate(360deg);
        }
    }
    

    【讨论】:

    • 不带前缀的属性通常应该排在最后。原因是属性相互覆盖,所以如果浏览器支持无前缀版本,这些应该覆盖上面设置的任何非标准值。
    【解决方案2】:

    这是 demo。正确的动画 CSS:

    .image {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 120px;
        height: 120px;
        margin:-60px 0 0 -60px;
        -webkit-animation:spin 4s linear infinite;
        -moz-animation:spin 4s linear infinite;
        animation:spin 4s linear infinite;
    }
    @-moz-keyframes spin { 
        100% { -moz-transform: rotate(360deg); } 
    }
    @-webkit-keyframes spin { 
        100% { -webkit-transform: rotate(360deg); } 
    }
    @keyframes spin { 
        100% { 
            -webkit-transform: rotate(360deg); 
            transform:rotate(360deg); 
        } 
    }
    &lt;img class="image" src="http://i.stack.imgur.com/pC1Tv.jpg" alt="" width="120" height="120"&gt;

    关于您的代码的一些注释:

    1. 您已将关键帧嵌套在 .image 规则中,这是不正确的
    2. float:left 不适用于绝对定位的元素
    3. 看看caniuse:IE10不需要-ms-前缀

    【讨论】:

    • 如果有人在自己的代码中尝试这样做:不要忘记底部的 @ 部分
    • 您好,我可以在 5 秒后停止动画无限旋转吗?
    • 运行动画时差点把水吐出来。
    • " float:left won't work on absolutely positioned elements " ,它会将它们缩小到最小尺寸,类似于 display: inline-block 所做的
    【解决方案3】:

    试试这个简单的

     
     .btn-circle span {
         top: 0;
       
          position: absolute;
         font-size: 18px;
           text-align: center;
        text-decoration: none;
          -webkit-animation:spin 4s linear infinite;
        -moz-animation:spin 4s linear infinite;
        animation:spin 4s linear infinite;
    }
    
    .btn-circle span :hover {
     color :silver;
    }
    
    
    /* rotate 360 key for refresh btn */
    @-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
    @-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
    @keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } } 
     &lt;button type="button" class="btn btn-success btn-circle" &gt;&lt;span class="glyphicon"&gt;&amp;#x21bb;&lt;/span&gt;&lt;/button&gt;

    【讨论】:

      【解决方案4】:

      我有一个和你一样的旋转图像:

      .knoop1 img{
          position:absolute;
          width:114px;
          height:114px;
          top:400px;
          margin:0 auto;
          margin-left:-195px;
          z-index:0;
      
          -webkit-transition-duration: 0.8s;
          -moz-transition-duration: 0.8s;
          -o-transition-duration: 0.8s;
          transition-duration: 0.8s;
      
          -webkit-transition-property: -webkit-transform;
          -moz-transition-property: -moz-transform;
          -o-transition-property: -o-transform;
           transition-property: transform;
      
           overflow:hidden;
      }
      
      .knoop1:hover img{
          -webkit-transform:rotate(360deg);
          -moz-transform:rotate(360deg); 
          -o-transform:rotate(360deg);
      }
      

      【讨论】:

      • 添加:transform:rotate(360deg); 用于 IE
      • 请选择本地示例而不是损坏的网站链接。
      • 链接已断开。
      【解决方案5】:

      要实现360度旋转,这里是Working Solution

      HTML:

      <img class="image" src="your-image.png">
      

      CSS:

      .image {
          overflow: hidden;
          transition-duration: 0.8s;
          transition-property: transform;
      }
      .image:hover {
          transform: rotate(360deg);
          -webkit-transform: rotate(360deg);
      }
      

      您必须将鼠标悬停在图像上,您将获得 360 度旋转效果。

      PS:添加一个-webkit- 扩展,使其可以在 chrome 和其他 webkit 浏览器上运行。您可以检查 webkit 的更新小提琴 HERE

      【讨论】:

      • Fiddle 不起作用 o.O Chrome 检查器不喜欢你的 CSS,特别是“transform”和“transition-property”。哦,亲爱的。
      • 为此,您需要添加 -webkit-tranform 才能使其正常工作。这是更新的小提琴。 jsfiddle.net/sELBM/172 - @JustPlainHigh
      • 2017:现在得到了很好的支持,并且是进行非无限旋转的首选方式。 -webkit- 前缀不再需要,可以安全删除。浏览器支持:caniuse.com/#search=transforms
      • 干净漂亮
      【解决方案6】:

      如果你想翻转图像,你可以使用它。

      .image{
          width: 100%;
          -webkit-animation:spin 3s linear infinite;
          -moz-animation:spin 3s linear infinite;
          animation:spin 3s linear infinite;
      }
      @-moz-keyframes spin { 50% { -moz-transform: rotateY(90deg); } }
      @-webkit-keyframes spin { 50% { -webkit-transform: rotateY(90deg); } }
      @keyframes spin { 50% { -webkit-transform: rotateY(90deg); transform:rotateY(90deg); } }
      

      【讨论】:

        【解决方案7】:

        这里应该对你有帮助

        下面的 jsfiddle 链接将帮助您了解如何旋转图像。我用同一个来旋转时钟的表盘。

        http://jsfiddle.net/xw89p/

        var rotation = function (){
           $("#image").rotate({
              angle:0, 
              animateTo:360, 
              callback: rotation,
              easing: function (x,t,b,c,d){       
                  return c*(t/d)+b;
              }
           });
        }
        rotation();
        

        在哪里: • t:当前时间,

        • b:起始值,

        • c:改变值,

        • d:持续时间,

        • x:未使用

        无缓动(线性缓动): 函数(x, t, b, c, d) { 返回 b+(t/d)*c ; }

        【讨论】:

        • 如果您提供更多信息(就像您在小提琴中所做的那样),我会赞成这个答案。我也觉得你应该提到这是一个 jQuery 插件,因为我都喜欢,“我不知道 jQuery 可以做到这一点!!!^_^ 看小提琴哦等等...... U_U”
        • 对 x,t,b,c,d 变量的解释(就像你在小提琴中所做的那样)并清楚地说明虽然它不是 OP 要求的 CSS 解决方案,但它相当一个简单有效的jQuery插件解决方案:)
        • 投反对票。 OP 没有要求 Javascript 解决方案,尤其是没有 jQuery 插件。坚持所要求的内容。
        猜你喜欢
        • 2015-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 2014-02-24
        相关资源
        最近更新 更多