-
Notifications
You must be signed in to change notification settings - Fork 28
/
visualViewport.js
204 lines (181 loc) · 8.12 KB
/
visualViewport.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// This is hacky but necessary in order to get the innerWidth/Height without
// page scale applied reliably.
function updateUnscaledDimensions() {
if (!window.viewPolyfill.iframeDummy) {
var iframe = document.createElement('iframe');
iframe.style.position="absolute";
iframe.style.width="100%";
iframe.style.height="100%";
iframe.style.left="0px";
iframe.style.top="0px";
iframe.style.border="0";
iframe.style.visibility="hidden";
iframe.style.zIndex="-1";
iframe.srcdoc = "<!DOCTYPE html><html><body style='margin:0px; padding:0px'></body></html>";
document.body.appendChild(iframe);
window.viewPolyfill.iframeDummy = iframe;
}
var iframe = window.viewPolyfill.iframeDummy;
var documentRect = document.documentElement.getBoundingClientRect();
var iframeBody = iframe.contentDocument.body;
iframeBody.style.width = documentRect.width + 'px';
iframeBody.style.height = documentRect.height + 'px';
// Hide overflow temporarily so that the iframe size isn't shrunk by
// scrollbars.
var prevDocumentOverflow = document.documentElement.style.overflow;
document.documentElement.style.overflow = "hidden";
var iframeWindow = window.viewPolyfill.iframeDummy.contentWindow;
window.viewPolyfill.unscaledInnerWidth = iframeWindow.innerWidth;
window.viewPolyfill.unscaledInnerHeight = iframeWindow.innerHeight;
document.documentElement.style.overflow = prevDocumentOverflow;
}
function fireScrollEvent() {
var listeners = window.viewPolyfill.scrollEventListeners;
for (var i = 0; i < listeners.length; i++)
listeners[i]();
}
function fireResizeEvent() {
var listeners = window.viewPolyfill.resizeEventListeners;
for (var i = 0; i < listeners.length; i++)
listeners[i]();
}
function updateViewportChanged() {
var scrollChanged =
window.viewPolyfill.offsetLeftSinceLastChange != window.visualViewport.offsetLeft ||
window.viewPolyfill.offsetTopSinceLastChange != window.visualViewport.offsetTop;
var sizeChanged =
window.viewPolyfill.widthSinceLastChange != window.visualViewport.width ||
window.viewPolyfill.heightSinceLastChange != window.visualViewport.height ||
window.viewPolyfill.scaleSinceLastChange != window.visualViewport.scale;
window.viewPolyfill.offsetLeftSinceLastChange = window.visualViewport.offsetLeft;
window.viewPolyfill.offsetTopSinceLastChange = window.visualViewport.offsetTop;
window.viewPolyfill.widthSinceLastChange = window.visualViewport.width;
window.viewPolyfill.heightSinceLastChange = window.visualViewport.height;
window.viewPolyfill.scaleSinceLastChange = window.visualViewport.scale;
if (scrollChanged)
fireScrollEvent();
if (sizeChanged)
fireResizeEvent();
setTimeout(updateViewportChanged, 500);
}
function registerChangeHandlers() {
window.addEventListener('scroll', updateViewportChanged, {'passive': true});
window.addEventListener('resize', updateViewportChanged, {'passive': true});
window.addEventListener('resize', updateUnscaledDimensions, {'passive': true});
}
var isChrome = navigator.userAgent.indexOf('Chrome') > -1;
var isSafari = navigator.userAgent.indexOf("Safari") > -1;
var isIEEdge = navigator.userAgent.indexOf('Edge') > -1;
if ((isChrome)&&(isSafari))
isSafari=false;
if (window.visualViewport) {
console.log('Using real visual viewport API');
} else {
console.log('Polyfilling Viewport API');
var layoutDummy = document.createElement('div');
layoutDummy.style.width = "100%";
layoutDummy.style.height = "100%";
if (isSafari) {
layoutDummy.style.position = "fixed";
} else {
layoutDummy.style.position = "absolute";
}
layoutDummy.style.left = "0px";
layoutDummy.style.top = "0px";
layoutDummy.style.visibility = "hidden";
window.viewPolyfill = {
"offsetLeftSinceLastChange": null,
"offsetTopSinceLastChange": null,
"widthSinceLastChange": null,
"heightSinceLastChange": null,
"scaleSinceLastChange": null,
"scrollEventListeners": [],
"resizeEventListeners": [],
"layoutDummy": layoutDummy,
"iframeDummy": null,
"unscaledInnerWidth": 0,
"unscaledInnerHeight": 0
}
registerChangeHandlers();
// TODO: Need to wait for <body> to be loaded but this is probably
// later than needed.
window.addEventListener('load', function() {
updateUnscaledDimensions();
document.body.appendChild(layoutDummy);
var viewport = {
get offsetLeft() {
if (isSafari) {
// Note: Safari's getBoundingClientRect left/top is wrong when pinch-zoomed requiring this "unscaling".
return window.scrollX - (layoutDummy.getBoundingClientRect().left * this.scale + window.scrollX * this.scale);
} else {
return window.scrollX + layoutDummy.getBoundingClientRect().left;
}
},
get offsetTop() {
if (isSafari) {
// Note: Safari's getBoundingClientRect left/top is wrong when pinch-zoomed requiring this "unscaling".
return window.scrollY - (layoutDummy.getBoundingClientRect().top * this.scale + window.scrollY * this.scale);
} else {
return window.scrollY + layoutDummy.getBoundingClientRect().top;
}
},
get width() {
var clientWidth = document.documentElement.clientWidth;
if (isIEEdge) {
// If there's no scrollbar before pinch-zooming, Edge will add
// a non-layout-affecting overlay scrollbar. This won't be
// reflected in documentElement.clientWidth so we need to
// manually subtract it out.
if (document.documentElement.clientWidth == window.viewPolyfill.unscaledInnerWidth
&& this.scale > 1) {
var oldWidth = document.documentElement.clientWidth;
var prevHeight = layoutDummy.style.height;
// Lengthen the dummy to add a layout vertical scrollbar.
layoutDummy.style.height = "200%";
var scrollbarWidth = oldWidth - document.documentElement.clientWidth;
layoutDummy.style.width = prevHeight;
clientWidth -= scrollbarWidth;
}
}
return clientWidth / this.scale;
},
get height() {
var clientHeight = document.documentElement.clientHeight;
if (isIEEdge) {
// If there's no scrollbar before pinch-zooming, Edge will add
// a non-layout-affecting overlay scrollbar. This won't be
// reflected in documentElement.clientHeight so we need to
// manually subtract it out.
if (document.documentElement.clientHeight == window.viewPolyfill.unscaledInnerHeight
&& this.scale > 1) {
var oldHeight = document.documentElement.clientHeight;
var prevWidth = layoutDummy.style.width;
// Widen the dummy to add a layout horizontal scrollbar.
layoutDummy.style.width = "200%";
var scrollbarHeight = oldHeight - document.documentElement.clientHeight;
layoutDummy.style.width = prevWidth;
clientHeight -= scrollbarHeight;
}
}
return clientHeight / this.scale;
},
get scale() {
return window.viewPolyfill.unscaledInnerWidth / window.innerWidth;
},
get pageLeft() {
return window.scrollX;
},
get pageTop() {
return window.scrollY;
},
"addEventListener": function(name, func) {
// TODO: Match event listener semantics. i.e. can't add the same callback twice.
if (name === 'scroll')
window.viewPolyfill.scrollEventListeners.push(func);
else if (name === 'resize')
window.viewPolyfill.resizeEventListeners.push(func);
}
};
window.visualViewport = viewport;
});
}