-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfillingBar.js
More file actions
92 lines (70 loc) · 1.77 KB
/
fillingBar.js
File metadata and controls
92 lines (70 loc) · 1.77 KB
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
/*
function stop(){
let width = 0;
let el = document.getElementById('progress-bar');
let interval_id = setInterval(frame, 60);
function frame() {
if(width >= 100){
clearInterval(interval_id);
}else{
width++;
el.style.width = width + '%';
}
}
}
animation?
循环?
*/
function movingBar(bar_container, model) {
// adding element
let btnElem = document.querySelector(".stop_btn");
btnElem.textContent = "STOP";
bar_container.innerHTML = "<div class='bar_container_filling'></div>";
// event
btnElem.addEventListener("click", function(e) {
model.action();
btnElem.textContent = (btnElem.textContent === "STOP") ? "RESUME" : "STOP";
});
//for CSS. react, update the information to DOM
function render(data) {
let innerElem = document.querySelector(".bar_container_filling");
innerElem.style.width = data + "%";
}
//subscribe() to store for changes
model.subscribe(render);
//
render();
}
function pbModel() {
let from = 0;
let subscriber;
let changeRate = 0.25;
let updateStatus = true;
let intervalId = setInterval(update, 5);
//back and force
function update() {
if ( from === 0 && changeRate < 0 || from === 100 && changeRate > 0 ) {
changeRate = -changeRate;
};
from += changeRate;
subscriber(from);
}
function _action() {
if (updateStatus) {
clearInterval(intervalId);
} else {
intervalId = setInterval(update, 5);
}
updateStatus = updateStatus ? false : true;
}
return {
subscribe: function(cb) {
if (!subscriber) {
subscriber = cb;
}
},
action: _action,
};
};
let bar_container = document.querySelector(".bar_container");
movingBar(bar_container, pbModel());