美文网首页
奇妙的CSS之Animation

奇妙的CSS之Animation

作者: 肉桂猿 | 来源:发表于2020-02-19 09:59 被阅读0次

    1. 进度条动画 (从左到右的元素延时显示动画)

    https://codepen.io/alphardex/pen/XWWWBmQ

    通过animation-delay来控制相同的元素(div)不同延时显示动画

    
    $colors: #7ef9ff, #89cff0, #4682b4, #0f52ba, #000080;
    
    @for $i from 1 through 5 {
    
          &:nth-child(#{$i}) {
    
            background: nth($colors, $i);
    
            &::before {
    
              animation-delay: $i * 0.2s;
    
            }
    
          }
    
        }
    

    2. 绚丽的文字效果(从最左边的文字开始动画)

    https://codepen.io/alphardex/pen/KKwvKGY
    用JS将句子或单词分割成字母,并给每个字母加上不同延时的动画。

    HTML

    <p class="landIn">Ano hi watashitachi mada shiranai no Fushigi no monogatari desu.</p>
    

    CSS

    .landIn {
      display: flex;
      flex-wrap: wrap;
      line-height: 1.8;
      color: white;
      font-family: Lora, serif;
      white-space: pre;
    
      span {
        animation: landIn 0.8s ease-out both;
      }
    }
    
    @keyframes landIn {
      from {
        opacity: 0;
        transform: translateY(-20%);
      }
    
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }
    

    JavaScript

    let letters = landInText.textContent.split("");
      landInText.textContent = "";
      letters.forEach((letter, i) => {
        let span = document.createElement("span");
        span.textContent = letter;
        span.style.animationDelay = `${i * 0.05}s`;
        landInText.append(span);
      });
    

    3. 绚丽的文字效果(从中间的文字开始动画)

    https://codepen.io/alphardex/pen/eYYMYXJ
    如果要从中间元素开始交错的话,就要给当前元素的延时各加上一个值,这个值就是中间元素的下标到当前元素的下标的距离(也就是下标之差的绝对值)与步长的乘积,即:delay + Math.abs(i - middle) * step,其中中间元素的下标middle = letters.filter(e => e !== "").length / 2

    HTML

    <div class="reveal">sword art online</div>
    

    CSS

    .reveal {
      position: relative;
      display: flex;
      color: #6ee1f5;
      font-size: 2em;
      font-family: Raleway, sans-serif;
      letter-spacing: 3px;
      text-transform: uppercase;
      white-space: pre;
    
      span {
        opacity: 0;
        transform: scale(0);
        animation: fadeIn 2.4s forwards;
      }
    
      &::before,
      &::after {
        position: absolute;
        content: "";
        top: 0;
        bottom: 0;
        width: 2px;
        height: 100%;
        background: white;
        opacity: 0;
        transform: scale(0);
      }
    
      &::before {
        left: 50%;
        animation: slideLeft 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
      }
    
      &::after {
        right: 50%;
        animation: slideRight 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
      }
    }
    
    @keyframes fadeIn {
      to {
        opacity: 1;
        transform: scale(1);
      }
    }
    
    @keyframes slideLeft {
      to {
        left: -6%;
        opacity: 1;
        transform: scale(0.9);
      }
    }
    
    @keyframes slideRight {
      to {
        right: -6%;
        opacity: 1;
        transform: scale(0.9);
      }
    }
    

    JavaScript

    let letters = revealText.textContent.split("");
    revealText.textContent = "";
    let middle = letters.filter(e => e !== " ").length / 2;
    letters.forEach((letter, i) => {
      let span = document.createElement("span");
      span.textContent = letter;
      span.style.animationDelay = `${delay + Math.abs(i - middle) * 0.1}s`;
      revealText.append(span);
    });
    

    4. 纯CSS模拟出下雪的效果

    https://codepen.io/alphardex/pen/dyPorwJ

    LESS

    body {
      height: 100vh;
      background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%);
      overflow: hidden;
      filter: drop-shadow(0 0 10px white);
    }
    
    @function random_range($min, $max) {
      $rand: random();
      $random_range: $min + floor($rand * (($max - $min) + 1));
      @return $random_range;
    }
    
    .snow {
      $total: 200;
      position: absolute;
      width: 10px;
      height: 10px;
      background: white;
      border-radius: 50%;
    
      @for $i from 1 through $total {
        $random-x: random(1000000) * 0.0001vw;
        $random-offset: random_range(-100000, 100000) * 0.0001vw;
        $random-x-end: $random-x + $random-offset;
        $random-x-end-yoyo: $random-x + ($random-offset / 2);
        $random-yoyo-time: random_range(30000, 80000) / 100000;
        $random-yoyo-y: $random-yoyo-time * 100vh;
        $random-scale: random(10000) * 0.0001;
        $fall-duration: random_range(10, 30) * 1s;
        $fall-delay: random(30) * -1s;
    
        &:nth-child(#{$i}) {
          opacity: random(10000) * 0.0001;
          transform: translate($random-x, -10px) scale($random-scale);
          animation: fall-#{$i} $fall-duration $fall-delay linear infinite;
        }
    
        @keyframes fall-#{$i} {
          #{percentage($random-yoyo-time)} {
            transform: translate($random-x-end, $random-yoyo-y) scale($random-scale);
          }
          
          to {
            transform: translate($random-x-end-yoyo, 100vh) scale($random-scale);
          }
        }
      }
    }
    
    
    

    相关文章

      网友评论

          本文标题:奇妙的CSS之Animation

          本文链接:https://www.haomeiwen.com/subject/prcnfhtx.html