【问题标题】:How to play CSS3 transitions in a loop?如何循环播放 CSS3 过渡?
【发布时间】:2012-10-29 20:49:00
【问题描述】:

以下样式只是如何在 CSS3 中设置过渡的示例。
有没有一个纯 CSS 技巧可以让这个循环播放?

div {
    width:100px;
    height:100px;
    background:red;
    transition:width 0.1s;
    -webkit-transition:width 0.1s; /* Safari and Chrome */
    -moz-transition:width 0.1s; /* Firefox 4 */
    -o-transition:width 0.1s; /* Opera */
    transition:width 0.1s; /* Opera */
}

div:hover {
    width:300px;
}

【问题讨论】:

    标签: html css stylesheet transform


    【解决方案1】:

    如果您想利用“变换”属性提供的 60FPS 平滑度,您可以将两者结合起来:

    @keyframes changewidth {
      from {
        transform: scaleX(1);
      }
    
      to {
        transform: scaleX(2);
      }
    }
    
    div {
      animation-duration: 0.1s;
      animation-name: changewidth;
      animation-iteration-count: infinite;
      animation-direction: alternate;
    }
    

    关于为什么 transform 提供更平滑的过渡的更多解释: https://medium.com/outsystems-experts/how-to-achieve-60-fps-animations-with-css3-db7b98610108

    【讨论】:

      【解决方案2】:

      CSS 过渡只会从一组样式动画到另一组样式;你要找的是CSS animations

      您需要定义动画关键帧并将其应用于元素:

      @keyframes changewidth {
        from {
          width: 100px;
        }
      
        to {
          width: 300px;
        }
      }
      
      div {
        animation-duration: 0.1s;
        animation-name: changewidth;
        animation-iteration-count: infinite;
        animation-direction: alternate;
      }
      

      查看上面的链接以了解如何根据自己的喜好对其进行自定义,并且您必须添加浏览器前缀。

      【讨论】:

        猜你喜欢
        • 2014-05-02
        • 2015-06-10
        • 2013-02-13
        • 2012-03-26
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        相关资源
        最近更新 更多