如何创建跨越多行文本的CSS类型动画?

时间:2017-09-17 18:13:48

标签: css css3 css-animations

我试图让输入动画效果在父容器的大小强制文本跨越多行时一次继续一行。

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

1 个答案:

答案 0 :(得分:0)

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

我能找到的最近的是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;
  }
}