js实现打字效果

代码实现

HTML

<div class="main"></div>

JS

let str = {
    msg: "",
    len: function () {
       return this.msg.length;
    },
    index: 0,
    speed: 200, //控制打字时间(ms)
    type: function () {
       let t = this;
       document.querySelector(".main").innerHTML = t.msg.substring(0,t.index);
       if (t.index == t.len()) {
            t.index = 0;
            clearTimeout(timer);
          } else {
             t.index++;
             timer = setTimeout(function () {
                t.type();
              }, this.speed);
            }
         },
     };
   let msg = "输入你想说的任何语言";
   str.msg = msg;
   str.type();

 

posted @ 2021-08-11 15:42  eternityAsr  阅读(136)  评论(0编辑  收藏  举报