Irfan Mir
Irfan Mir

Reputation: 2175

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

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!

Upvotes: 2

Views: 4476

Answers (2)

dt192
dt192

Reputation: 1013

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>

Upvotes: 3

adaam
adaam

Reputation: 3706

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

Upvotes: 4

Related Questions