代码之家  ›  专栏  ›  技术社区  ›  Jacksonkr

一个常规动画一个循环动画没有JS

  •  3
  • Jacksonkr  · 技术社区  · 7 年前

    如何播放0%-50%的关键帧,然后从50%-100%连续循环?我知道通过从DIV中添加/删除类可以做到这一点,但我希望在没有JavaScript的情况下做到这一点。

    body {
      background: black;
    }
    
    @keyframes divReady {
      0% { width: 0vmin; }
      50% { width: 20vmin; transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    
    #the-div {
      position: absolute;
      top: 40vmin;
      left: 40vmin;
      width: 0vmin;
      height: 20vmin;
      background: orange;
      animation: divReady 2s;
      animation-iteration-count: 2;
    }
    <div id="the-div">
    </div>
    2 回复  |  直到 7 年前
        1
  •  4
  •   Temani Afif    7 年前

    考虑两个动画。一带 forwards 以及无限的,其延迟等于第一个延迟的持续时间:

    body {
      background: black;
    }
    
    @keyframes divReady-one {
      0% { width: 0vmin; }
      100% { width: 20vmin; }
    }
    
    @keyframes divReady-two {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    
    #the-div {
      position: absolute;
      top: 40vmin;
      left: 40vmin;
      width: 0vmin;
      height: 20vmin;
      background: orange;
      animation: 
        divReady-one 1s forwards,
        divReady-two 2s 1s infinite;
    }
    <div id="the-div">
    </div>
        2
  •  2
  •   Ginger Wizard    7 年前

    您不能在一个关键帧内执行此操作,您可以使用两个关键帧和两个DIV元素,请参见下文。

    body {
      background: black;
    }
    
    @keyframes ani1 {
      0% { width: 0vmin; }
      100% { width: 20vmin; }
    }
    
    @keyframes ani2 {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    
    #div1 {
      top: 40vmin;
      left: 40vmin;
      width: 20vmin;
      height: 20vmin;
      position: absolute;
      animation: ani2 1s;
      animation-delay: 1s;
      animation-iteration-count: infinite;
    }
    
    #div2 {
      width: 20vmin;
      height: 20vmin;
      background: orange;
      animation: ani1 1s;
    }
    <div id="div1">
    <div id="div2">
    </div>
    </div>