-
Notifications
You must be signed in to change notification settings - Fork 1
/
stopBodyScrolling.ts
172 lines (158 loc) · 5.8 KB
/
stopBodyScrolling.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright 2019 LABOR.digital
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Last modified: 2019.01.24 at 10:51
*/
import {getData} from '../Dom/getData';
import {isBrowser} from '../Environment/isBrowser';
import type {List} from '../Interfaces/List';
import type {PlainObject} from '../Interfaces/PlainObject';
import {forEach} from '../Lists/forEach';
import {getPageStorage} from '../Misc/getPageStorage';
import {isBool} from '../Types/isBool';
import {isNull} from '../Types/isNull';
import {isObject} from '../Types/isObject';
import {isUndefined} from '../Types/isUndefined';
import {getScrollPos} from './getScrollPos';
const storage = getPageStorage();
const isIos = isBrowser() && !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
const storageKeyStopped = 'stopBodyScrolling';
const storageKeyBackup = 'stopBodyScrollingPropertyBackup';
let html: HTMLElement | null = null;
let body: HTMLElement | null = null;
let forceHtmlBlocking = isIos;
/**
* Reads the current body width in pixels
*/
function getBodyWidth(): number
{
return Math.max(
document.documentElement.clientWidth,
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth
);
}
/**
* If this is set to true the html block will also have it's overflow marked as hidden.
* This causes jumping but is required for some sites.
* On IOS clients this is true by default.
*
* @param state
*/
export function setForceHtmlScrollBlocking(state?: boolean)
{
forceHtmlBlocking = isBool(state) ? state : true;
}
/**
* Helper to prevent the body from being scrolled with a fix for the ios 9 safari which is a pain...
* @param {boolean} state True to stop the scrolling, false to re-enable it.
*/
export function stopBodyScrolling(state?: boolean)
{
if (!isBrowser()) {
return;
}
if (isNull(html)) {
html = document.querySelector('html') as HTMLElement;
}
if (isNull(body)) {
body = document.querySelector('body') as HTMLElement;
}
// Stop the scrolling
if (isUndefined(state) || state) {
// Ignore if already stopped
if (storage.get(storageKeyStopped, false)) {
return;
}
storage.set(storageKeyStopped, true);
// Calculate the body width before we hide the scrollbar
const oldBodyWidth = getBodyWidth();
// Backup elements
const createElementStyleBackup = function (el: HTMLElement) {
if (!isObject(el) || isUndefined(el.style)) {
return {};
}
const style = el.style;
return {
overflowX: style.overflowX,
overflowY: style.overflowY,
overflow: style.overflow,
position: style.position,
height: style.height,
transition: style.transition,
paddingRight: style.paddingRight
};
};
storage.set(storageKeyBackup, {
html: createElementStyleBackup(html),
body: createElementStyleBackup(body),
scrollPos: getScrollPos()
});
// Disable scrolling
const disableElementScroll = function (el: HTMLElement, isHtml: boolean) {
el.style.overflow = 'hidden';
el.style.transition = 'none';
el.style.overflowY = 'hidden';
el.style.overflowX = 'auto';
if (!isHtml || isIos) {
el.style.overflow = 'hidden';
el.style.height = 'hidden';
}
};
if (forceHtmlBlocking) {
disableElementScroll(html, true);
}
disableElementScroll(body, false);
// Calculate difference
const bodyWidthDiff = getBodyWidth() - oldBodyWidth;
body.style.paddingRight = bodyWidthDiff + 'px';
// Prevent jumping of fixed elements
forEach(document.querySelectorAll('*[data-stop-body-scrolling-fixed]') as List, (element: HTMLElement) => {
let offsetWidth = bodyWidthDiff;
const config = getData(element, 'stop-body-scrolling-fixed', '');
if (config === 'half') {
offsetWidth = offsetWidth / 2;
}
element.style.transform = 'translateX(-' + offsetWidth + 'px)';
});
} else {
// Ignore if already active
if (!storage.get(storageKeyStopped, false)) {
return;
}
storage.set(storageKeyStopped, false);
// Restore backup
const backup = storage.get(storageKeyBackup, {html: {}, body: {}, scrollPos: 0});
storage.remove(storageKeyBackup);
const restoreBackup = function (el: HTMLElement, backup: PlainObject) {
forEach(backup, (v, k) => {
el.style[k] = v;
});
};
if (forceHtmlBlocking) {
restoreBackup(html, backup.html);
}
restoreBackup(body, backup.body);
if (forceHtmlBlocking) {
window.scrollTo(0, backup.scrollPos);
}
// Reset fixed elements
forEach(document.querySelectorAll('*[data-stop-body-scrolling-fixed]') as List, (element: HTMLElement) => {
element.style.transform = '';
});
}
}