-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjquery.animateNumber.js
153 lines (138 loc) · 5.58 KB
/
jquery.animateNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* jquery.animateNumber.js - jquery number animation plugin
* Copyright (C) 2013, Robert Kajic ([email protected])
* http://kajic.com
*
* Used on elements that have a number as content (integer or float)
* to animate the number to a new value over a short period of time.
*
* Licensed under the MIT License.
*
* Date: 2013-01-08
* Version: 0.1
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function(jQuery, undefined) {
var defaults = {
duration : 5000,
easing: "swing",
animateOpacity: true,
intStepDecimals: 0,
intEndDecimals: 0,
floatStepDecimals: 4,
floatEndDecimals: 1,
format: "default",
currencyIndicator: "$",
currencyGroupSeparator: (1000).toLocaleString().charAt(1),
currencyDecimalSeparator: (1.5).toLocaleString().charAt(1),
callback: function() {},
stepCallback: function() {}
};
function formatNumber(number, options, decimals) {
if (typeof options.format === "function") {
return options.format(number, options, decimals);
} else if (options.format == "currency") {
return formatCurrency(number, options);
} else {
return round(number, decimals);
}
}
function round(number, decimals) {
return Math.round(number*Math.pow(10,decimals))/Math.pow(10,decimals);
}
function formatCurrency(number, options) {
if (isNaN(number)) { return; }
var integer = Math.floor(number);
var decimal = (number - integer).toFixed(options.floatEndDecimals).split('.')[1];
var integerPart = integer.toLocaleString();
var decimalPart = '';
if (parseInt(options.floatEndDecimals) > 0) {
decimalPart += options.currencyDecimalSeparator + decimal;
}
// This check is necessary because IE renders (25).toLocaleString() as 25.00
// while Chrome, Firefox and others return it as 25
if (integerPart.indexOf(options.currencyDecimalSeparator) >= 0) {
integerPart = integerPart.split('.')[0];
}
return options.currencyIndicator + integerPart + decimalPart;
}
function isInt(number) {
return /^-?[\d]+$/.test(number);
}
$.fn.animateNumber = function (value, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
}
options = $.extend({}, defaults, options);
return this.each(function () {
var container = $(this);
var initialValue;
if (container.data("numeric-value")) {
initialValue = container.data("numeric-value");
} else if (options.format === "currency") {
initialValue = container.text().replace(options.currencyIndicator, "").replace(options.currencyGroupSeparator, "");
} else {
initialValue = parseFloat(container.text(), 10);
}
if (round(value, options.floatEndDecimals) == round(initialValue, options.floatEndDecimals)) {
return;
}
var type = container.data("type") || (isInt($(this).text()) ? "int" : "float"),
stepDecimals, endDecimals,
defaultStepDecimals, defaultEndDecimals;
if (type == "int") {
defaultStepDecimals = options.intStepDecimals;
defaultEndDecimals = options.intEndDecimals;
} else {
defaultStepDecimals = options.floatStepDecimals;
defaultEndDecimals = options.floatEndDecimals;
}
stepDecimals = container.data("stepDecimals") || defaultStepDecimals;
endDecimals = container.data("endDecimals") || defaultEndDecimals;
// animate opacity
if (options.animateOpacity) {
container.animate({opacity: 0.2}, {
duration: options.duration/2,
easing: options.easing,
complete: function() {
container.animate({opacity: 1}, {
duration: options.duration/2,
easing: options.easing
});
}
});
}
// animate number
$({number: initialValue}).animate({number: value}, {
duration: options.duration,
easing: options.easing,
step: function() {
container.text(formatNumber(this.number, options, stepDecimals));
if(typeof options.stepCallback === 'function') {
options.stepCallback(this, container, this.number);
}
},
complete: function() {
container.data("numeric-value", this.number);
container.text(formatNumber(this.number, options, endDecimals));
if (typeof options.callback === "function") {
options.callback.call(this, container, this.number);
}
}
});
});
};
}));