Created
November 19, 2019 17:36
-
-
Save UbaldoRosas/87a073673e72e6360ae1f4664e1a5837 to your computer and use it in GitHub Desktop.
Random number animator
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
(function($){ | |
$.fn.extend({ | |
numAnim: function(options) { | |
if ( ! this.length) | |
return false; | |
this.defaults = { | |
endAt: 2560, | |
numClass: 'autogen-num', | |
duration: 5, // seconds | |
interval: 90 // ms | |
}; | |
var settings = $.extend({}, this.defaults, options); | |
var $num = $('<span/>', { | |
'class': settings.numClass | |
}); | |
return this.each(function() { | |
var $this = $(this); | |
// Wrap each number in a tag. | |
var frag = document.createDocumentFragment(), | |
numLen = settings.endAt.toString().length; | |
for (x = 0; x < numLen; x++) { | |
var rand_num = Math.floor( Math.random() * 10 ); | |
frag.appendChild( $num.clone().text(rand_num)[0] ) | |
} | |
$this.empty().append(frag); | |
var get_next_num = function(num) { | |
++num; | |
if (num > 9) return 0; | |
return num; | |
}; | |
// Iterate each number. | |
$this.find('.' + settings.numClass).each(function() { | |
var $num = $(this), | |
num = parseInt( $num.text() ); | |
var interval = setInterval( function() { | |
num = get_next_num(num); | |
$num.text(num); | |
}, settings.interval); | |
setTimeout( function() { | |
clearInterval(interval); | |
}, settings.duration * 1000 - settings.interval); | |
}); | |
setTimeout( function() { | |
$this.text( settings.endAt.toString() ); | |
}, settings.duration * 1000); | |
}); | |
} | |
}); | |
})(jQuery); | |
$("#number").numAnim({ | |
endAt: 97855, | |
duration: 3 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment