-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
401 lines (276 loc) · 8.24 KB
/
main.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
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
/*
Dimension by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
*/
(function($) {
var $window = $(window),
$body = $('body'),
$wrapper = $('#wrapper'),
$header = $('#header'),
$footer = $('#footer'),
$main = $('#main'),
$main_articles = $main.children('article');
// Breakpoints.
breakpoints({
xlarge: [ '1281px', '1680px' ],
large: [ '981px', '1280px' ],
medium: [ '737px', '980px' ],
small: [ '481px', '736px' ],
xsmall: [ '361px', '480px' ],
xxsmall: [ null, '360px' ]
});
// Play initial animations on page load.
$window.on('load', function() {
window.setTimeout(function() {
$body.removeClass('is-preload');
}, 100);
});
// Fix: Flexbox min-height bug on IE.
if (browser.name == 'ie') {
var flexboxFixTimeoutId;
$window.on('resize.flexbox-fix', function() {
clearTimeout(flexboxFixTimeoutId);
flexboxFixTimeoutId = setTimeout(function() {
if ($wrapper.prop('scrollHeight') > $window.height())
$wrapper.css('height', 'auto');
else
$wrapper.css('height', '100vh');
}, 250);
}).triggerHandler('resize.flexbox-fix');
}
// Nav.
var $nav = $header.children('nav'),
$nav_li = $nav.find('li');
// Add "middle" alignment classes if we're dealing with an even number of items.
if ($nav_li.length % 2 == 0) {
$nav.addClass('use-middle');
$nav_li.eq( ($nav_li.length / 2) ).addClass('is-middle');
}
// Main.
var delay = 325,
locked = false;
// Methods.
$main._show = function(id, initial) {
var $article = $main_articles.filter('#' + id);
// No such article? Bail.
if ($article.length == 0)
return;
// Handle lock.
// Already locked? Speed through "show" steps w/o delays.
if (locked || (typeof initial != 'undefined' && initial === true)) {
// Mark as switching.
$body.addClass('is-switching');
// Mark as visible.
$body.addClass('is-article-visible');
// Deactivate all articles (just in case one's already active).
$main_articles.removeClass('active');
// Hide header, footer.
$header.hide();
$footer.hide();
// Show main, article.
$main.show();
$article.show();
// Activate article.
$article.addClass('active');
// Unlock.
locked = false;
// Unmark as switching.
setTimeout(function() {
$body.removeClass('is-switching');
}, (initial ? 1000 : 0));
return;
}
// Lock.
locked = true;
// Article already visible? Just swap articles.
if ($body.hasClass('is-article-visible')) {
// Deactivate current article.
var $currentArticle = $main_articles.filter('.active');
$currentArticle.removeClass('active');
// Show article.
setTimeout(function() {
// Hide current article.
$currentArticle.hide();
// Show article.
$article.show();
// Activate article.
setTimeout(function() {
$article.addClass('active');
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
}, 25);
}, delay);
}
// Otherwise, handle as normal.
else {
// Mark as visible.
$body
.addClass('is-article-visible');
// Show article.
setTimeout(function() {
// Hide header, footer.
$header.hide();
$footer.hide();
// Show main, article.
$main.show();
$article.show();
// Activate article.
setTimeout(function() {
$article.addClass('active');
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
}, 25);
}, delay);
}
};
$main._hide = function(addState) {
var $article = $main_articles.filter('.active');
// Article not visible? Bail.
if (!$body.hasClass('is-article-visible'))
return;
// Add state?
if (typeof addState != 'undefined'
&& addState === true)
history.pushState(null, null, '#');
// Handle lock.
// Already locked? Speed through "hide" steps w/o delays.
if (locked) {
// Mark as switching.
$body.addClass('is-switching');
// Deactivate article.
$article.removeClass('active');
// Hide article, main.
$article.hide();
$main.hide();
// Show footer, header.
$footer.show();
$header.show();
// Unmark as visible.
$body.removeClass('is-article-visible');
// Unlock.
locked = false;
// Unmark as switching.
$body.removeClass('is-switching');
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
return;
}
// Lock.
locked = true;
// Deactivate article.
$article.removeClass('active');
// Hide article.
setTimeout(function() {
// Hide article, main.
$article.hide();
$main.hide();
// Show footer, header.
$footer.show();
$header.show();
// Unmark as visible.
setTimeout(function() {
$body.removeClass('is-article-visible');
// Window stuff.
$window
.scrollTop(0)
.triggerHandler('resize.flexbox-fix');
// Unlock.
setTimeout(function() {
locked = false;
}, delay);
}, 25);
}, delay);
};
// Articles.
$main_articles.each(function() {
var $this = $(this);
// Close.
$('<div class="close">Close</div>')
.appendTo($this)
.on('click', function() {
location.hash = '';
});
// Prevent clicks from inside article from bubbling.
$this.on('click', function(event) {
event.stopPropagation();
});
});
// Events.
$body.on('click', function(event) {
// Article visible? Hide.
if ($body.hasClass('is-article-visible'))
$main._hide(true);
});
$window.on('keyup', function(event) {
switch (event.keyCode) {
case 27:
// Article visible? Hide.
if ($body.hasClass('is-article-visible'))
$main._hide(true);
break;
default:
break;
}
});
$window.on('hashchange', function(event) {
// Empty hash?
if (location.hash == ''
|| location.hash == '#') {
// Prevent default.
event.preventDefault();
event.stopPropagation();
// Hide.
$main._hide();
}
// Otherwise, check for a matching article.
else if ($main_articles.filter(location.hash).length > 0) {
// Prevent default.
event.preventDefault();
event.stopPropagation();
// Show article.
$main._show(location.hash.substr(1));
}
});
// Scroll restoration.
// This prevents the page from scrolling back to the top on a hashchange.
if ('scrollRestoration' in history)
history.scrollRestoration = 'manual';
else {
var oldScrollPos = 0,
scrollPos = 0,
$htmlbody = $('html,body');
$window
.on('scroll', function() {
oldScrollPos = scrollPos;
scrollPos = $htmlbody.scrollTop();
})
.on('hashchange', function() {
$window.scrollTop(oldScrollPos);
});
}
// Initialize.
// Hide main, articles.
$main.hide();
$main_articles.hide();
// Initial article.
if (location.hash != ''
&& location.hash != '#')
$window.on('load', function() {
$main._show(location.hash.substr(1), true);
});
})(jQuery);