html:
"en">"UTF-8" />"viewport" content="width=device-width, initial-scale=1.0" />"stylesheet" href="style.css" />自动打字机 "text">css
* { box-sizing: border-box; } body { background-color: darksalmon; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; margin: 0; text-align: center; color: #fff; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }js:
const text=document.getElementById('text'); const prog="陈业贵&liwen"; let index=1; function writeText() { text.innerText=prog.slice(0,index); index++; if(index>prog.length) { index=1; } } setInterval(writeText,300);
js逻辑:
第一步:获取到h1标签
第二步:显示的文本是什么?
第三步:定时器的作用是多次调用writeText函数
第四步:用slice截图文字,从prog变量中截取下标0.。。。。到下标123456789(复制的截取)
第五步:当超出文字的数量时就重新截图(从下标0开始) 注意:slice的第二个参数是不计算到本身的哦-1
效果图: