使文本看起来像正在用打字机打字一样

Make text appear like it is being typed like with a typewriter

本文关键字:一样 打字机 看起来 文本      更新时间:2023-09-26

我怎样才能使文本出现,如果它是键入一个字母在一个时间与JavaScript和jQuery?

我尝试隐藏内容,然后在许多setTimeout()调用中每次显示一个字母,但这似乎效率非常低。

有谁知道更好的方法来做到这一点或脚本或插件,可以模拟这种效果?

我也想要一个闪烁的光标!

jQuery打字机可在此下载:https://github.com/chadselph/jquery-typewriter

下面是它如何工作的一个例子:

  $('.someselector').typewrite({
    'delay': 100, //time in ms between each letter
    'extra_char': '', //"cursor" character to append after each display
    'trim': true, // Trim the string to type (Default: false, does not trim)
    'callback': null // if exists, called after all effects have finished
});

这是我做的一个例子

(function($){$.fn.typeWrite=function(s){
        var o={content:$(this).text(),delay:50,t:this,i:0};
        if(s){$.extend(o,s);}o.t.text('');
        var i=setInterval(function() {
            o.t.text(o.t.text()+o.content.charAt(o.i++));    
            if(o.i==o.content.length){clearInterval(i);}}
        ,o.delay);
      return o.t;  
    };})(jQuery);
    
    $('#test').typeWrite({content:'The stuff to type'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id=test></div>