Last active
August 29, 2015 14:06
-
-
Save mattbell87/aa29c353b76a92e8f2f2 to your computer and use it in GitHub Desktop.
jQuery plugin for typing animation - simulates typing the contained text
This file contains 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
//Type animation | |
$.fn.typeout = function(duration, oncomplete) | |
{ | |
$(this).each(function() | |
{ | |
//Grab the text | |
var text = $(this).text(); | |
$(this).data("percent", 0); | |
//Create the animation | |
$(this).animate({percent: 100}, | |
{ | |
duration: duration, | |
step: function(number) | |
{ | |
//Calculate and update text | |
var numletters = Math.ceil(text.length * (number * 0.01)); | |
var newtext = text.substr(0, numletters); | |
$(this).html(newtext + "|"); | |
}, | |
complete: function() | |
{ | |
//Remove the fake cursor and call oncomplete | |
$(this).html(text); | |
if (typeof oncomplete == "function") | |
oncomplete(); | |
} | |
}); | |
}); | |
return this; | |
}; |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Fade test</title> | |
<script src="jquery.js"></script> | |
<script src="typeout.js"></script> | |
<script src="usage.js"></script> | |
</head> | |
<body> | |
<div class="typeme"> | |
The quick brown fox jumps over the lazy dog. | |
</div> | |
</body> | |
</html> |
This file contains 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
$(document).ready(function() | |
{ | |
$('.typeme').typeout(10000); //ten seconds to complete typeout | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment