-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress-bar.js
112 lines (87 loc) · 3.23 KB
/
progress-bar.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
(function(){
if (!document.addEventListener ||
( document.documentElement.style.transform === undefined
&& document.documentElement.style.webkitTransform === undefined)){
return;
}
var TRANSFORM = document.documentElement.style.webkitTransform !== undefined ? 'webkitTransform' : 'transform';
var options = INSTALL_OPTIONS;
var containerEl = null;
var barEl = null;
var render = function(){
if (!document.body)
return;
if (!containerEl){
containerEl = document.createElement('div');
document.body.appendChild(containerEl);
barEl = document.createElement('div');
barEl.className = 'scroll-progress-bar-bar';
containerEl.appendChild(barEl);
}
// set class name
containerEl.className = 'scroll-progress-bar-container';
// set bar position
containerEl.style.top = '';
containerEl.className += ' scroll-progress-bar-' + options.position;
// set bar thickness
if (options.thickness == 'custom')
barEl.style.height = options.customThickness + 'px';
else
barEl.style.height = options.thickness + 'px';
// set bar color
barEl.style.backgroundColor = options.barColor;
// set background color
if (options.containerHasColor)
containerEl.style.backgroundColor = options.containerColor;
else
containerEl.style.backgroundColor = '';
updateProgress();
};
var now = function(){
return (window.performance && performance.now()) || (+ new Date);
};
var lastCall = 0;
var updateProgress = function(){
if (!barEl)
return;
// Don't allow updates faster than 60 FPS
if (now() - lastCall < 15)
return;
lastCall = now();
var top = window.scrollY;
var height = document.body.getBoundingClientRect().height - window.innerHeight;
var percent = Math.min(top / height * 100, 100);
barEl.style[TRANSFORM] = 'translateX(' + -(100 - percent) + '%)';
if (options.showAdvanced){
if ((options.advanced.hideUntilScroll && percent < 5)
|| (options.advanced.hideAfterDone && percent > 95)) {
var delta = +options.thickness;
if (options.thickness == 'custom')
delta = +options.customThickness;
if (options.position == 'top')
delta = - delta;
containerEl.style[TRANSFORM] = 'translateY(' + delta + 'px)';
} else {
containerEl.style[TRANSFORM] = '';
}
}
if (options.translucent){
containerEl.style.opacity = 0.6;
} else {
containerEl.style.opacity = 1;
}
};
var setOptions = function(opts){
options = opts;
render();
};
window.addEventListener('scroll', updateProgress);
window.addEventListener('resize', updateProgress);
if (document.readyState == 'loading')
window.addEventListener('DOMContentLoaded', render);
else
render();
window.ScrollProgressBar = {
setOptions: setOptions
};
})();