2

如何使文本看起来好像是用 JavaScript 和 jQuery 一次输入一个字母?

我尝试隐藏内容,然后通过多次调用一次显示一个字母setTimeout(),但这似乎效率很低。

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

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

4

2 回答 2

4

jQuery Typewriter available here: https://github.com/chadselph/jquery-typewriter

Here is an example of how it works:

  $('.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
});
于 2013-05-23T19:10:13.947 回答
3

here is an example I made

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

于 2013-05-23T19:09:37.040 回答