-
Notifications
You must be signed in to change notification settings - Fork 0
/
whereurat.js
99 lines (82 loc) · 2.73 KB
/
whereurat.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
var whereURAtUtils = (function () {
'use strict';
function addEventListener(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler);
} else {
element.attachEvent('on' + eventName, function () {
handler.call(element);
});
}
}
function extend(target, extender) {
for (var key in extender) {
if (target.hasOwnProperty(key)) {
target[key] = extender[key];
}
}
}
return {
extend: extend,
addEventListener: addEventListener
};
}());
var whereURAt = (function (utilityService) {
'use strict';
var bar,
_config = {
barId: 'whereURAt-bar',
barColor: '#298AD9',
barHeight: '2px',
animate: true,
animationSpeed: 0.1,
zIndex: '999999'
};
function add(config) {
utilityService.extend(_config, config);
clearBar();
addBar();
addBarStyle();
addWidthAdjustHandler();
adjustWidth();
}
function addBar() {
document.body.insertAdjacentHTML('afterbegin', '<div id="' + _config.barId + '"></div>');
bar = document.getElementById(_config.barId);
}
function clearBar() {
var bar = document.getElementById(_config.barId);
if (bar) {
document.removeEventListener('scroll', adjustWidth);
document.body.removeChild(bar);
}
}
function addBarStyle() {
var transition;
bar.style.position = 'fixed';
bar.style.top = '0';
bar.style.width = '0';
bar.style.height = _config.barHeight;
bar.style.backgroundColor = _config.barColor;
bar.style.zIndex = _config.zIndex;
if (_config.animate) {
transition = 'width ' + _config.animationSpeed + 's linear';
bar.style['-webkit-transition'] = transition;
bar.style['-moz-transition'] = transition;
bar.style['-ms-transition'] = transition;
bar.style['-o-transition'] = transition;
bar.style['transition'] = transition;
}
}
function addWidthAdjustHandler() {
utilityService.addEventListener(document, 'scroll', adjustWidth);
utilityService.addEventListener(document, 'resize', adjustWidth);
}
function adjustWidth() {
var percentage = 100 / (document.body.clientHeight - window.innerHeight) * document.body.scrollTop;
bar.style.width = percentage + '%';
}
return {
add: add
};
}(whereURAtUtils));