因此,我创建了一个基本网站,并使用了w3schools的打字机功能:

https://www.w3schools.com/howto/howto_js_typewriter.asp

我的问题是我想在两行上显示文本,但我不确定如何做到这一点。任何帮助!

谢谢

var i = 0;
var txt = 'Lorem ipsum typing effect!'; /* The text */
var speed = 50; /* The speed/duration of the effect in milliseconds */

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<span id='demo'></span>

最佳答案

您可能想在那里获得一个<br/>

您可以执行以下操作:

var i = 0;
var txt = `Lorem ipsum dummy \n text blabla.`;
var speed = 50;

function typeWriter() {
  if (i < txt.length) {
    let text = txt.charAt(i);
    document.getElementById("demo").innerHTML += text=== "\n" ? "<br/>": text;
    i++;
    setTimeout(typeWriter, speed);
  }
}

typeWriter();
<span id='demo'></span>

关于javascript - 两行Javascript typewriter(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54858400/

10-13 01:08