目前只有示例2得到了完全的解释。示例3与问题完全相同,只是做了一些微小的更改。
示例1 — 为背景图片和渐变重新设计
HTML
首先,我们可以清理HTML。这是一个单独的段落,所以让我们将它放在一个段落元素中:
<p class="typewriter">
A well defined plan will identify problems,
address challenges, and help restore confidence.
</p>
其次,我们需要逐行显示文本,因此我们将每行文本包装在嵌套的元素中,并使用换行符手动断开每行:
<p class="typewriter">
<span class="slide">
<span class="inner-slide">A well defined plan will identify problems,
</span>
</span><br>
<span class="slide">
<span class="inner-slide">address challenges, and help restore confidence. </span>
</span>
</p>
完整示例1
当前限制:我们必须为left
设置一个固定的像素宽度。
.typewriter {
position: relative;
height: 500px;
margin: 0 auto;
width: 310px;
overflow: hidden;
}
.typewriter .slide,
.inner-slide {
display: inline-block;
overflow: hidden;
height: 1.1em;
}
.typewriter .slide {
position: relative;
animation: typing 2s steps(30, end) forwards, blink-caret .75s step-end infinite;
left: -310px;
border-right: .15em solid transparent;
}
.typewriter .slide:nth-of-type(1) {
animation: typing 2s steps(30, end) forwards, blink-caret .75s step-end 2.6;
}
.inner-slide {
position: relative;
animation: typing2 2s steps(30, end) forwards;
white-space: nowrap;
left: 310px;
}
.typewriter .slide:nth-of-type(2),
.typewriter .slide:nth-of-type(2) .inner-slide {
animation-delay: 2s;
}
@keyframes typing {
from {
left: -310px;
}
to {
left: 0;
}
}
@keyframes typing2 {
from {
left: 310px;
}
to {
left: 0;
}
}
@keyframes blink-caret {
0,
100% {
border-color: transparent
}
50% {
border-color: #00aeff
}
}
body {
background: linear-gradient(to bottom right, #CCC 0, #F00 100%) no-repeat;
}
<p class="typewriter">
<span class="slide">
<span class="inner-slide">A well defined plan will identify problems,
</span>
</span><br>
<span class="slide">
<span class="inner-slide">address challenges, and help restore confidence.</span>
</span>
</p>
示例二 — 原版。适用于纯色背景
HTML
首先,我们可以整理 HTML。这是一个段落,所以让我们使用一个段落元素来包裹它:
<p class="typewriter">
A well defined plan will identify problems,
address challenges, and help restore confidence.
</p>
其次,我们需要逐行逐个显示文本内容,因此我们会将每一行用 span 元素包裹,并在需要处手动添加换行符:
<p class="typewriter">
<span>A well defined plan will identify problems,</span><br>
<span> address challenges, and help restore confidence.</span>
</p>
CSS
现在我们需要一个元素来覆盖文本并作为动画光标。我们可以使用一个伪元素,它将从100%的宽度开始,并具有左边框,如下所示:
.typewriter > span::before {
content: '';
border-left: .15em solid #00aeff;
position: absolute;
background: white;
height: 1.1em;
right: -5px;
width: 100%;
}
- 高度应足以覆盖包括基线以下的所有文本。
- 正确的负值会将其拉出其父级,因此光标不会显示在第一行,这要归功于父级上的
overflow-hidden
。
- 它从100%宽度开始,然后动画变为0。
- 它相对于具有相对位置的span进行绝对定位。
为了保持光标在最后一行,我们需要给它一个0的右边值:
.typewriter > span:last-of-type::before {
right: 0;
}
现在它将不再被拉出父元素。
第二行需要延迟与动画运行时间相同的时间:
.typewriter > span:nth-of-type(2)::before {
animation-delay: 2s;
}
由于我们希望段落的宽度由文本的宽度决定,而跨度接受宽度,因此我们需要将它们设置为内联块:
.typewriter,
.typewriter > span {
display: inline-block;
}
最后,我们将打字动画反转,从100%到0:
@keyframes typing {
from {
width: 100%
}
to {
width: 0
}
}
完整示例2
.typewriter,
.typewriter > span {
display: inline-block;
}
.typewriter > span {
position: relative;
overflow: hidden;
padding-right: 4px;
}
.typewriter > span::before {
content: '';
position: absolute;
border-left: .15em solid #00aeff;
background: white;
height: 1.1em;
right: -5px;
width: 100%;
animation: blink-caret .75s step-end infinite, typing 2s steps(30, end) forwards;
}
.typewriter > span:nth-of-type(2)::before {
animation-delay: 2s;
}
.typewriter > span:last-of-type::before {
right: 0;
}
@keyframes typing {
from {
width: 100%
}
to {
width: 0
}
}
@keyframes blink-caret {
from,
to {
border-color: #00aeff
}
50% {
border-color: transparent
}
}
<p class="typewriter">
<span>A well defined plan will identify problems,</span><br>
<span> address challenges, and help restore confidence.</span>
</p>
示例3 —— 使用问题中的示例
根据第一条线的插入符号适当更改迭代计数。在此示例中,该值为4.1。此动画将迭代4.1次,然后停止:
animation: blink-caret .75s step-end 4.1
将创建插入符号的边框更改为透明:
border-right: .15em solid transparent
并且动画被翻转了:
@keyframes blink-caret {
0,
100% {
border-color: transparent
}
50% {
border-color: #00aeff
}
}
现在停止状态是透明的,并且第一行会在第一行消失。
完整示例3
body {
width: 330px;
}
.typewriter1 p {
overflow: hidden;
border-right: .15em solid transparent;
white-space: nowrap;
margin: 0 auto;
letter-spacing: 0;
padding-left: 10px;
animation: typing 3.5s steps(40, end), blink-caret .75s step-end 4.1;
}
.typewriter2 p {
overflow: hidden;
border-right: .15em solid transparent;
white-space: nowrap;
margin: 0 auto;
letter-spacing: 0;
padding-left: 10px;
opacity: 0;
animation: typing 3.5s steps(40, end), blink-caret .75s step-end infinite, slidein 1s ease 3.5s forwards;
animation-delay: 3.5s;
}
@keyframes typing {
from {
width: 0
}
to {
width: 100%
}
}
@keyframes slidein {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes blink-caret {
0,
100% {
border-color: transparent
}
50% {
border-color: #00aeff
}
}
<div class="typewriter1">
<p>A well defined plan will identify problems,</p>
</div>
<div class="typewriter2">
<p> address challenges, and help restore confidence.</p>
</div>