【问题标题】:Run css animation every minute [duplicate]每分钟运行一次css动画[重复]
【发布时间】:2015-12-02 22:37:25
【问题描述】:

如何每 5 分钟再次运行一次 CSS 动画?

html

<div></div>

css

div {
  width: 300px;
  height: 300px;
  animation: anim 1s;
  background: brown;
}
@keyframes anim {
  from {
    background: red;
  }
  top {
    background: blue;
  }
}

在没有 javascript 的情况下需要这个

【问题讨论】:

    标签: html css css-transitions


    【解决方案1】:

    如果你想要一个纯 CSS 解决方案,你需要“伪造”它。使用百分比属性和动画持续时间来模拟延迟。

    例如

    div {
      height: 20px;
      width: 20px;
      background-color: blue;
      animation: changebgc 5s infinite;
    }
    @keyframes changebgc {
      2% {
        background-color: red;
      }
      4% {
        background-color: blue;
      }
      98% {
        background-color: blue;
      }
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      在 CSS 中执行此操作需要一些技巧,但可以做到。您将不得不做一些数学运算,并找到如何正确计时动画:

      div {
        width: 300px;
        height: 300px;
        animation: anim 300s infinite;
        background: brown;
      }
      
      @keyframes anim {
        0% {
          background: red;
        }
        0.33% {
          background: blue;
        }
        100% {
          background: blue;
        }
      }
      

      JSFiddle demo
      这样做的一个问题是更难改变时间。在当前设置下,red - blue 转换需要 1 秒,就像您在 JSFiddle 中所做的那样。
      计算方法很简单:
      100 / #_seconds = percentage in animation for 1 sec 例如100 / 300 = 0.33

      您可以在此网站上阅读更多相关信息:
      http://samuelmichaud.fr/2013/web-design/add-a-delay-between-repeated-css3-animations/

      【讨论】:

        【解决方案3】:

        我认为您可以使用“动画持续时间”和“动画延迟”规则。您可以设置效果的持续时间,以及等待下一次迭代的延迟。

        div{
          width: 300px;
          height: 300px;
        
          background: brown;
          animation-name: anim;
          animation-duration: 10s;
          animation-iteration-count: 10000;
          animation-direction: alternate;
          animation-timing-function: ease-out;
          animation-fill-mode: forwards;
          animation-delay: 50s;
        }
        @keyframes anim {
          from {
            background: red;
          }
          to {
            background: blue;
          }
        }
        

        希望对你有帮助。

        问候

        【讨论】:

          猜你喜欢
          • 2016-07-17
          • 1970-01-01
          • 2014-12-01
          • 2019-11-10
          • 1970-01-01
          • 2012-08-26
          • 1970-01-01
          • 2016-01-04
          • 1970-01-01
          相关资源
          最近更新 更多