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

如何在CSS中制作一个多步骤的简单动画?

  •  2
  • ScalaBoy  · 技术社区  · 6 年前

    我想在屏幕中央使用CSS实现以下动画:

    1. “你好”这段文字渐渐消失了
    2. 小圆圈出现并变为较大的矩形

    .step3 {
      height: 250px;
      width: 250px;
      margin: 0 auto;
      background-color: blue;
      animation-name: stretch;
      animation-duration: 10s;
      animation-timing-function: ease-out;
      animation-delay: 0;
      animation-direction: alternate;
      animation-iteration-count: 1;
      animation-fill-mode: none;
      animation-play-state: running;
    }
    
    @keyframes stretch {
      0% {
        transform: scale(.3);
        border-radius: 100%;
      }
      100% {
        transform: scale(1.5);
      }
    }
    <div class="step3"></div>

    animate.css 可用于淡入淡出文本:

    <div class="animated fadeIn 1">Hello</div>
    

    2 回复  |  直到 6 年前
        1
  •  3
  •   ashish singh    6 年前

    你需要了解这里的两处房产 1动画延迟

    动画延迟会导致动画在一段时间后开始,所以基本上可以在第一个动画结束后开始第二个动画

    动画填充模式可以在这里阅读 https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode

    div {
      position: absolute;
      
    }
    
    body {
      width: 100vw;
      height: 100vh;
      display: flex;
      align-items:center;
      justify-content: center;
    }
    
    .text{
      font-size: 3em;
      /*animation: showfade 4s linear 2s both;*/
      animation-name: showfade;
      animation-duration: 4s;
      animation-delay: .4s;
      animation-fill-mode: both;
      
    }
    
    .rect {
      
      background-color: #07f;
      width: 200px;
      height: 200px;
      /*animation: rect 2s  linear 6s both;*/
      animation-name: rect;
      animation-duration: 2s;
      animation-delay: 4.4s;
      animation-fill-mode: both;
    }
    
    
    @keyframes showfade {
      0%, 100% {
        opacity: 0;
        
      }
      
      50% {
        opacity: 100;
      }
    }
    
    @keyframes rect {
      from {
        transform: scale(0);
        border-radius: 100%;
      }
    }
    <div class="text">Hello</div>
    <div class="rect"></div>
        2
  •  1
  •   VXp Kadir BuÅ¡atlić    6 年前

    如果我理解正确,你可以这样做:

    body {
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 0;
      height: 100vh;
    }
    
    .step3 {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 250px;
      width: 250px;
      border-radius: 50%;
      animation: stretch 10s 3s ease-out forwards;
    }
    
    .step3 > span {
      opacity: 0;
      animation: fadeInOut 3s;
    }
    
    @keyframes stretch {
      0%, 100% {transform: scale(.3); background: blue}
      100% {
        transform: scale(1.5);
        border-radius: 0;
      }
    }
    
    @keyframes fadeInOut {
      50% {opacity: 1}
      100% {opacity: 0}
    }
    <div class="step3"><span>Hello</span></div>