Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (130 loc) · 4.8 KB
/
Copy pathscript.js
File metadata and controls
213 lines (130 loc) · 4.8 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// My Project no. #2
// function for get value from input element entered by user and return it
function getInputValue () {
// get input element
const input_el = document.querySelector ("input");
// get input element value
const input_val = input_el.value;
// return it
return input_val;
}
// function for clear input field
function clearInputField () {
// get input element
const input_el = document.querySelector ("input");
// clear
input_el.value = "";
}
// function for enable or disable button
function disabledButton (bool) {
// get button element
const button_el = document.querySelector ("button");
// set bool for it
button_el.disabled = bool;
// set background color on different state
if (bool) {
button_el.style.backgroundColor = "gray";
}else {
button_el.style.backgroundColor = "gold";
// clear input field
clearInputField ();
}
}
// This is called when the user clicks the button
// and update percentage text and progress bar
function update () {
disabledButton(true);
// get input value from getInputValue function
let input_val = getInputValue ();
// condition for check input_val entered by user
// Number only not empty
if (input_val) {
// change input_val to Integer
input_val = parseInt (input_val);
// change input_val to absolute (e.g. -75 to 75)
input_val = Math.abs(input_val);
// condition for check input_val is greater than 100
if (input_val > 100) {
input_val = 100;
}
}else {
input_val = 0;
}
// calling function
updateProgressBar (input_val);
}
// function for change text in paragraph element
function changeTextInPara (num) {
// get paragraph (para) element (el) by id
const para_el = document.getElementById ("percentage");
// change text in paragraph
para_el.innerText = num + "%";
}
// function for update dash offset value
function updateDashOffset(dashoffset_val) {
// get circle element by it's Id
const circle = document.getElementById('progress-circle');
// set it's transition value to none
circle.style.transition = 'none'; // Disable transition temporarily
// set attribute
circle.setAttribute('stroke-dashoffset', dashoffset_val);
// Trigger reflow to apply immediate changes
void circle.offsetWidth;
// Re-enable transition with empty string (using single quotes)
circle.style.transition = '';
}
// function for update progress bar
function updateProgressBar (percentage) {
// here, calculating the dashoffset value from percentage
const dashoffset_val = Math.round (534 - (534*percentage)/100);
// update dashoffset with dashoffset_val
updateDashOffset (dashoffset_val);
// get paragraph (para) element (el) by id
const para_el = document.getElementById ("percentage");
// get innerText value of para_el
// and remove '%' with replace method
let current_percentage = para_el.innerText.replace ("%", "");
// condition
if (current_percentage < percentage) {
// calculate the interval time for increasePercentage
const interval_time = Math.round (2000/(percentage - current_percentage));
// calling increasePercentage
increasePercentage (current_percentage, percentage, interval_time);
}else if (current_percentage > percentage) {
// calculate the interval time for decreasePercentage
const interval_time = Math.round (2000/(current_percentage - percentage));
// calling decreasePercentage
decreasePercentage (current_percentage, percentage, interval_time);
}else {
disabledButton (false);
}
}
// function for increase percentage
function increasePercentage (current_percentage, percentage, interval_time) {
const my_interval = setInterval ( () => {
changeTextInPara (current_percentage);
current_percentage ++;
if (current_percentage > percentage) {
clearInterval (my_interval);
disabledButton (false);
}
}, interval_time);
}
// function for decrease percentage
function decreasePercentage (current_percentage, percentage, interval_time) {
const my_interval = setInterval ( () => {
changeTextInPara (current_percentage);
current_percentage --;
if (current_percentage < percentage) {
clearInterval (my_interval);
disabledButton (false);
}
}, interval_time);
}
// Hope you liked this code.
// I have written a post for this code. You can checkout if you want.
// Please let me know in the comments so I can improve it further.
// My name is SONU JANGIR.
// I'm 24 years old.
// I Love coding and art
// I make projects for people who like it.