-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmobile-chrome-vh-fix.js
59 lines (51 loc) · 1.88 KB
/
mobile-chrome-vh-fix.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
var VHChromeFix = function(selectors) {
var self = this;
var userAgent = navigator.userAgent.toLowerCase();
var isAndroidChrome = /chrome/.test(userAgent) && /android/.test(userAgent);
var isIOSChrome = /crios/.test(userAgent);
if (isAndroidChrome || isIOSChrome) {
// If we detected Chrome on Android or iOS
// Cache elements and trigger fix on init
this.getElements(selectors);
this.fixAll();
// Cache window dimensions
this.windowWidth = window.innerWidth;
this.windowHeight = window.innerHeight;
window.addEventListener('resize', function() {
// Both width and height changed (orientation change)
// This is a hack, as Android when eyboard pops up
// Triggers orientation change
if (self.windowWidth !== window.innerWidth && self.windowHeight !== window.innerHeight) {
self.windowWidth = window.innerWidth;
self.windowHeight = window.innerHeight;
self.fixAll();
}
});
}
};
VHChromeFix.prototype.getElements = function(selectors) {
this.elements = [];
// Convert selectors to array if they are not
selectors = this.isArray(selectors) ? selectors : [selectors];
for (var i = 0; i < selectors.length; i++) {
// Get all elements for selector
var selector = selectors[i].selector;
var elements = document.querySelectorAll(selector);
// Go through all elements for one selector to filter them
for (var j = 0; j < elements.length; j++) {
this.elements.push({
domElement: elements[j],
vh: selectors[i].vh
});
}
}
};
VHChromeFix.prototype.isArray = function(array) {
return Object.prototype.toString.call(array) === '[object Array]';
};
VHChromeFix.prototype.fixAll = function() {
for (var i = 0; i < this.elements.length; i++) {
var element = this.elements[i];
element.domElement.style.height = (window.innerHeight * element.vh / 100) + 'px';
}
};