【问题标题】:How to create a CSS typing animation that spans multiple lines of text?如何创建跨越多行文本的 CSS 打字动画?
【发布时间】:2019-01-22 20:13:36
【问题描述】:

当父容器的大小迫使文本跨越多行时,我试图让打字动画效果一次继续一行。

/* The typing effect */
@keyframes typing {
  from {
    width: 0;
  }
  to {
    width: 100%;
  }
}

/* The typewriter cursor effect */
@keyframes blink-caret {
  from,
  to {
    border-color: transparent;
  }
  50% {
    border-color: green;
  }
}

.animated-text {
  font: bold 1.45em monospace;
  color: black;
  border-right: 0.6em solid;
  overflow: hidden; /* Ensures the content is not revealed until the animation */
  margin: 0 auto; /* Gives that scrolling effect as the typing happens */
  animation: typing 3.5s steps(40, end), blink-caret 0.9s step-end infinite;
}

.container {
  border: 10px solid;
  position: absolute;
  width: 25%;
  height: 32%;
  left: 35%
}
  <div class='container'>
        <h1 class='animated-text'>The typing effect should continue line by line at a time when the text needs to wrap</h1>
      </div>

【问题讨论】:

    标签: css css-animations


    【解决方案1】:

    我也一直在寻找这个问题的答案。

    我能找到的最接近的是this - multiline typewriter effect。但是,您必须手动设置每个 p 标签的宽度。我还没有找到动态设置每行宽度的方法。

    除最后一行外的所有行都使用border-right来显示打字机效果,只有最后一行有闪烁动画,即打字机光标。

    <div class="css-typing">
      <p>
        Hi I'm Jenssen Lee! Looking to start my career as a Front-End Developer in Singapore.
      </p>
      <p>
        I have experience with HTML, SASS, Bootstrap, JavaScript, jQuery, React, Node.js, Express.
      </p>
      <p>
        This site was designed and built by me - the code is available on Github.
      </p>
    </div>
    
    .css-typing p {
      border-right: .15em solid orange;
      font-family: "Courier";
      font-size: 14px;
      white-space: nowrap;
      overflow: hidden;
    }
    .css-typing p:nth-child(1) {
      width: 780px; /* manually set width */
      -webkit-animation: type 2s steps(40, end);
      animation: type 2s steps(40, end);
      -webkit-animation-fill-mode: forwards;
      animation-fill-mode: forwards;
    }
    
    .css-typing p:nth-child(2) {
      width: 780px; /* manually set width */
      opacity: 0;
      -webkit-animation: type2 2s steps(40, end);
      animation: type2 2s steps(40, end);
      -webkit-animation-delay: 2s;
      animation-delay: 2s;
      -webkit-animation-fill-mode: forwards;
      animation-fill-mode: forwards;
    }
    
    .css-typing p:nth-child(3) {
      width: 620px; /* manually set width */
      opacity: 0;
      -webkit-animation: type3 5s steps(20, end), blink .5s step-end infinite alternate;
      animation: type3 2s steps(20, end), blink .5s step-end infinite alternate;
      -webkit-animation-delay: 4s;
      animation-delay: 4s;
      -webkit-animation-fill-mode: forwards;
      animation-fill-mode: forwards;
    }
    
    @keyframes type {
      0% {
        width: 0;
      }
      99.9% {
        border-right: .15em solid orange;
      }
      100% {
        border: none;
      }
    }
    
    @-webkit-keyframes type {
      0% {
        width: 0;
      }
      99.9% {
        border-right: .15em solid orange;
      }
      100% {
        border: none;
      }
    }
    
    @keyframes type2 {
      0% {
        width: 0;
      }
      1% {
        opacity: 1;
      }
      99.9% {
        border-right: .15em solid orange;
      }
      100% {
        opacity: 1;
        border: none;
      }
    }
    
    @-webkit-keyframes type2 {
      0% {
        width: 0;
      }
      1% {
        opacity: 1;
      }
      99.9% {
        border-right: .15em solid orange;
      }
      100% {
        opacity: 1;
        border: none;
      }
    }
    
    @keyframes type3 {
      0% {
        width: 0;
      }
      1% {
        opacity: 1;
      }
      100% {
        opacity: 1;
      }
    }
    
    @-webkit-keyframes type3 {
      0% {
        width: 0;
      }
      1% {
        opacity: 1;
      }
      100% {
        opacity: 1;
      }
    }
    
    @keyframes blink {
      50% {
        border-color: transparent;
      }
    }
    @-webkit-keyframes blink {
      50% {
        border-color: tranparent;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-18
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多