-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.html
305 lines (271 loc) · 8.02 KB
/
index.html
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!doctype html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='css/style.css'>
<meta name = "viewport" content = "initial-scale = 1.0">
<title>onMediaQuery - Responsive JS</title>
<style>
/*
-----------------------------------------------------------
This is a hack. It works in:
FF 3.6+, Anroid 2.2+, Safari, Chrome, IE7, IE8, IE9+
-----------------------------------------------------------
*/
html {
font-family: 'mobile';
}
* html { /* IE6 */
font-family: 'desktop'
}
*+html { /* IE7 */
font-family: 'desktop'
}
@media \0screen {
html { /* IE8 */
font-family: 'desktop'
}
}
/* Reset your font families here.
----------------------------------- */
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Queries for supported browsers.
----------------------------------- */
@media screen and (min-width: 35em) {
html {
font-family: "skinny";
}
}
@media screen and (min-width: 56em) {
html {
font-family: "desktop";
}
}
</style>
</head>
<body>
<!-- #mobile nav -->
<div id="mobile_site_nav" class='nav'>
<div id="nav_items" class='mobile_nav_items'>
<ul>
<li><a href='#1'>Home</a></li>
<li><a href='#2'>Sprockets</a></li>
<li><a href='#2'>Widgets</a></li>
<li><a href='#3'>Contact us</a></li>
</ul>
</div>
</div>
<!-- /#mobile nav -->
<div id='wrapper'>
<div id='desc'>
<h2 class='title'>onMediaQuery</h2>
<h3>A simple way to handle Media Queries with Javascript.</h3>
<p>
Chances are, you're going to want to execute some code based on media queries in your snazzy responsive layout. At Springload we've been doing this with the <code>window.matchMedia()</code> function with great results, but this has meant maintaining breakpoints in two places — once in our stylesheet and once in our JS.
</p>
<p>
Clearly this isn't very DRY. Right now, that's not too much of a problem, but the bigger a project grows, the more chance there is for this duplicate information to get out of sync. Fortunately, there's a better way.
</p>
<p>
<a href='#'>Jeremy Keith</a> recently posted a great article about using <a href='http://adactio.com/journal/5429/' target='_blank'>conditional CSS</a> to add properties to the page that are enumerable in Javascript. Jeremy uses the <code>:after</code> psuedo property on the <code><body></code> tag to add information about the current viewport. The markup is really simple:
</p>
<pre class='css_code'>
/*
-----------------------------------------------------------
This is a hack. It works in:
FF 3.6+, Anroid 2.2+, Safari, Chrome, IE7, IE8, IE9+
-----------------------------------------------------------
*/
html {
font-family: 'mobile';
}
* html { /* IE6 */
font-family: 'desktop'
}
*+html { /* IE7 */
font-family: 'desktop'
}
@media \0screen {
html { /* IE8 */
font-family: 'desktop'
}
}
/* Reset your font families here.
----------------------------------- */
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Queries for supported browsers.
----------------------------------- */
@media screen and (min-width: 35em) {
html {
font-family: "skinny";
}
}
@media screen and (min-width: 56em) {
html {
font-family: "desktop";
}
}
</pre>
<p>
This gives us a hidden string containing the currently active query, which we can fish out using a couple of lines of Javascript. All we need to do is evaluate the window when it resizes, check if the body:after property has changed, and fire a callback.
</p>
<h3>The Javascript</h3>
<p>
We've wrapped up Jeremy's great solution in a little Javascript module that can be inserted into your page. It's super lean and engineered with mobile loading times in mind.
</p>
<pre class='js_code'>
<script type="text/javascript" src="js/onmediaquery.min.js"></script>
<script>
// Define the queries you want to test for.. and what to do if they're TRUE
var queries = [
{
context: 'mobile',
match: function() {
console.log('Mobile callback. Maybe hook up some tel: numbers?');
// Your mobile specific logic can go here.
},
unmatch: function() {
// We're leaving mobile.
}
},
{
context: 'skinny',
match: function() {
console.log('skinny callback! Swap the class on the body element.');
// Your tablet specific logic can go here.
},
unmatch: function() {
console.log('leaving skinny context!');
}
},
{
context: ['mobile', 'skinny'],
call_for_each_context: false,
match: function() {
console.log('a callback which spans multiple breakpoints, skinny and mobile!');
// setting 'call_for_each_context' to false means this callback will only fire once across this range of breakpoints
},
unmatch: function() {
console.log('leaving small context!');
}
},
{
context: 'desktop',
match: function() {
console.log('desktop callback woohoo! Load some heavy desktop JS badddness.');
// your desktop specific logic can go here.
}
}
];
// Go!
MQ.init(queries);
</script>
</pre>
<p>
Make sure that the strings you define in the <code>context:</code> property match the <code>:after</code> content in your css!
</p>
<h3>Adding queries</h3>
<p>
As well as passing an array of objects when you initialise the plugin, you can add extra callbacks at any time. This is especially handy if you've got multiple JS files across the site that need to test whether a query is true.
</p>
<pre class='js_code'>
//Add a media query test
var my_query = MQ.addQuery({
context: 'skinny',
match: function() {
console.log( 'second skinny callback!' )
}
});
</pre>
<p>
The addQuery function returns a reference to the object you added, so you can remove it later if you need to:
</p>
<pre class='js_code'>
//Remove a media query test by reference
MQ.removeQuery( my_query );
</pre>
<p>
This weighs in at a whopping <strong>700 bytes</strong> minified and gzipped, well worth including in your next project.
</p>
<h3 id='ie_test'></h3>
</div>
</div>
<!-- Scripts -->
<script id="responder" type="text/javascript" src="js/onmediaquery.js"></script>
<script language='javascript'>
var ie, t, n;
queries = [
{
context: 'mobile',
match: function() {
ie.innerHTML = 'mobile';
console.log('mobile callback woohoo!');
},
unmatch: function(){
console.log('mobile callback unmatched!');
}
},
{
context: 'skinny',
match: function() {
console.log('skinny callback woohoo!');
ie.innerHTML = 'skinny';
},
unmatch: function(){
console.log('skinny callback unmatched!');
}
},
{
context: ['mobile', 'skinny'],
call_for_each_context: false,
match: function() {
console.log('a callback which spans multiple breakpoints, skinny and mobile!');
// setting 'call_for_each_context' to false means this callback will only fire once across this range of breakpoints
},
unmatch: function() {
console.log('leaving small context!');
}
},
{
context: 'desktop',
match: function() {
console.log('desktop callback woohoo!');
ie.innerHTML = 'desktop';
},
unmatch: function(){
console.log('desktop callback unmatched!');
}
}
];
window.onload = function() {
ie = document.getElementById('ie_test');
MQ.init(queries);
var my_query = MQ.addQuery({
context: ['skinny'],
match: function() {
console.log( 'second skinny callback!' )
}
});
//Add a media query test
var my_query = MQ.addQuery({
context: 'skinny',
match: function() {
console.log( 'second skinny callback!' )
}
});
var callbacklen = MQ.callbacks.length;
//Remove a media query test by reference
MQ.removeQuery( my_query );
if(callbacklen - MQ.callbacks.length === 1){
console.log("RemoveQuery test success");
}else{
console.log("RemoveQuery test failture");
}
}
</script>
<!-- End scripts -->
</body>
</html>