我正在做一个基于文本的游戏,在部分文本中,它没有创建一个新的行。
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style media="screen">
body {
background-color: black;
}
p {
color: white;
font-family: "Courier";
font-size: 20px;
margin: 10px 0 0 10px;
white-space: nowrap;
overflow: hidden;
width: 30em;
animation: type 4s steps(60, end);
}
@keyframes type{
from { width: 0; }
}
</style>
</head>
<body>
<p class="one">Welcome, unsuspectful human. \nWhat is your name?</p>
</body>
</html>但结果是:
Welcome, unsuspectful human. \nWhat is your name?由于某些原因,\n没有额外的行。我会有单独的<p>,但文本同时出现?我真的不知道如何解释它。
发布于 2022-01-14 15:48:49
使用<br />标记代替
<body>
<p class="one">Welcome, unsuspectful human. <br /> What is your name?</p>
</body>发布于 2022-01-14 18:38:51
如果每一行都是<p>,那么可以使用Javascript在每一行之后添加动画类。
HTML
<p>Welcome, unsuspectful human.</p>
<p>What is your name?</p>CSS
body {
background-color: black;
}
p {
display: none;
}
.write {
display: block;
color: white;
font-family: "Courier";
font-size: 20px;
margin: 10px 0 0 10px;
white-space: nowrap;
overflow: hidden;
width: 30em;
animation: type 4s steps(60, end);
}
@keyframes type {
from { width: 0; }
}JS
writeLine(0);
function writeLine(lineNumber) {
let elements = document.querySelectorAll("p");
elements[lineNumber].classList.add("write");
if(lineNumber < elements.length-1) {
// I didn't use a forEach because then the timeouts would all be set together
setTimeout(function(){writeLine(lineNumber+1)}, 3500);
}
}https://stackoverflow.com/questions/70713105
复制相似问题