You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.7 KiB
39 lines
1.7 KiB
/***********
|
|
Animates element's number to new number with commas
|
|
Parameters:
|
|
stop (number): number to stop on
|
|
commas (boolean): turn commas on/off (default is true)
|
|
duration (number): how long in ms (default is 1000)
|
|
ease (string): type of easing (default is "swing", others are avaiable from jQuery's easing plugin
|
|
Examples:
|
|
$("#div").animateNumbers(1234, false, 500, "linear"); // half second linear without commas
|
|
$("#div").animateNumbers(1234, true, 2000); // two second swing with commas
|
|
$("#div").animateNumbers(4321); // one second swing with commas
|
|
This fully expects an element containing an integer
|
|
If the number is within copy then separate it with a span and target the span
|
|
Inserts and accounts for commas during animation by default
|
|
***********/
|
|
|
|
(function($) {
|
|
$.fn.animateNumbers = function(stop, commas, duration, ease) {
|
|
return this.each(function() {
|
|
var $this = $(this);
|
|
var start = parseInt($this.text().replace(/,/g, ""));
|
|
commas = (commas === undefined) ? true : commas;
|
|
$({value: start}).animate({value: stop}, {
|
|
duration: duration == undefined ? 1000 : duration,
|
|
easing: ease == undefined ? "swing" : ease,
|
|
step: function() {
|
|
$this.text(Math.floor(this.value));
|
|
if (commas) { $this.text($this.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")); }
|
|
},
|
|
complete: function() {
|
|
if (parseInt($this.text()) !== stop) {
|
|
$this.text(stop);
|
|
if (commas) { $this.text($this.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")); }
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
})(jQuery); |