PageSourceSearch

https://track24.ru/js/countUp.js

js track24.ru collected 2026-10-01 13:05:46 UTC 7,540 bytes, 212 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.