如何创建跨多行文本的CSS输入动画? [英] How to create a CSS typing animation that spans multiple lines of text?

查看:81
本文介绍了如何创建跨多行文本的CSS输入动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当父容器的大小迫使文本跨越多行时,我试图使键入动画效果连续一行.

I am trying to get the typing animation effect to continue one line at a time when the size of parent container forces the text to span multiple lines.

/* 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>

推荐答案

我也一直在寻找该问题的答案.

I've been searching for the answer to this question as well.

我能找到的最接近的是这是多行打字机效果.但是,您必须手动设置每个p标签的宽度.我还没有找到一种动态设置每行宽度的方法.

The closest I could find was this - multiline typewriter effect. However, you have to manually set the width of each p tag. I've yet to find a way to dynamically set the width of each line.

除最后一行外,所有行均使用右边框显示打字机效果,只有最后一行具有闪烁动画,即打字机光标.

All lines except for the last line uses border-right to display the typewriter effect, only the last line has blink animation which is the typewriter cursor.

<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;
  }
}

这篇关于如何创建跨多行文本的CSS输入动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆