-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollprogressJS.js
73 lines (68 loc) · 2.61 KB
/
scrollprogressJS.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
const progressJS = {
"defaults": {
//color
"color": "#003f72",
//height
"height": "3px",
"top": 0,
"bottom": 0,
"left": 0,
"right": 0,
"zIndex": 9999,
//bottom or top
"ontop": true,
//left to right
"ltr": true,
//element to use value
"attach": false,
//js style or css
"css": false,
//decimal numbers
"round": false,
//use only values
"nobar": false
},
"start": function (configs = {}) {
let progressJSelem;
if(!configs.nobar) {
//create element
progressJSelem = document.createElement("div");
//use css instead of js style
progressJSelem.setAttribute('id', 'progressbarJS');
//append to body
document.body.appendChild(progressJSelem);
//styles
progressJSelem.style.position = 'fixed';
progressJSelem.style.zIndex = '99999';
progressJSelem.style.width = '0%';
//configurable options
//top or bottom
configs.ontop ? progressJSelem.style.bottom = '0' : progressJSelem.style.top = progressJS.defaults.top;
//ltr or rtl
configs.ltr ? progressJSelem.style.right = '0' : progressJSelem.style.left = progressJS.defaults.left;
//height
configs.height ? progressJSelem.style.height = configs.height : progressJSelem.style.height = progressJS.defaults.height;
//color
configs.color ? progressJSelem.style.backgroundColor = configs.color : progressJSelem.style.backgroundColor = progressJS.defaults.color;
}
let attachElem = progressJS.defaults.attach;
let roundto = progressJS.defaults.round;
//round to
configs.round ? roundto = configs.round : roundto = 2;
//attach
configs.attach ? attachElem = document.querySelector(configs.attach) : false ;
//scroll event
document.addEventListener('scroll', function (e) {
const maxHeight = document.body.scrollHeight;
const sizeHeight = window.innerHeight;
const scrolls = window.scrollY;
const percentage = (scrolls / (maxHeight - sizeHeight)) * 100;
if (!configs.nobar) {
progressJSelem.style.width = percentage.toFixed(roundto) + "%";
}
if (attachElem) {
attachElem.innerHTML = percentage.toFixed(roundto);
}
});
}
}