forked from tysonmatanich/viewportSize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewportSize.js
56 lines (49 loc) · 1.9 KB
/
viewportSize.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
/*! viewportSize | Author: Tyson Matanich, 2013 | License: MIT */
(function (window) {
window.viewportSize = {};
window.viewportSize.getHeight = function () {
return getSize("Height");
};
window.viewportSize.getWidth = function () {
return getSize("Width");
};
var getSize = function (Name) {
var size;
var name = Name.toLowerCase();
var document = window.document;
var documentElement = document.documentElement;
if (window["inner" + Name] === undefined) {
// IE6 & IE7 don't have window.innerWidth or innerHeight
size = documentElement["client" + Name];
}
else if (window["inner" + Name] != documentElement["client" + Name]) {
// WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
// Insert markup to test if a media query will match document.doumentElement["client" + Name]
var bodyElement = document.createElement("body");
bodyElement.id = "vpw-test-b";
bodyElement.style.cssText = "overflow:scroll";
var divElement = document.createElement("div");
divElement.id = "vpw-test-d";
divElement.style.cssText = "position:absolute;top:-1000px";
// Getting specific on the CSS selector so it won't get overridden easily
divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
bodyElement.appendChild(divElement);
documentElement.insertBefore(bodyElement, document.head);
if (divElement["offset" + Name] == 7) {
// Media query matches document.documentElement["client" + Name]
size = documentElement["client" + Name];
}
else {
// Media query didn't match, use window["inner" + Name]
size = window["inner" + Name];
}
// Cleanup
documentElement.removeChild(bodyElement);
}
else {
// Default to use window["inner" + Name]
size = window["inner" + Name];
}
return size;
};
})(this);