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

时间:2013-05-23 19:07:21

标签: javascript jquery

如何将文本显示为使用JavaScript和jQuery一次输入一个字母?

我尝试隐藏内容,然后一次显示一个字母,其中包含许多setTimeout()次来电,但这似乎非常低效。

任何人都知道更好的方法或者可以模拟这种效果的脚本或插件吗?

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

2 个答案:

答案 0 :(得分:4)

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
});

答案 1 :(得分:3)

这是我做的一个例子

(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>

相关问题