counterup.min.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. * jquery.countup.js 1.0.3
  3. *
  4. * Copyright 2016, Adrián Guerra Marrero http://agmstudio.io @AGMStudio_io
  5. * Released under the MIT License
  6. *
  7. * Date: Oct 27, 2016
  8. */
  9. (function( $ ){
  10. "use strict";
  11. $.fn.countUp = function( options ) {
  12. // Defaults
  13. var settings = $.extend({
  14. 'time': 2000,
  15. 'delay': 10
  16. }, options);
  17. return this.each(function(){
  18. // Store the object
  19. var $this = $(this);
  20. var $settings = settings;
  21. var counterUpper = function() {
  22. if(!$this.data('counterupTo')) {
  23. $this.data('counterupTo',$this.text());
  24. }
  25. var time = parseInt($this.data("counter-time")) > 0 ? parseInt($this.data("counter-time")) : $settings.time;
  26. var delay = parseInt($this.data("counter-delay")) > 0 ? parseInt($this.data("counter-delay")) : $settings.delay;
  27. var divisions = time / delay;
  28. var num = $this.data('counterupTo');
  29. var nums = [num];
  30. var isComma = /[0-9]+,[0-9]+/.test(num);
  31. num = num.replace(/,/g, '');
  32. var isInt = /^[0-9]+$/.test(num);
  33. var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
  34. var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;
  35. // Generate list of incremental numbers to display
  36. for (var i = divisions; i >= 1; i--) {
  37. // Preserve as int if input was int
  38. var newNum = parseInt(Math.round(num / divisions * i));
  39. // Preserve float if input was float
  40. if (isFloat) {
  41. newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
  42. }
  43. // Preserve commas if input had commas
  44. if (isComma) {
  45. while (/(\d+)(\d{3})/.test(newNum.toString())) {
  46. newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
  47. }
  48. }
  49. nums.unshift(newNum);
  50. }
  51. $this.data('counterup-nums', nums);
  52. $this.text('0');
  53. // Updates the number until we're done
  54. var f = function() {
  55. if (!$this.data('counterup-nums')) {
  56. return;
  57. }
  58. $this.text($this.data('counterup-nums').shift());
  59. if ($this.data('counterup-nums').length) {
  60. setTimeout($this.data('counterup-func'),delay);
  61. } else {
  62. delete $this.data('counterup-nums');
  63. $this.data('counterup-nums', null);
  64. $this.data('counterup-func', null);
  65. }
  66. };
  67. $this.data('counterup-func', f);
  68. // Start the count up
  69. setTimeout($this.data('counterup-func'),delay);
  70. };
  71. // Perform counts when the element gets into view
  72. $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true });
  73. });
  74. };
  75. })( jQuery );