-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
67 lines (59 loc) · 1.67 KB
/
index.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
(function(root) {
var isOn = false;
var scrollbarSize;
var scrollTop;
function getScrollbarSize() {
if (typeof scrollbarSize !== 'undefined') return scrollbarSize;
var doc = document.documentElement;
var dummyScroller = document.createElement('div');
dummyScroller.setAttribute('style', 'width:99px;height:99px;' + 'position:absolute;top:-9999px;overflow:scroll;');
doc.appendChild(dummyScroller);
scrollbarSize = dummyScroller.offsetWidth - dummyScroller.clientWidth;
doc.removeChild(dummyScroller);
return scrollbarSize;
}
function hasScrollbar() {
return document.documentElement.scrollHeight > window.innerHeight;
}
function on(options) {
if (typeof document === 'undefined' || isOn) return;
var doc = document.documentElement;
scrollTop = window.pageYOffset;
if (hasScrollbar()) {
doc.style.width = 'calc(100% - '+ getScrollbarSize() +'px)';
} else {
doc.style.width = '100%';
}
doc.style.position = 'fixed';
doc.style.top = -scrollTop + 'px';
doc.style.overflow = 'hidden';
isOn = true;
}
function off() {
if (typeof document === 'undefined' || !isOn) return;
var doc = document.documentElement;
doc.style.width = '';
doc.style.position = '';
doc.style.top = '';
doc.style.overflow = '';
window.scroll(0, scrollTop);
isOn = false;
}
function toggle() {
if (isOn) {
off();
return;
}
on();
}
var noScroll = {
on: on,
off: off,
toggle: toggle,
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = noScroll;
} else {
root.noScroll = noScroll;
}
})(this);