Created
December 4, 2015 16:38
-
-
Save wallacemaxters/b445d08c0035cecfeacb to your computer and use it in GitHub Desktop.
Animated text in jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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