forked from kalasoo/NumberProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-pb.js
114 lines (100 loc) · 3.1 KB
/
number-pb.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
(function($) {
var NumberProgressBar = function(element, options) {
var settings = $.extend ({
duration: 10000,
min: 0,
max: 100,
current: 0,
shownQuery: '.number-pb-shown',
numQuery: '.number-pb-num'
}, options || {});
this.duration = settings.duration;
if (settings.min < settings.max) {
this.min = settings.min;
this.max = settings.max;
this.current = (settings.current >= this.min && settings.current <= this.max) ? settings.current : this.min;
} else {
this.min = 0;
this.max = 100;
this.current = 0;
}
this.interval = this.max - this.min;
this.last = this.min;
this.$element = $(element);
this.$shownBar = this.$element.find(settings.shownQuery);
this.$num = this.$element.find(settings.numQuery);
this.reach(this.current);
}
NumberProgressBar.prototype.calDestination = function(dest) {
return (dest < this.min) ? this.min : ( (dest > this.max) ? this.max : dest )
}
NumberProgressBar.prototype.calDuration = function() {
return this.duration * Math.abs(this.current - this.last) / this.interval;
}
NumberProgressBar.prototype.shuffle = function(callback) {
var dest = Math.round(Math.random() * this.interval) + this.min;
this.reach(dest, null, callback);
}
NumberProgressBar.prototype.calPercentage = function() {
return (this.current - this.min) / this.interval * 100
}
NumberProgressBar.prototype.reach = function(dest, duration, callback) {
this.current = this.calDestination(dest);
this.moveShown(duration);
this.moveNum(duration, callback);
this.last = this.current;
}
NumberProgressBar.prototype.moveShown = function(duration) {
this.$shownBar.velocity({
width: this.calPercentage() + '%'
}, {
duration: duration || this.calDuration()
})
}
NumberProgressBar.prototype.moveNum = function(duration, callback) {
var self = this;
var duration = duration || this.calDuration();
this.$num.velocity({
left: this.calPercentage() + '%'
}, {
duration: duration,
complete: callback
});
// number
$({num: this.last}).animate({
num: this.current
}, {
queue: true,
duration: duration,
step: function() {
self.$num.text(Math.ceil(this.num));
},
complete: function() {
self.$num.text(self.current);
}
})
}
$.fn.NumberProgressBar = function(options) {
return this.each(function () {
var element = $(this);
if (element.data('number-pb')) return;
element.data('number-pb', new NumberProgressBar(this, options));
})
}
$.fn.reach = function(dest, options) {
var settings = $.extend ({
duration : null,
complete : null
}, options || {});
return this.each(function() {
var element = $(this);
var progressbar = element.data('number-pb');
if (!progressbar) return;
if (typeof dest === "undefined") {
progressbar.shuffle(settings.complete);
} else {
progressbar.reach(dest, settings.duration, settings.complete);
}
})
}
})(jQuery);