forked from JoshBarr/on-media-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
198 lines (166 loc) · 4.99 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
<!doctype html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='css/style.css'>
<title>onMediaQuery - Responsive JS</title>
<style>
body:after {
content: 'mobile';
display: none;
}
@media screen and (min-width: 35em) {
body:after {
content: 'skinny'
}
}
@media screen and (min-width: 56em) {
body:after {
content: 'wide-screen'
}
}
</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'>
<style>
body:after {
content: 'mobile';
display: none; /* comment this line for debugging purposes */
}
@media screen and (min-width: 35em) {
body:after {
content: 'skinny'
}
}
@media screen and (min-width: 56em) {
body:after {
content: 'wide-screen'
}
}
</style>
</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',
callback: function() {
console.log('Mobile callback. Maybe hook up some tel: numbers?');
// Your mobile specific logic can go here.
}
},
{
context: 'skinny',
callback: function() {
console.log('skinny callback! Swap the class on the body element.');
// Your tablet specific logic can go here.
}
},
{
context: 'wide-screen',
callback: function() {
console.log('wide-screen 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',
callback: 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>581 bytes</strong> minified and gzipped, well worth including in your next project.
</p>
<h3 id='ie_test'></h3>
<p id='dump'></p>
</div>
</div>
<!-- Scripts -->
<script type="text/javascript" src="js/onmediaquery.min.js"></script>
<script language='javascript'>
var ie = document.getElementById('ie_test');
queries = [
{
context: 'mobile',
callback: function() {
ie.innerHTML = 'mobile';
console.log('mobile callback woohoo!');
}
},
{
context: 'skinny',
callback: function() {
console.log('skinny callback woohoo!');
ie.innerHTML = 'skinny';
}
},
{
context: 'wide-screen',
callback: function() {
console.log('widescreen callback woohoo!');
ie.innerHTML = 'desktop';
}
}
];
MQ.init(queries);
</script>
<!-- End scripts -->
</body>
</html>