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

是否可以用步骤阻止关键帧动画?

  •  0
  • Borja  · 技术社区  · 8 年前

    200px 属于 width

    在酒吧里,我想制作一个 div 只有一次 ,增加宽度和背景色。

    @keyframes first
    {
      0%   {background-color: green; width: 0%}
      33%  {background-color: yellow; width: 33%}
      66%  {background-color: orange; width: 66%}
      100% {background-color: red; width: 100%}
    }
    

    我想把第一个栏增加到150像素 150px 也可以阻止背景色动画)。在CSS或jQuery中这可能吗?

    在这种情况下,就像在步骤中停止动画一样 75%

    https://jsfiddle.net/gefz5wtL/3/

    2 回复  |  直到 8 年前
        1
  •  1
  •   TylerH Ash Burlaczenko    8 年前

    一个想法是通过添加/删除类来使用JS运行和暂停动画:

    $('#first').addClass('first-start');
    setTimeout(function() {
      $('#first').addClass('first-pause');
    }, 1500);
    #page {
      margin-top: 50px;
      width: 300px;
      height: 200px;
      background-color: #000;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }
    
    #box-first,
    #box-second {
      width: 200px;
      height: 50px;
      background-color: #fff;
      border-radius: 8px;
      margin-top: 10px;
      margin-bottom: 10px;
    }
    
    @keyframes first {
      0% {
        background-color: green;
        width: 0%
      }
      33% {
        background-color: yellow;
        width: 33%
      }
      66% {
        background-color: orange;
        width: 66%
      }
      100% {
        background-color: red;
        width: 100%
      }
    }
    
    @keyframes second {
      0% {
        background-color: green;
        width: 0%
      }
      33% {
        background-color: yellow;
        width: 33%
      }
      66% {
        background-color: orange;
        width: 66%
      }
      100% {
        background-color: red;
        width: 100%
      }
    }
    
    #first {
      width: 100%;
      height: 100%;
      border-radius: 8px;
      background-color: #fff;
    }
    
    .first-start {
      animation: first 2s infinite;
    }
    
    .first-pause {
      animation-play-state: paused;
    }
    
    #second {
      width: 100%;
      height: 100%;
      border-radius: 8px;
      background-color: #fff;
      animation: second 2s infinite;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="page">
      <div id="box-first">
        <div id="first">
        </div>
      </div>
    
      <div id="box-second">
        <div id="second">
        </div>
      </div>
    </div>
        2
  •  -2
  •   Mahesh Langote    8 年前

    它将如下所示:

    #page {
      margin-top: 50px;
      width: 300px;
      height: 200px;
      background-color: #000;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }
    
    #box-first, #box-second {
       width: 200px;
       height: 50px;
       background-color: #fff;
       border-radius: 8px;
       margin-top: 10px;
       margin-bottom: 10px;
      }
    
        @keyframes first
        {
          0%   {background-color: green; width: 0%}
          33%  {background-color: yellow; width: 33%}
          66%  {background-color: orange; width: 66%}
          100% {background-color: red; width: 75%}
        }
        
     
        @keyframes second
        {
          0%   {background-color: green; width: 0%}
          33%  {background-color: yellow; width: 33%}
          66%  {background-color: orange; width: 66%}
          100% {background-color: red; width: 100%}
        }   
        #first{
          width: 100%;
          height: 100%;
          border-radius: 8px;
          background-color: #fff;
          animation: first 2s 1 forwards;
        }
        
        #second {
          width: 100%;
          height: 100%;
          border-radius: 8px;
          background-color: #fff;
          animation: second 2s infinite;  
        }
        
    <div id="page">
        <div id="box-first">
           <div id="first">       
           </div>       
        </div>
    
        <div id="box-second">
           <div id="second">       
           </div>
        </div>
    </div>