forked from ciromattia/jquery.counterup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.counterup.js
executable file
·149 lines (127 loc) · 5.69 KB
/
jquery.counterup.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
/*!
* jquery.counterup.js 2.1.0
*
* Copyright 2013, Benjamin Intal http://gambit.ph @bfintal
* Released under the GPL v2 License
*
* Amended by Jeremy Paris, Ciro Mattia Gonano and others
*
* Date: Feb 24, 2017
*/
(function ($) {
"use strict";
$.fn.counterUp = function (options) {
// Defaults
var settings = $.extend({
'time': 400,
'delay': 10,
'offset': 100,
'beginAt': 0,
'formatter': false,
'context': 'window',
'again': false,
'ifVisible': true,
'easing': false,
'booster': 25,
callback: function () {
}
}, options),
s;
return this.each(function () {
// Store the object
var $this = $(this),
counter = {
num: $(this).data('counterup-num') || $(this).text(),
time: $(this).data('counterup-time') || settings.time,
delay: $(this).data('counterup-delay') || settings.delay,
offset: $(this).data('counterup-offset') || settings.offset,
beginAt: $(this).data('counterup-beginat') || settings.beginAt,
again: $(this).data('counterup-again') || settings.again,
ifVisible: $(this).data('counterup-ifvisible') || settings.ifVisible,
easing: $(this).data('counterup-easing') || settings.easing,
booster: $(this).data('counterup-booster') || settings.booster,
context: $(this).data('counterup-context') || settings.context
};
var counterUpper = function () {
var nums = [];
var divisions = counter.time / counter.delay;
var num = counter.num;
var isComma = /[0-9]+,[0-9]+/.test(num);
num = num.replace(/,/g, '');
var decimalPlaces = (num.split('.')[1] || []).length;
if (counter.beginAt > num)
counter.beginAt = num;
var isTime = /[0-9]+:[0-9]+:[0-9]+/.test(num);
// Convert time to total seconds
if (isTime) {
var times = num.split(':'),
m = 1;
s = 0;
while (times.length > 0) {
s += m * parseInt(times.pop(), 10);
m *= 60;
}
}
// Generate list of incremental numbers to display
for (var i = divisions; i >= counter.beginAt / num * divisions; i--) {
var newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
// Add incremental seconds and convert back to time
if (isTime) {
newNum = parseInt(s / divisions * i);
var hours = parseInt(newNum / 3600) % 24;
var minutes = parseInt(newNum / 60) % 60;
var seconds = parseInt(newNum % 60, 10);
newNum = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}
// Preserve commas if input had commas
if (isComma) {
while (/(\d+)(\d{3})/.test(newNum.toString())) {
newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
}
}
if (settings.formatter) {
newNum = settings.formatter.call(this, newNum);
}
nums.unshift(newNum);
}
$this.data('counterup-nums', nums);
$this.text(counter.beginAt);
// Updates the number until we're done
var step = 0;
var f = function () {
if (counter.easing) {
var delay = $.easing[counter.easing](++step / divisions);
delay = delay * counter.booster + 2;
} else {
var delay = counter.delay;
}
if (!$this.data('counterup-nums')) {
settings.callback.call(this);
return;
}
$this.html($this.data('counterup-nums').shift());
if ($this.data('counterup-nums').length) {
setTimeout($this.data('counterup-func'), delay);
} else {
$this.data('counterup-nums', null);
$this.data('counterup-func', null);
settings.callback.call(this);
}
};
$this.data('counterup-func', f);
// Start the count up
setTimeout($this.data('counterup-func'), counter.delay);
};
// Perform counts when the element gets into view
$this.waypoint(function (direction) {
if (counter.ifVisible && !$(this.element).is(":visible")) {
return;
}
counterUpper();
if (!counter.again) {
this.destroy(); //-- Waypoint 3.0 version of triggerOnce
}
}, {offset: counter.offset + "%", context: counter.context});
});
};
})(jQuery);