forked from davidtheclark/no-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 loc) · 1.18 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
var scrollbarSize;
var doc = global.document && global.document.documentElement;
function getScrollbarSize() {
if (typeof scrollbarSize !== 'undefined') return scrollbarSize;
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 doc.scrollHeight > window.innerHeight;
}
function on(options) {
if (!doc) return;
var rightPad = parseInt(getComputedStyle(doc)['padding-right'], 10);
var originalStyle = doc.getAttribute('style') || '';
originalStyle += 'overflow:hidden;';
if (hasScrollbar()) {
rightPad += getScrollbarSize();
originalStyle += 'padding-right:' + rightPad + 'px;';
}
doc.setAttribute('style', originalStyle);
}
function off() {
if (!doc) return;
var cleanedStyle = doc.getAttribute('style')
.replace(/overflow:hidden;(?:padding-right:.+?;)?/, '');
doc.setAttribute('style', cleanedStyle);
}
module.exports = {
on: on,
off: off,
};