Advertisement
Guest User

count to

a guest
Mar 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.   var CountTo = function (element, options) {
  3.     this.$element = $(element);
  4.     this.options  = $.extend({}, CountTo.DEFAULTS, this.dataOptions(), options);
  5.     this.init();
  6.   };
  7.  
  8.   CountTo.DEFAULTS = {
  9.     from: 0,
  10.     to: 0,
  11.     speed: 1000,
  12.     refreshInterval: 100,
  13.     decimals: 0,
  14.     formatter: formatter,
  15.     onUpdate: null,
  16.     onComplete: null
  17.   };
  18.  
  19.   CountTo.prototype.init = function () {
  20.     this.value     = this.options.from;
  21.     this.loops     = Math.ceil(this.options.speed / this.options.refreshInterval);
  22.     this.loopCount = 0;
  23.     this.increment = (this.options.to - this.options.from) / this.loops;
  24.   };
  25.  
  26.   CountTo.prototype.dataOptions = function () {
  27.     var options = {
  28.       from:            this.$element.data('from'),
  29.       to:              this.$element.data('to'),
  30.       speed:           this.$element.data('speed'),
  31.       refreshInterval: this.$element.data('refresh-interval'),
  32.       decimals:        this.$element.data('decimals')
  33.     };
  34.  
  35.     var keys = Object.keys(options);
  36.  
  37.     for (var i in keys) {
  38.       var key = keys[i];
  39.  
  40.       if (typeof(options[key]) === 'undefined') {
  41.         delete options[key];
  42.       }
  43.     }
  44.  
  45.     return options;
  46.   };
  47.  
  48.   CountTo.prototype.update = function () {
  49.     this.value += this.increment;
  50.     this.loopCount++;
  51.  
  52.     this.render();
  53.  
  54.     if (typeof(this.options.onUpdate) == 'function') {
  55.       this.options.onUpdate.call(this.$element, this.value);
  56.     }
  57.  
  58.     if (this.loopCount >= this.loops) {
  59.       clearInterval(this.interval);
  60.       this.value = this.options.to;
  61.  
  62.       if (typeof(this.options.onComplete) == 'function') {
  63.         this.options.onComplete.call(this.$element, this.value);
  64.       }
  65.     }
  66.   };
  67.  
  68.   CountTo.prototype.render = function () {
  69.     var formattedValue = this.options.formatter.call(this.$element, this.value, this.options);
  70.     this.$element.text(formattedValue);
  71.   };
  72.  
  73.   CountTo.prototype.restart = function () {
  74.     this.stop();
  75.     this.init();
  76.     this.start();
  77.   };
  78.  
  79.   CountTo.prototype.start = function () {
  80.     this.stop();
  81.     this.render();
  82.     this.interval = setInterval(this.update.bind(this), this.options.refreshInterval);
  83.   };
  84.  
  85.   CountTo.prototype.stop = function () {
  86.     if (this.interval) {
  87.       clearInterval(this.interval);
  88.     }
  89.   };
  90.  
  91.   CountTo.prototype.toggle = function () {
  92.     if (this.interval) {
  93.       this.stop();
  94.     } else {
  95.       this.start();
  96.     }
  97.   };
  98.  
  99.   function formatter(value, options) {
  100.     return value.toFixed(options.decimals);
  101.   }
  102.  
  103.   $.fn.countTo = function (option) {
  104.     return this.each(function () {
  105.       var $this   = $(this);
  106.       var data    = $this.data('countTo');
  107.       var init    = !data || typeof(option) === 'object';
  108.       var options = typeof(option) === 'object' ? option : {};
  109.       var method  = typeof(option) === 'string' ? option : 'start';
  110.  
  111.       if (init) {
  112.         if (data) data.stop();
  113.         $this.data('countTo', data = new CountTo(this, options));
  114.       }
  115.  
  116.       data[method].call(data);
  117.     });
  118.   };
  119. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement