-
Notifications
You must be signed in to change notification settings - Fork 4
/
viewport.genie.bookmarklet.js
119 lines (105 loc) · 2.77 KB
/
viewport.genie.bookmarklet.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
/*
* Viewport Genie v0.3
* Adds the "real" viewport width and height (in px and em) as an element on the body to help with obtaining values for responsive breakpoints
*
* Use it in conjunction with mqGenie (https://github.com/stowball/mqGenie) to trigger the correct breakpoints in non-WebKit browsers
*
* Copyright (c) 2013 Matt Stow
*
* http://mattstow.com
*
* Licensed under the MIT license
*/
;(function(param) {
if (param) {
var rx = /(&|\?)(\w+)=true/gi,
str = window.location.href,
m,
s = '',
gup = {};
while (m = rx.exec(str)) {
s += ' ' + m[2];
gup[m[2]] = true;
}
if (!gup.genie)
return;
}
var $html = document.documentElement,
$head = document.head,
$body = document.body,
id = 'vp-genie',
$genie = document.getElementById(id),
$style = document.getElementById(id + '-style'),
fontSize,
property = $html.clientWidth ? true: false;
var viewportGenie = {
init: function() {
if ($genie) {
$body.removeChild($genie);
$head.removeChild($style);
return;
}
var css = '#' + id + '{' +
'background: rgba(255,0,0,.75);' +
'border: 1px solid #900;' +
'-webkit-box-shadow: 2px 2px 2px rgba(0,0,0,.3);' +
'box-shadow: 2px 2px 2px rgba(0,0,0,.3);' +
'color: #fff;' +
'font-family: monospace;' +
'font-size: 13px;' +
'left: 0;' +
'line-height: 17px;' +
'padding: 3px 6px 5px;' +
'position: fixed;' +
'top: 0;' +
'z-index: 9999;' +
'}' +
'#' + id + ' span {' +
'white-space: nowrap;' +
'}',
rules = document.createTextNode(css);
$style = document.createElement('style');
$style.setAttribute('id', id + '-style');
$style.type = 'text/css';
if ($style.styleSheet)
$style.styleSheet.cssText = rules.nodeValue;
else
$style.appendChild(rules);
$head.appendChild($style);
$genie = document.createElement('div');
$genie.setAttribute('id', id);
$body.appendChild($genie);
viewportGenie.calculate();
if (window.addEventListener)
window.addEventListener('resize', viewportGenie.calculate, false);
},
calculate: function() {
if (window.getComputedStyle)
fontSize = parseInt(window.getComputedStyle($html).getPropertyValue('font-size'));
else
fontSize = 16;
var width,
height;
if (property) {
width = $html.clientWidth;
height = $html.clientHeight;
}
else {
width = window.innerWidth;
height = window.innerHeight;
}
$genie.innerHTML = '<span>' +
width + 'px' +
' × ' +
height + 'px' +
'</span>' +
' • ' +
'<span>' +
width/fontSize + 'em' +
' × ' +
height/fontSize + 'em' +
'</span>';
}
};
viewportGenie.init();
})();