我采用了一个有效的 CSS 动画(文本键入效果)并将其嵌套在父 div 中。这个小编辑完全打破了它。帮我理解为什么?



我正在为一个朋友的生日开发一个小型游戏网站,我正试图通过以下网站上提供的示例,使用CSS动画创建一个打字文本效果。基本上,它会逐步更改文本元素的宽度,从0到100%,以显示文本。它还使用眨眼动画来创建光标。这个例子甚至提供了一个可以玩的代码笔。

我能够通过一些我想要的修改成功地重现效果。然而,我也想把整个东西放在div中,但当我尝试这样做时,Irun遇到了一个问题。文本滚动效果不是到文本的末尾,文本保持居中,而是动画到父元素的全宽,文本一直向左推!我不知道为什么会发生这种事。我试着找出可能导致这个问题的原因,我唯一能学到的是,当动画被禁用时,文本会正确显示(居中(。

如果你看看这个电笔,你就能看到我想要的效果。这是代码:

body {
background: #333;
padding-top: 5em;
display: flex;
justify-content: center;
}
.typewriter h1 {
color: #fff;
font-family: monospace;
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation: 
typing 3.5s steps(30, end),
blink-caret .5s step-end infinite;
}
/* 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: orange }
}
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>

这个代码笔有我对这个例子的小编辑。

body {
background: #333;
padding-top: 5em;
display: flex;
justify-content: center;
}
.main {
background-color: blue;
height: 300px;
width: 75%;
}
.typewriter h1 {
color: #fff;
font-family: monospace;
overflow: hidden; /* Ensures the content is not revealed until the animation */
border-right: .15em solid orange; /* The typwriter cursor */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation: 
typing 3.5s steps(30, end),
blink-caret .5s step-end infinite;
}
/* 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: orange }
}
<div class="main">  
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
</div>

我在这里不知所措。有人能帮助我了解这么小的编辑可能会产生什么问题,以及我如何解决它吗?

唯一的区别是父容器上的样式,第一种情况下是body,因此第二种情况下应该是.main

body {
background: #333;
padding-top: 5em;
}
.main {
background-color: blue;
height: 300px;
width: 75%;
display: flex;
justify-content: center;
}
.typewriter h1 {
color: #fff;
font-family: monospace;
overflow: hidden;
/* Ensures the content is not revealed until the animation */
border-right: .15em solid orange;
/* The typwriter cursor */
white-space: nowrap;
/* Keeps the content on a single line */
margin: 0 auto;
/* Gives that scrolling effect as the typing happens */
letter-spacing: .15em;
/* Adjust as needed */
animation: typing 3.5s steps(30, end), blink-caret .5s step-end infinite;
}

/* 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: orange
}
}
<div class="main">
<div class="typewriter">
<h1>The cat and the hat.</h1>
</div>
</div>

最新更新