显示另一个<p>之后如何删除上一个<p>?

时间:2019-12-18 21:18:20

标签: javascript html

Javascript / Css3专家

我的编码可以按顺序显示文本,但是在显示另一个<p>时,我需要删除前面的<p>文本。

用简单的话 ...我想用即将出现的新文本替换旧显示的文本,并按原样显示最终文本。

这是代码:

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;
}
<p>Text first.</p>
<p>Text 2nd...</p>
<p>Text 3rd...</p>
<p>Text 4th...</p>
<p>Text 5th...</p>
<p>Text 6th...</p>
<p><span>Final/Blinking Line</span>  <span>|</span>
</p>

摘要::当您执行代码时。它依次显示<p>很好。但是<p>应该彼此替换,不要显示4行...仅最后一行<p>的空白行应显示在最后。

谢谢

1 个答案:

答案 0 :(得分:0)

以下是基于已发布代码的CSS版本,其中包含一些想法:

body {
  background: #000;
  color: white;
  padding-top: 10px;
}
.container {
    position: relative; /* to host absolute child elements */
}
.temporary {
    position: absolute;
    width: 0px;
}

p {
  color: lime;
  font-family: "Courier";
  font-size: 20px;
  margin: 10px 0 0 10px;
  white-space: nowrap;
  overflow: hidden;
  width: 0;
  animation: type 1s steps(60, end);
  -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;
}
p:nth-child(7) {
  animation-delay: 6s;
  animation-fill-mode: forwards;
}

span {
  animation: blink 1.8s infinite 8s;
}

p:nth-child(7) span:first-child {
  animation-duration: 0.8s;
}
p a {
  color: lime;
  text-decoration: none;
}
@keyframes type {
  0% {
  }
  30% {
    width: 10em;
  }
  31% {
    width: 30em;  /* provide reading time for longer lines */
  }
  100% {
    width: 30em;
  }
}
@keyframes blink {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
::selection {
  background: black;
}
<h3>Demo</h3>
<div class="container">
  <p class="temporary">Text first.</p>
  <p class="temporary">Text 2nd...</p>
  <p class="temporary">Text 3rd...</p>
  <p class="temporary">Text 4th... but this is a fairly long line as well.</p>
  <p class="temporary">Text 5th...</p>
  <p class="temporary">Text 6th...</p>
  <p class="temporary"><span>Final/Blinking Line</span>  <span>|</span></p>
</div>

更改:

  1. 段落使用绝对定位,并放置在相对定位的容器中,因此它们占据相同的屏幕区域。这是为了防止不可见的段落影响可见段落的垂直位移。

  2. 动画不影响段落不透明度。动画段落被指定为动画前后宽度为零。与overflow: hidden结合使用时,这会隐藏默认情况下未设置动画的段落,而无需使用opacitydisplay属性。

  3. 眨眼的段落是给定animation-fill-style的{​​{1}}的唯一段落,以防止动画结束时变回零宽度。

为避免同时显示多个段落,段落的forwards时间必须不少于单个段落的animation-delay时间。修改后的CSS规则将持续时间减少到1秒,以匹配段落延迟步骤。但是,为了使更长的线显示至少七分之一秒,在跳转到全宽之前,仅对宽度扩展的前30%进行动画处理。为了使动画保持简单,或多或少需要做出一些妥协,但时间和宽度始终可以根据要求进行更改。

闪烁

CRT文本终端闪烁的模拟可以包括淡出效果,以模拟屏幕荧光粉的persistence time。也许最简单的方法是为动画根据图形设计提供animation-duration上下倾斜的多个关键帧。

例如,该图形的闪烁速率为1hz,标称占空比为50%,磷光体快速衰减为200ms,随后衰减较慢,为300ms:

opacity
.screen {
   font-family: "lucida console", "courier new", monospace;
   color: #0b0;
   background-color: black;
   padding: 1em;
   border-radius: 1em;
   border: thick solid beige;
}

.fadeBlinkText {
   animation-name: fade-blink;
   animation-duration: 1s;
   animation-iteration-count: infinite;
   animation-delay: 2s;
}
.fadeBlinkCursor {
   animation-name: fade-blink;
   animation-duration: 1.5s;
   animation-iteration-count: infinite;
   animation-delay: 2s;
}

@keyframes fade-blink {
   0%    { opacity: 1;}
   20%   { opacity: 0.1;}
   49.9% { opacity: 0.05;}
   50%   { opacity: 1;}
   100%  { opacity: 1;}
}