-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnav-offcanvas.js
More file actions
71 lines (61 loc) · 2.11 KB
/
Copy pathnav-offcanvas.js
File metadata and controls
71 lines (61 loc) · 2.11 KB
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
/*
* Off-canvas pattern for navigation.
*
* Dependancies:
*
*
* Author:
* James Taylor
*
* Date:
* 23/01/2015
*
* HTML Pattern:
<div class="js-container">
<p class="skip-to-nav"><a href="#global-nav">Menu</a></p>
<nav id="global-nav" class="nav-global-primary">
... navigation html ...
</nav>
</div>
*
*/
(function (win, doc) {
'use strict';
if (!doc.querySelector || !win.addEventListener) {
console.log('This browser don\'t cut the Mustard - Exiting off-canvas nav');
return;
}
var menuActivated = false, // so skiplink event is only applied once
configureMenu = function () {
var toggleclass = 'slid',
reg = new RegExp('(\\s|^)' + toggleclass + '(\\s|$)'),
page = doc.querySelector('.js-container'),
primary = doc.querySelector('.nav-global-primary'),
skiplink = doc.querySelector('.skip-to-nav'),
newnav = doc.createElement('div'),
mobileBreakPoint = 623,
togglePage = function(e) {
e.preventDefault();
if (!page.className.match(reg)) {
page.className += ' ' + toggleclass;
} else {
page.className = page.className.replace(reg, '');
}
};
if (doc.documentElement.clientWidth < mobileBreakPoint || mobileBreakPoint === null) {
// If the viewport is small enough, use the off-canvas pattern for navigation
if (!page || !primary || !skiplink) {
return;
}
if(!menuActivated) {
skiplink.addEventListener('click', togglePage, false);;
menuActivated = true;
}
newnav.appendChild(primary);
newnav.className = 'js-offcanvas';
skiplink.className = skiplink.className + ' persist';
doc.body.appendChild(newnav);
}
};
configureMenu();
}(this, this.document));