-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaskHelper.js
55 lines (52 loc) · 1.93 KB
/
MaskHelper.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
/**
* polyfill document.scrollingElement
* author: Brook <[email protected]>
* https://github.com/mathiasbynens/document.scrollingElement
*/
if(!document.scrollingElement) (function() {
var element = null;
function scrollingElement() {
if(element) {
return element;
} else if(document.body.scrollTop) {
// speed up if scrollTop > 0
return (element = document.body);
}
var iframe = document.createElement('iframe');
iframe.style.height = '1px';
document.documentElement.appendChild(iframe);
var doc = iframe.contentWindow.document;
doc.write('<!DOCTYPE html><div style="height:9999em">x</div>');
doc.close();
var isCompliant = doc.documentElement.scrollHeight > doc.body.scrollHeight;
iframe.parentNode.removeChild(iframe);
return (element = isCompliant ? document.documentElement : document.body);
}
Object.defineProperty(document, 'scrollingElement', {
'get': scrollingElement
});
})();
/**
* ======================== mask ========================
* 参考:https://uedsky.com/2016-06/mobile-modal-scroll/
* ModalHelper helpers resolve the modal scrolling issue on mobile devices
* https://github.com/twbs/bootstrap/issues/15852
* requires document.scrollingElement polyfill https://uedsky.com/demo/src/polyfills/document.scrollingElement.js
*/
var MaskHelperScrollTop;
var MaskHelper = {
afterOpen: function(bodyCls) {
MaskHelperScrollTop = document.scrollingElement.scrollTop;
document.body.classList.add(bodyCls);
document.body.style.top = -MaskHelperScrollTop + 'px';
// console.log(MaskHelperScrollTop);
},
beforeClose: function(bodyCls) {
document.body.classList.remove(bodyCls);
// scrollTop lost after set position:fixed, restore it back.
// document.scrollingElement.scrollTop = MaskHelperScrollTop;
document.body.style.top = '0';
document.body.scrollTop = MaskHelperScrollTop;
// console.log(MaskHelperScrollTop);
}
};