countUp.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. (function(root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(factory);
  4. } else if (typeof exports === 'object') {
  5. module.exports = factory(require, exports, module);
  6. } else {
  7. root.CountUp = factory();
  8. }
  9. }(this, function(require, exports, module) {
  10. /*
  11. countUp.js
  12. by @inorganik
  13. */
  14. // target = id of html element or var of previously selected html element where counting occurs
  15. // startVal = the value you want to begin at
  16. // endVal = the value you want to arrive at
  17. // decimals = number of decimal places, default 0
  18. // duration = duration of animation in seconds, default 2
  19. // options = optional object of options (see below)
  20. var CountUp = function(target, startVal, endVal, decimals, duration, options) {
  21. // make sure requestAnimationFrame and cancelAnimationFrame are defined
  22. // polyfill for browsers without native support
  23. // by Opera engineer Erik Möller
  24. var lastTime = 0;
  25. var vendors = ['webkit', 'moz', 'ms', 'o'];
  26. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  27. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  28. window.cancelAnimationFrame =
  29. window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
  30. }
  31. if (!window.requestAnimationFrame) {
  32. window.requestAnimationFrame = function(callback, element) {
  33. var currTime = new Date().getTime();
  34. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  35. var id = window.setTimeout(function() { callback(currTime + timeToCall); },
  36. timeToCall);
  37. lastTime = currTime + timeToCall;
  38. return id;
  39. };
  40. }
  41. if (!window.cancelAnimationFrame) {
  42. window.cancelAnimationFrame = function(id) {
  43. clearTimeout(id);
  44. };
  45. }
  46. var self = this;
  47. self.version = function () { return '1.8.3'; };
  48. function formatNumber(num) {
  49. num = num.toFixed(self.decimals);
  50. num += '';
  51. var x, x1, x2, rgx;
  52. x = num.split('.');
  53. x1 = x[0];
  54. x2 = x.length > 1 ? self.options.decimal + x[1] : '';
  55. rgx = /(\d+)(\d{3})/;
  56. if (self.options.useGrouping) {
  57. while (rgx.test(x1)) {
  58. x1 = x1.replace(rgx, '$1' + self.options.separator + '$2');
  59. }
  60. }
  61. return self.options.prefix + x1 + x2 + self.options.suffix;
  62. }
  63. // Robert Penner's easeOutExpo
  64. function easeOutExpo(t, b, c, d) {
  65. return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  66. }
  67. function ensureNumber(n) {
  68. return (typeof n === 'number' && !isNaN(n));
  69. }
  70. // default options
  71. self.options = {
  72. useEasing: true, // toggle easing
  73. useGrouping: true, // 1,000,000 vs 1000000
  74. separator: ',', // character to use as a separator
  75. decimal: '.', // character to use as a decimal
  76. easingFn: easeOutExpo, // optional custom easing function, default is Robert Penner's easeOutExpo
  77. formattingFn: formatNumber, // optional custom formatting function, default is formatNumber above
  78. prefix: '', // optional text before the result
  79. suffix: '' // optional text after the result
  80. };
  81. // extend default options with passed options object
  82. if (options && typeof options === 'object') {
  83. for (var key in self.options) {
  84. if (options.hasOwnProperty(key) && options[key]) {
  85. self.options[key] = options[key];
  86. }
  87. }
  88. }
  89. if (self.options.separator === '') self.options.useGrouping = false;
  90. self.initialize = function() {
  91. if (self.initialized) return true;
  92. self.d = (typeof target === 'string') ? document.getElementById(target) : target;
  93. if (!self.d) {
  94. console.error('[CountUp] target is null or undefined', self.d);
  95. return false;
  96. }
  97. self.startVal = Number(startVal);
  98. self.endVal = Number(endVal);
  99. // error checks
  100. if (ensureNumber(self.startVal) && ensureNumber(self.endVal)) {
  101. self.decimals = Math.max(0, decimals || 0);
  102. self.dec = Math.pow(10, self.decimals);
  103. self.duration = Number(duration) * 1000 || 2000;
  104. self.countDown = (self.startVal > self.endVal);
  105. self.frameVal = self.startVal;
  106. self.initialized = true;
  107. return true;
  108. }
  109. else {
  110. console.error('[CountUp] startVal or endVal is not a number', self.startVal, self.endVal);
  111. return false;
  112. }
  113. };
  114. // Print value to target
  115. self.printValue = function(value) {
  116. var result = self.options.formattingFn(value);
  117. if (self.d.tagName === 'INPUT') {
  118. this.d.value = result;
  119. }
  120. else if (self.d.tagName === 'text' || self.d.tagName === 'tspan') {
  121. this.d.textContent = result;
  122. }
  123. else {
  124. this.d.innerHTML = result;
  125. }
  126. };
  127. self.count = function(timestamp) {
  128. if (!self.startTime) { self.startTime = timestamp; }
  129. self.timestamp = timestamp;
  130. var progress = timestamp - self.startTime;
  131. self.remaining = self.duration - progress;
  132. // to ease or not to ease
  133. if (self.options.useEasing) {
  134. if (self.countDown) {
  135. self.frameVal = self.startVal - self.options.easingFn(progress, 0, self.startVal - self.endVal, self.duration);
  136. } else {
  137. self.frameVal = self.options.easingFn(progress, self.startVal, self.endVal - self.startVal, self.duration);
  138. }
  139. } else {
  140. if (self.countDown) {
  141. self.frameVal = self.startVal - ((self.startVal - self.endVal) * (progress / self.duration));
  142. } else {
  143. self.frameVal = self.startVal + (self.endVal - self.startVal) * (progress / self.duration);
  144. }
  145. }
  146. // don't go past endVal since progress can exceed duration in the last frame
  147. if (self.countDown) {
  148. self.frameVal = (self.frameVal < self.endVal) ? self.endVal : self.frameVal;
  149. } else {
  150. self.frameVal = (self.frameVal > self.endVal) ? self.endVal : self.frameVal;
  151. }
  152. // decimal
  153. self.frameVal = Math.round(self.frameVal*self.dec)/self.dec;
  154. // format and print value
  155. self.printValue(self.frameVal);
  156. // whether to continue
  157. if (progress < self.duration) {
  158. self.rAF = requestAnimationFrame(self.count);
  159. } else {
  160. if (self.callback) self.callback();
  161. }
  162. };
  163. // start your animation
  164. self.start = function(callback) {
  165. if (!self.initialize()) return;
  166. self.callback = callback;
  167. self.rAF = requestAnimationFrame(self.count);
  168. };
  169. // toggles pause/resume animation
  170. self.pauseResume = function() {
  171. if (!self.paused) {
  172. self.paused = true;
  173. cancelAnimationFrame(self.rAF);
  174. } else {
  175. self.paused = false;
  176. delete self.startTime;
  177. self.duration = self.remaining;
  178. self.startVal = self.frameVal;
  179. requestAnimationFrame(self.count);
  180. }
  181. };
  182. // reset to startVal so animation can be run again
  183. self.reset = function() {
  184. self.paused = false;
  185. delete self.startTime;
  186. self.initialized = false;
  187. if (self.initialize()) {
  188. cancelAnimationFrame(self.rAF);
  189. self.printValue(self.startVal);
  190. }
  191. };
  192. // pass a new endVal and start animation
  193. self.update = function (newEndVal) {
  194. if (!self.initialize()) return;
  195. cancelAnimationFrame(self.rAF);
  196. self.paused = false;
  197. delete self.startTime;
  198. self.startVal = self.frameVal;
  199. self.endVal = Number(newEndVal);
  200. if (ensureNumber(self.endVal)) {
  201. self.countDown = (self.startVal > self.endVal);
  202. self.rAF = requestAnimationFrame(self.count);
  203. } else {
  204. console.error('[CountUp] update() - new endVal is not a number', newEndVal);
  205. }
  206. };
  207. // format startVal on initialization
  208. if (self.initialize()) self.printValue(self.startVal);
  209. };
  210. return CountUp;
  211. }));