Skip to content

Instantly share code, notes, and snippets.

@geminorum
Created November 16, 2013 08:08
Show Gist options
  • Save geminorum/7497412 to your computer and use it in GitHub Desktop.
Save geminorum/7497412 to your computer and use it in GitHub Desktop.
jQuery Typer, from : http://jsfiddle.net/ARTsinn/RED9H/4/ RTL compatible
$.fn.typer = function(text, options){
options = $.extend({}, {
char: ' ',
delay: 1000,
duration: 600,
endless: true
}, options || text);
text = $.isPlainObject(text) ? options.text : text;
var elem = $(this),
isTag = false,
c = 0;
(function typetext(i) {
var e = ({string:1, number:1}[typeof text] ? text : text[i]) + options.char,
char = e.substr(c++, 1);
if( char === '<' ){ isTag = true; }
if( char === '>' ){ isTag = false; }
elem.html(e.substr(0, c));
if(c <= e.length){
if( isTag ){
typetext(i);
} else {
setTimeout(typetext, options.duration/10, i);
}
} else {
c = 0;
i++;
if (i === text.length && !options.endless) {
return;
} else if (i === text.length) {
i = 0;
}
setTimeout(typetext, options.delay, i);
}
})(0);
};
$('#foo').typer(['<i>فارسی</i> <u>کلام</u> <b>شعر است</b>!', 'Foo bar.', 1337]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment