forked from remy/responsivepx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
533 lines (455 loc) · 17.5 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>responsivepx - find that tricky breakpoint</title>
<!-- Feel free to fork on github too: https://github.com/remy/responsivepx -->
<link rel="shortcut icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAANCAYAAACdKY9CAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZpJREFUeNqUUb9LQmEUfb+fPn8gDpFogRCIgoSgS0MgbQ1CEQ3tRQRhQtDa2lTRWHMtRUu0Zf+A4BKCk0RDBJqiPsWnvtc5ouDaB4d73rnnu/e79ym5XO670+m84+yl0+mgqqqbw+HwUcAB3wF/LZVKv9ls9t7n82WlYDC4GAqFdjOZzJosyxuRSOQO3lWCnBpz9NArVSqVc0mS5HA4XNR1/dDlcumO4ywR5NSYo4deka1TqdRxNBq9higCQrVa3aIei8WebdsWAKdWq+XL5fKNkkwmTwzDKPR6PQfVJgVGo5HCOBgMhGl04DmFV1ZwO+52u5cpAvZ4PLYty/qisdlsjjCDBCrS02q14sJ/j4JNrGN9+3NaDx3OSDRNu0AwZgms+FYRRTHv9Xq356vU6/UXRugH8zqeZCgY9qjf7795PJ5LdNKmlVRG6LPKlmmaBaz6SWo0Gj/oUsDwWrfbNbkZbGkCcmrM0UOvFAgEPvGx0m63i6hyNb0wJsipMUcPvYLf77fwnA+2xp9NYN8PiAvElCeYo4fePwEGAFJa2ZbVKf+5AAAAAElFTkSuQmCC">
<style>
body { font-family: sans-serif; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACcAAAAnCAYAAACMo1E1AAAAcUlEQVRYCe3TsQ0AIQzFUGCD7L9jMgIwAJUVOlMiGZ2e/s3M3KPxRETba6vtpQ8P+XEUVTnlqADt3JxyVIB2bk45KkA7N6ccFaDd3PfQ+NVV1esa3flDILYbKaccFaCdm1OOCtDOzSlHBWjn5pSjArQ7Dl0LEgUi4pIAAAAASUVORK5CYII=); margin: 0; padding: 0; }
input { font-size: 16px; padding: 2px; font-family: sans-serif; }
input[type=range] { width: 100px; height: 7px; }
iframe.viewport { background: #fff; position: width: 100%; margin: 34px auto 0 auto; display: block; height: 2000px; -webkit-box-shadow: 0 5px 80px #505050; }
form { height: 30px; overflow: hidden; display: block; background: #333; background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#666666), to(#666666), color-stop(.6,#333)); color: #fff; width: 100%; padding: 2px; position: fixed; top: 0; left: 0; border-bottom: 1px solid rgba(0, 0, 0, .15); }
form {
text-shadow: 0 -1px 1px #000;
}
#info {
width: 50%;
margin: 15% auto 0;
box-shadow: 0 5px 80px #505050;
-webkit-box-shadow: 0 5px 80px #505050;
-moz-box-shadow: 0 5px 80px #505050;
-o-box-shadow: 0 5px 80px #505050;
-ms-box-shadow: 0 5px 80px #505050;
background: #fff;
padding: 40px;
line-height: 1.4em;
}
#info h1 {
margin-top: 0;
}
#info p:last-child {
margin-bottom: 0;
}
#share {
text-decoration: none;
color: #fff;
border: 2px solid transparent;
padding: 2px 4px;
}
#share:hover {
text-decoration: none;
color: #fff;
border: 2px inset rgba(0, 0, 0, .15);
background: rgba(0, 0, 0, 0.1);
border-radius: 3px;
}
#share:active {
background: rgba(0, 0, 0, 0.4);
color: #ddd;
}
#presets {
min-width: 100px;
}
button {
min-width: 25px;
}
</style>
</head>
<body>
<form>
<input placeholder=http:// value=http:// id=url name=url autocomplete autofocus spellcheck=false autocapitalize=off autocorrect=off>
<input type=submit value=Open>
<span title="Because I can't determine programatically whether the scrollbars are visible, you need to check this box yourself to adjust the pixel value to give the real viewport width - rather than the window width">Scrollbar visible? <input type=checkbox id=scrollbars checked></span>
Width: <select id=width step=1 type=range>
<option>10</option>
<option>340</option>
<option>480</option>
<option selected>640</option>
<option>800</option>
<option>960</option>
<option>1024</option>
<option>1920</option>
<option>2048</option>
<option>3000</option>
</select>
<!-- <input type=range id=width step=1 min=10 max=3000 value=640> -->
<input size=4 type=number min=10 step=1 class=width>
Height:
<select id=height step=1 type=range>
<option>10</option>
<option>340</option>
<option selected>480</option>
<option>640</option>
<option>800</option>
<option>960</option>
<option>1024</option>
<option>1920</option>
<option>2048</option>
<option>3000</option>
</select>
<!-- <input type=range step=1 id='height' step=10 min=100 max=3000 value=640> -->
<input size=4 type=number min=10 step=1 class=height>
<button id="presets-add" title="Create a preset from the current values">+</button>
<button id="presets-prev" title="Previous preset (try Ctrl+LeftArrow)" disabled><</button>
<select id="presets" disabled></select>
<button id="presets-next" title="Next preset (try Ctrl+RightArrow)" disabled>></button>
<button id="presets-remove" title="Remove the chosen preset" disabled>-</button>
<a title='Open a new email with the permalink to this breakpoint' href='mailto:?subject=responsivepx.com' id=share>Share</a>
</form>
<div id="info">
<h1>About responsivepx</h1>
<p>Enter the url to your site - local or online: both work - and use the controls to adjust the width and height of your viewport to find exact breakpoint widths in pixels. Then use that information in your media queries to create a <a href="http://www.alistapart.com/articles/responsive-web-design/">responsive design</a>.</p>
<p>Watch the <a target="_new" href="http://www.youtube.com/watch?v=kYpENMubJKQ">video explaining usage</a> and why it was built.</p>
<p>If your site appears with scrollbars, make sure to check the scrollbar visible box to get the right viewport width and height.</p>
<p>Fork the project or log an issue on <a href="http://github.com/remy/responsivepx">github</a></p>
<p>Built by <a href="http://twitter.com/rem">@rem</a></p>
<h3>Support for preset values</h3>
<p>Added by <a href="http://twitter.com/harriha">@harriha</a> (<a href="https://github.com/harriha/responsivepx">fork on GitHub</a>)</p>
<p>You can add desired width+height pairs to the preset list for quick access. Values are stored to localStorage (works fine in modern browsers). Use Ctrl+[LeftArrow|RightArrow] to switch between the presets.</p>
<p>Note: Due to a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=507361">bug</a> in Firefox, localStorage doesn't work if the file is opened locally (<i>file:///...</i>).</p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
/**
* This was from a proposal that James Edwards / Brothercake had during
* 2011 Highland Fling - to use select elements as a starting point for
* the range element - so it's a pretty darn good fit, I put this together
* during his Q&A. In the end I needed to lift the detection code from
* Modernizr - which credit really goes to @miketaylr.
* My code starts from "if (bool) {"
*
* This code is looking for <select type="range"> and will progressively
* enhance to a input[type=range] copying much of the attributes across.
*/
!function () {
var rangeTest = document.createElement('input'),
smile = ':)';
rangeTest.setAttribute('type', 'range');
var bool = rangeTest.type !== 'text';
if (bool) {
rangeTest.style.cssText = 'position:absolute;visibility:hidden;';
rangeTest.value = smile;
if (rangeTest.style.WebkitAppearance !== undefined ) {
document.body.appendChild(rangeTest);
defaultView = document.defaultView;
// Safari 2-4 allows the smiley as a value, despite making a slider
bool = defaultView.getComputedStyle &&
defaultView.getComputedStyle(rangeTest, null).WebkitAppearance !== 'textfield' &&
// Mobile android web browser has false positive, so must
// check the height to see if the widget is actually there.
(rangeTest.offsetHeight !== 0);
document.body.removeChild(rangeTest);
}
} else {
bool = rangeTest.value == smile;
}
// if the input[range] is natively supported, then upgrade the <select type="range">
// into a range element.
if (bool) {
function firstChild(el, nodeName) {
nodeName = nodeName.toUpperCase();
if (el.firstChild.nodeName === nodeName) {
return el.firstChild;
} else {
return el.firstChild.nextSibling;
}
}
function lastChild(el, nodeName) {
nodeName = nodeName.toUpperCase();
if (el.lastChild.nodeName === nodeName) {
return el.lastChild;
} else {
return el.lastChild.previousSibling;
}
}
var selects = document.getElementsByTagName('select'),
i = 0;
for (; i < selects.length; i++) {
if (selects[i].getAttribute('type') == 'range') (function (select) {
var range = document.createElement('input'),
parent = select.parentNode;
range.setAttribute('type', 'range');
// works with the select element removed from the DOM
select = parent.replaceChild(range, select);
range.autofocus = select.autofocus;
range.disabled = select.disabled;
range.autocomplete = select.autocomplete; // eh? how would this even work?
range.className = select.className;
range.id = select.id;
range.style = select.style;
range.tabindex = select.tabindex;
range.title = select.title;
range.min = firstChild(select, 'option').value;
range.max = lastChild(select, 'option').value;
range.value = select.value;
// yeah, this is filth, but it's because getElementsByTagName is
// a live DOM collection, so when we removed the select element
// the selects object reduced in length. Freaky, eh?
i--;
})(selects[i]);
}
}
}();
</script>
<script>
// workout what the dimensions of a scrollbar is for the particular browser.
// It's usually 15px, but who sersiously trusts browsers nowadays?!
function calcScrollbarWidth() {
var el = $('<div style="overflow: scroll;">').appendTo('body')[0];
var offset = el.offsetHeight;
el.parentNode.removeChild(el);
return offset;
}
function newframe(url) {
$('iframe').remove();
$('<iframe frameborder=0 class="viewport" src="' + url + '"></iframe>').css('display', 'none').appendTo('body').one('load', function () {
// this is just to prevent the initial iframe from appearing empty (as it has a shadow)
$(this).css('display', 'block');
});
}
function addhistory() {
if (history.pushState && url) {
clearTimeout(historyTimer);
historyTimer = setTimeout(function () {
history.pushState(null, null, '?' + encodeURIComponent(url) + '#' + $w.val() + 'x' + $h.val() + (useoffset ? '&scrollbars' : ''));
}, 500);
}
}
function size(w, h, nohistory) {
// ensure the values are numbers
w *= 1;
h *= 1;
if (!nohistory) addhistory();
// adjust for whether we have the scrollbars visible
$('iframe').width(w + (useoffset ? offset : 0)).height(h + (useoffset ? offset : 0));
}
// Going bit OOP with the presets
function Preset(w, h) {
var self = {
width: w || 0,
height: h || 0,
val: function() { return self.width + "x" + self.height; },
compare: function(preset) { return self.width - preset.width; },
equals: function(preset) { return self.val() === preset.val(); }
}
return self;
}
function addPreset(width, height) {
var preset = new Preset(width, height);
// We don't want to have the same preset twice
var i = presets.length;
while(i--) {
if(presets[i].equals(preset)) return;
}
// Add to the end, the order is handled later on
presets.push(preset);
var opt = document.createElement("option");
opt.value = preset.val();
opt.innerHTML = preset.val() + "px";
opt.selected = true;
if(presets.length > 1) {
// Sort presets, find the correct index for the new item and add it to the list
presets = presets.sort(function(a, b) { return a.width - b.width; });
i = presets.length;
while(--i && presets[i].compare(preset) > 0);
!i ? $("#presets").prepend(opt) : $(opt).insertAfter($("#presets").children()[i - 1]);
}
else {
$("#presets").append(opt);
}
storePresets();
updatePresetControls();
}
function storePresets() {
if(hasLocalStorage()) {
localStorage.setItem("presets", JSON.stringify(presets));
}
}
// The approach used in Modernizr
function hasLocalStorage() {
try {
return !!localStorage.getItem;
} catch(e) {
return false;
}
};
function updatePresetControls() {
if(presets.length == 1) {
$.each(["#presets", "#presets-remove"], function(i, val) {
$(val).attr("disabled", false);
})
$.each(["#presets-prev", "#presets-next"], function(i, val) {
$(val).attr("disabled", true);
})
}
else if(presets.length > 1) {
$.each(["#presets", "#presets-remove", "#presets-prev", "#presets-next"], function(i, val) {
$(val).attr("disabled", false);
})
}
else {
$.each(["#presets", "#presets-remove", "#presets-prev", "#presets-next"], function(i, val) {
$(val).attr("disabled", true);
})
}
}
function refresh(width, height, nohistory) {
if(width) {
$('#width').val(width);
$('.width').val(width);
}
if(height) {
$('#height').val(height);
$('.height').val(height);
}
size($w.val(), $h.val(), nohistory);
}
var win = $('iframe'),
$w = $('#width'),
$h = $('#height'),
offset = calcScrollbarWidth(),
useoffset = $('#scrollbars').is(':checked'),
$url = $('#url'),
$hide = $('#hide'),
$form = $('form'),
$range = $('[type=range]'), // done on purpose
historyTimer = null,
url = '',
presets = [];
$("#presets").bind("change", function(e, nohistory) {
refresh(this.value.split("x")[0], this.value.split("x")[1], nohistory);
});
$("#presets-add").click(function(e) {
e.preventDefault();
addPreset($w.val(), $h.val());
});
$("#presets-remove").click(function(e) {
e.preventDefault();
var current = $("#presets option:selected");
if(current.length) {
presets.splice(current.index(), 1);
current.remove();
storePresets();
$("#presets").trigger("change", [ true ]); // Retain history
updatePresetControls();
}
});
$("#presets-prev").click(function(e) {
e.preventDefault();
var current = $("#presets option:selected");
if(current.prev().length) {
$("#presets").val(current.prev().val()).trigger("change");
}
});
$("#presets-next").click(function(e) {
e.preventDefault();
var current = $("#presets option:selected");
if(current.next().length) {
$("#presets").val(current.next().val()).trigger("change");
}
});
/*
* Navigating between the presets using keyboard: Ctrl+LeftArrow
* selects the previous preset, Ctrl+RightArrow the next one.
*/
$(document).keydown(function(e) {
if(e.ctrlKey) {
if(e.which == '37') { // Left arrow
$("#presets-prev").click();
}
else if(e.which == '39') { // Right arrow
$("#presets-next").click();
}
}
});
$form.submit(function (e, nohistory) {
e.preventDefault();
$('#info').remove();
// allows me to keep things nice and friendly on the url
var newurl = url = $url.val();
if (newurl.indexOf('://') === -1) newurl = 'http://' + newurl;
if (url.indexOf('http://') === 0) url = url.substr(7); // strip the http:// part - don'ts needs'it
newframe(newurl);
$range.trigger('change', nohistory); // I realise this triggers the size function twice - I'm just lazy :)
});
$('#share').click(function () {
var copy = '';
if (!url) {
copy = 'Check out this tool for finding breakpoints in responsive designs:\n\nhttp://' + location.host;
} else {
copy = 'See these breakpoints in ' + url + ':\n\nhttp://' + location;
}
this.href = 'mailto:?body=' + encodeURIComponent(copy);
});
$('#scrollbars').click(function () {
useoffset = this.checked;
var w = $('iframe').width() - (useoffset ? offset : 0),
h = $('iframe').height() - (useoffset ? offset : 0);
addhistory();
$('input[type=number], [type=range]').each(function () {
if (this.id == 'width' || this.className == 'width') {
this.value = w;
} else {
this.value = h;
}
});
});
$('input[type=number]').bind('input', function (e, nohistory) {
if(this.className == "width") refresh(this.value, undefined, nohistory);
else refresh(undefined, this.value, nohistory);
}).bind('keydown', function (e) {
if (e.shiftKey) {
this.step = 10;
} else { // I could test here, but the cost of the DOM action would outweigh the cost of the optimisation
this.step = 1;
}
});
$range.bind('change', function (e, nohistory) {
if(this.id == "width") refresh(this.value, undefined, nohistory);
else refresh(undefined, this.value, nohistory);
}).bind('click', function () {
$(this).focus();
});
if (window.top !== window.self) window.top.location.replace(window.self.location.href);
// Load presets from localStorage
if(hasLocalStorage()) {
var arr = JSON.parse(localStorage.getItem("presets")) || [];
for(var i = 0; i < arr.length; i++) {
addPreset(arr[i].width, arr[i].height);
}
updatePresetControls();
}
if (history.pushState) {
window.onpopstate = function () {
// read the location and reload
var settings = location.hash.substr(1).split('&');
if (settings.length == 2) {
useoffset = true;
$('#scrollbars').attr('checked', 'checked');
} else if (location.hash) {
useoffset = false;
$('#scrollbars').removeAttr('checked');
}
if (settings[0].indexOf('x') !== -1) { // if it actually has some value
var wh = settings[0].split('x');
$w.val(wh[0]);
$h.val(wh[1]);
// Try to set the presets select to reflect the current state
$("#presets").val(wh[0] + "x" + wh[1]);
}
$('input.width').val($w.val());
$('input.height').val($h.val());
var newurl = decodeURIComponent(location.search.substr(1));
if (newurl !== url) {
$url.val(newurl);
$form.trigger('submit', [ true ]); // tell the form not to add to the history
} else {
$range.trigger('change', [ true ]);
}
};
}
</script>
</body>
</html>