Skip to content

Instantly share code, notes, and snippets.

@wallacemaxters
Created December 4, 2015 16:38
Show Gist options
  • Save wallacemaxters/b445d08c0035cecfeacb to your computer and use it in GitHub Desktop.
Save wallacemaxters/b445d08c0035cecfeacb to your computer and use it in GitHub Desktop.
Animated text in jquery
(function ( $ ) {
$.fn.animatedText = function (first_param, options) {
var $self = $(this);
$self.attr('in-animation-text', 'true');
if ($.isPlainObject(first_param)) {
options = first_param;
} else if (typeof(first_param) == 'string') {
text = first_param;
delete first_param;
} else if (first_param === false) {
$self.attr('in-animation-text', 'false');
$self.dequeue().stop().clearQueue();
return this;
}
options = options || {};
if (! $.isPlainObject(options)) {
throw new Error('O parâmetro deve ser um objecto');
}
options.delay = options.delay || 400;
var index = 0;
var animatedText = function() {
var $self = $(this);
var currentText = '';
$self.filter('[in-animation-text=true]').delay(options.delay).queue(function (next) {
var isValid = text.charAt(index++);
if (isValid !== '') {
if (typeof(options.prefix) === 'string') {
currentText = options.prefix;
}
currentText += text.substr(0, index)
$self.html(currentText);
next();
}
if (options.infinity === true && index === text.length) {
index = 0;
}
$.proxy(animatedText, this)();
});
}
$.proxy(animatedText, this)()
return this;
}
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment