如何按顺序运行 CSS 文本动画。(一行一行的文字)

Emi*_*ily 4 html css

我有这个小提琴:https : //jsfiddle.net/8uwu59sL/目前它正在同时输入每一行,但我想让它一行接一行地输入它们,在两行之间有第二个长时间的延迟。

这是 HTML:

<p>First Line...</p>
<p>Second Line...</p>
<p>Third Line...</p>
<p>Fourth Line...</p>
<p>Fifth Line...</p>
<p>Sixth Line...</p>
<br>
<p>Final/Blinking Line<span>|</span></p> 
Run Code Online (Sandbox Code Playgroud)

这是 CSS:

body{
  background: #000;
  padding-top: 10px;
} 

p{
  color: lime; 
  font-family: "Courier";
  font-size: 20px;
  margin: 10px 0 0 10px;
  white-space: nowrap;
  overflow: hidden;
  width: 30em;
  animation: type 4s steps(60, end); 
}

p:nth-child(2){
  animation: type2 8s steps(60, end);
}

p a{
  color: lime;
  text-decoration: none;
}

span{
  animation: blink 1s infinite;
}

@keyframes type{ 
  from { width: 0; } 
} 

@keyframes type2{
  0%{width: 0;}
  50%{width: 0;}
  100%{ width: 100; } 
} 

@keyframes blink{
  to{opacity: .0;}
}

::selection{
  background: black;
}
Run Code Online (Sandbox Code Playgroud)

我想知道,我该怎么做?另外,是否可以让最后一行(“最后一行/闪烁行”)缓慢闪烁,并让“|” 也闪烁,但速度不同?在此先感谢您的帮助!

Ric*_*cky 5

纯 CSS 方法:

要实现您的第一个要求,您需要使用 css 选择器:nth-child()。如何使用它: element:nth-child(n) 其中 n 表示子元素在其父元素中的索引。

对于另一个,您需要将文本和“|”分开 每个<span>单独使用不同的动画持续时间(速度)单独定位它们。

编辑:

如果您可以使用 JQuery 并且计划随着时间的推移添加更多行,我建议使用DaniP 的答案,它的可扩展性更强,而不必担心为每一行添加新的 css 选择器。如果您想针对特定行来更改动画持续时间之类的内容,您可以只针对它们单独添加一个 id 并使用 css。

body {
  background: #000;
  padding-top: 10px;
}
p {
  color: lime;
  font-family: "Courier";
  font-size: 20px;
  margin: 10px 0 0 10px;
  white-space: nowrap;
  overflow: hidden;
  width: 0;
  opacity: 0;
  animation: type 4s steps(60, end) forwards;
  -webkit-user-select: none;
  user-select: none;
}
p:nth-child(2) {
  animation-delay: 1s;
}
p:nth-child(3) {
  animation-delay: 2s;
}
p:nth-child(4) {
  animation-delay: 3s;
}
p:nth-child(5) {
  animation-delay: 4s;
}
p:nth-child(6) {
  animation-delay: 5s;
  margin-bottom: 25px;
}
p:nth-child(7) {
  animation-delay: 6s;
}
p:nth-child(7) span:first-child {
  animation-duration: 0.8s;
}
span {
  animation: blink 1.8s infinite 8s;
}
p a {
  color: lime;
  text-decoration: none;
}
@keyframes type {
  0% {
    opacity: 1;
  }
  100% {
    width: 30em;
    opacity: 1;
  }
}
@keyframes blink {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
::selection {
  background: black;
}
Run Code Online (Sandbox Code Playgroud)
<p>First Line...</p>
<p>Second Line...</p>
<p>Third Line...</p>
<p>Fourth Line...</p>
<p>Fifth Line...</p>
<p>Sixth Line...</p>
<p><span>Final/Blinking Line</span>  <span>|</span>
</p>
Run Code Online (Sandbox Code Playgroud)