-
Notifications
You must be signed in to change notification settings - Fork 0
/
body-scroll-lock.js
154 lines (132 loc) · 4.92 KB
/
body-scroll-lock.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
var bodyScrollLock = {
_slicedToArray: (function() {
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"]) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
return function(arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
};
})(),
// Adopted and modified solution from Bohdan Didukh (2017)
// https://stackoverflow.com/questions/41594997/ios-10-safari-prevent-scrolling-behind-a-fixed-overlay-and-maintain-scroll-posi
isIosDevice: typeof window !== "undefined" && window.navigator && window.navigator.platform && /iPad|iPhone|iPod|(iPad Simulator)|(iPhone Simulator)|(iPod Simulator)/.test(window.navigator.platform),
firstTargetElement: null,
allTargetElements: {},
initialClientY: -1,
previousBodyOverflowSetting: "",
previousDocumentElementOverflowSetting: "",
preventDefault: function(rawEvent) {
var e = rawEvent || window.event;
if (e.preventDefault) e.preventDefault();
return false;
},
setOverflowHidden: function() {
// Setting overflow on body/documentElement synchronously in Desktop Safari slows down
// the responsiveness for some reason. Setting within a setTimeout fixes this.
setTimeout(function() {
this.previousBodyOverflowSetting = document.body.style.overflow;
this.previousDocumentElementOverflowSetting = document.documentElement.style.overflow;
document.body.style.overflow = "hidden";
document.documentElement.style.overflow = "hidden";
});
},
restoreOverflowSetting: function() {
// Setting overflow on body/documentElement synchronously in Desktop Safari slows down
// the responsiveness for some reason. Setting within a setTimeout fixes this.
setTimeout(function() {
document.body.style.overflow = this.previousBodyOverflowSetting;
document.documentElement.style.overflow = this.previousDocumentElementOverflowSetting;
});
},
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
isTargetElementTotallyScrolled: function(targetElement) {
return targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
},
handleScroll: function(event, targetElement) {
var clientY = event.targetTouches[0].clientY - this.initialClientY;
if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
// element is at the top of its scroll
return this.preventDefault(event);
}
if (this.isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
// element is at the top of its scroll
return this.preventDefault(event);
}
return true;
},
disableBodyScroll: function(targetElement) {
if (this.isIosDevice) {
if (targetElement) {
this.allTargetElements[targetElement] = targetElement;
targetElement.ontouchstart = function(event) {
if (event.targetTouches.length === 1) {
// detect single touch
this.initialClientY = event.targetTouches[0].clientY;
}
};
targetElement.ontouchmove = function(event) {
if (event.targetTouches.length === 1) {
// detect single touch
scrollJack.handleScroll(event, targetElement);
}
};
}
} else {
this.setOverflowHidden();
}
if (!this.firstTargetElement) this.firstTargetElement = targetElement;
},
clearAllBodyScrollLocks: function() {
if (this.isIosDevice) {
// Clear all this.allTargetElements ontouchstart/ontouchmove handlers, and the references
Object.entries(this.allTargetElements).forEach(function(_ref) {
var _ref2 = this._slicedToArray(_ref, 2),
key = _ref2[0],
targetElement = _ref2[1];
targetElement.ontouchstart = null;
targetElement.ontouchmove = null;
delete this.allTargetElements[key];
}, this);
// Reset initial clientY
this.initialClientY = -1;
} else {
this.restoreOverflowSetting();
this.firstTargetElement = null;
}
},
enableBodyScroll: function(targetElement) {
if (this.isIosDevice) {
targetElement.ontouchstart = null;
targetElement.ontouchmove = null;
} else if (this.firstTargetElement === targetElement) {
this.restoreOverflowSetting();
this.firstTargetElement = null;
}
}
};