forked from HubSpot/Memberships-webinar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
72 lines (54 loc) · 2.45 KB
/
module.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
// Website header variables
var menuParentItems = document.querySelectorAll('.menu--desktop .menu__item--has-submenu');
var childToggle = document.querySelectorAll('.menu--mobile .menu__child-toggle');
// Desktop menu
if (menuParentItems) {
Array.prototype.forEach.call(menuParentItems, function(el){
// Menu item variables
var childToggle = el.querySelector('.menu__child-toggle');
// Handles hover over
el.addEventListener('mouseover', function(){
this.classList.add('menu__item--open');
this.querySelector('a').setAttribute('aria-expanded', 'true');
this.querySelector('button').setAttribute('aria-expanded', 'true');
});
// Handles hover out
el.addEventListener('mouseout', function(){
document.querySelector('.menu__item--open > a').setAttribute('aria-expanded', 'false');
document.querySelector('.menu__item--open > button').setAttribute('aria-expanded', 'false');
document.querySelector('.menu__item--open').classList.remove('menu__item--open');
});
// Handles toggle of submenus
childToggle.addEventListener('click', function(){
if (this.parentNode.classList.contains('menu__item--open')) {
this.parentNode.classList.remove('menu__item--open');
this.parentNode.querySelector('a').setAttribute('aria-expanded', 'false');
this.parentNode.querySelector('button').setAttribute('aria-expanded', 'false');
}
else {
this.parentNode.classList.add('menu__item--open');
this.parentNode.querySelector('a').setAttribute('aria-expanded', 'true');
this.parentNode.querySelector('button').setAttribute('aria-expanded', 'true');
}
});
});
}
// Mobile menu
// Handles toggle of submenus
if (childToggle) {
Array.prototype.forEach.call(childToggle, function(el){
el.addEventListener('click', function(){
this.classList.toggle('menu__child-toggle--open');
if (this.parentNode.classList.contains('menu__item--open')) {
this.parentNode.classList.remove('menu__item--open');
this.parentNode.querySelector('a').setAttribute('aria-expanded', 'false');
this.parentNode.querySelector('button').setAttribute('aria-expanded', 'false');
}
else {
this.parentNode.classList.add('menu__item--open');
this.parentNode.querySelector('a').setAttribute('aria-expanded', 'true');
this.parentNode.querySelector('button').setAttribute('aria-expanded', 'true');
}
});
});
}