-
Notifications
You must be signed in to change notification settings - Fork 5
/
progress.js
119 lines (108 loc) · 3.41 KB
/
progress.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
/*
* author: https://github.com/biaochenxuying
* 使用方法:
var config = {
mountedId: '#bar',
target: 8,
step: 1,
color: 'green',
fontSize: "20px",
borderRadius: "5px",
backgroundColor: '#eee',
barBackgroundColor: 'red',
};
var p = new Progress();
p.init(config);
*/
(function() {
function Progress() {
this.mountedId = null;
this.target = 100;
this.step = 1;
this.color = '#333';
this.fontSize = '18px';
this.borderRadius = 0;
this.backgroundColor = '#eee';
this.barBackgroundColor = '#26a2ff';
}
Progress.prototype = {
init: function(config) {
if (!config.mountedId) {
alert('请输入挂载节点的 id');
return;
}
this.mountedId = config.mountedId;
this.target = config.target || this.target;
this.step = config.step || this.step;
this.fontSize = config.fontSize || this.fontSize;
this.color = config.color || this.color;
this.borderRadius = config.borderRadius || this.borderRadius;
this.backgroundColor = config.backgroundColor || this.backgroundColor;
this.barBackgroundColor =
config.barBackgroundColor || this.barBackgroundColor;
var box = document.querySelector(this.mountedId);
var width = box.offsetWidth;
var height = box.offsetHeight;
var progress = document.createElement('div');
progress.style.position = 'absolute';
progress.style.height = height + 'px';
progress.style.width = width + 'px';
progress.style.borderRadius = this.borderRadius;
progress.style.backgroundColor = this.backgroundColor;
var bar = document.createElement('div');
bar.style.float = 'left';
bar.style.height = '100%';
bar.style.width = '0';
bar.style.lineHeight = height + 'px';
bar.style.textAlign = 'center';
bar.style.borderRadius = this.borderRadius;
bar.style.backgroundColor = this.barBackgroundColor;
var text = document.createElement('span');
text.style.position = 'absolute';
text.style.top = '0';
text.style.left = '0';
text.style.height = height + 'px';
text.style.lineHeight = height + 'px';
text.style.fontSize = this.fontSize;
text.style.color = this.color;
progress.appendChild(bar);
progress.appendChild(text);
box.appendChild(progress);
this.run(progress, bar, text, this.target, this.step);
},
/**
* @name 执行动画
* @param progress 底部的 dom 对象
* @param bar 占比的 dom 对象
* @param text 文字的 dom 对象
* @param target 目标值( Number )
* @param step 动画步长( Number )
*/
run: function(progress, bar, text, target, step) {
var self = this;
++step;
var endRate = parseInt(target) - parseInt(bar.style.width);
if (endRate <= step) {
step = endRate;
}
var width = parseInt(bar.style.width);
var endWidth = width + step + '%';
bar.style.width = endWidth;
text.innerHTML = endWidth;
if (width >= 94) {
text.style.left = '94%';
} else {
text.style.left = width + 1 + '%';
}
if (width === target) {
clearTimeout(timeout);
return;
}
var timeout = setTimeout(function() {
self.run(progress, bar, text, target, step);
}, 30);
},
};
// 注册到 window 全局
window.Progress = Progress;
})();