admin管理员组

文章数量:1391975

How can I make text appear as if it is being typed one letter at a time with JavaScript and jQuery?

I tried hiding the content and then revealing one letter at a time with many setTimeout() calls, but that seems highly inefficient.

Anyone know of a better way to do this or a script or plugin that can simulate this effect?

I also would like a blinking cursor!

How can I make text appear as if it is being typed one letter at a time with JavaScript and jQuery?

I tried hiding the content and then revealing one letter at a time with many setTimeout() calls, but that seems highly inefficient.

Anyone know of a better way to do this or a script or plugin that can simulate this effect?

I also would like a blinking cursor!

Share Improve this question edited May 23, 2013 at 19:54 nalply 28.9k15 gold badges83 silver badges104 bronze badges asked May 23, 2013 at 19:07 Irfan MirIrfan Mir 2,1858 gold badges27 silver badges33 bronze badges 9
  • 3 What is inefficient about setTimeout()? – PinnyM Commented May 23, 2013 at 19:08
  • 1 github./chadselph/jquery-typewriter – adaam Commented May 23, 2013 at 19:09
  • You could progressively move an element that obscures the text to the right in increments. – user1726343 Commented May 23, 2013 at 19:09
  • 1 Here's an nice, tiny plugin to do it: stackoverflow./a/10872412/2287470 – Joe Commented May 23, 2013 at 19:09
  • @PinnyM That there are so many of them. one per each character. – Irfan Mir Commented May 23, 2013 at 19:11
 |  Show 4 more ments

2 Answers 2

Reset to default 4

jQuery Typewriter available here: https://github./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
});

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./ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<div id=test></div>

本文标签: javascriptMake text appear like it is being typed like with a typewriterStack Overflow