-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (122 loc) · 3.83 KB
/
index.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
// SOCIAL NAVIGATION
const socialLinks = document.querySelectorAll('.social-nav a');
const onSocialLinkFocus = (focusedLink) => {
socialLinks.forEach((link) => {
link.style.opacity = 0.25;
});
focusedLink.style.opacity = 1;
};
socialLinks.forEach((link) => {
['focus', 'mouseover'].forEach((event) => {
link.addEventListener(event, () => onSocialLinkFocus(link));
});
});
const onSocialLinkBlur = () => {
socialLinks.forEach((link) => {
link.style.opacity = 1;
});
};
socialLinks.forEach((link) => {
['blur', 'mouseout'].forEach((event) => {
link.addEventListener(event, () => onSocialLinkBlur());
});
});
// HAMBURGER MENU
const hamburgerMenu = document.querySelector('.site-nav');
const trapHamburgerMenuFocus = (e) => {
const tabIsPressed = e.key === 'Tab' || e.keyCode === 9;
const focusableElements = hamburgerMenu.querySelectorAll('a[href], button');
const firstFocusableElement = focusableElements[0];
const lastFocusableElement = focusableElements[focusableElements.length - 1];
if (tabIsPressed) {
// shift + tab
if (e.shiftKey) {
if (document.activeElement === firstFocusableElement) {
lastFocusableElement.focus();
e.preventDefault();
}
} else {
if (document.activeElement === lastFocusableElement) {
firstFocusableElement.focus();
e.preventDefault();
}
}
}
};
const openHamburgerMenu = () => {
const hamburgerMenuCloseButton = document.getElementById(
'hamburgerNavCloseButton'
);
hamburgerMenu.classList.add('-open');
document.body.classList.add('-navOpen');
hamburgerMenuCloseButton.focus();
hamburgerMenu.addEventListener('keydown', (e) => trapHamburgerMenuFocus(e));
};
const closeHamburgerMenu = () => {
hamburgerMenu.classList.remove('-open');
document.body.classList.remove('-navOpen');
hamburgerMenu.removeEventListener('keydown', (e) =>
trapHamburgerMenuFocus(e)
);
};
// SITE NAVIGATION
const siteNavLinks = document.querySelectorAll('.site-nav-links a');
const clearActiveLinkStyles = () => {
siteNavLinks.forEach((link) => link.classList.remove('-active'));
};
const addActiveLinkStyles = (activeLink) => {
activeLink.classList.add('-active');
};
siteNavLinks.forEach((link) =>
link.addEventListener('click', (e) => {
e.preventDefault();
clearActiveLinkStyles();
addActiveLinkStyles(link);
if (hamburgerMenu.classList.contains('-open')) {
closeHamburgerMenu();
}
window.location.href = link.getAttribute('href');
})
);
// COPYRIGHT YEAR
const copyrightYearElements = document.querySelectorAll('.copyrightYear');
const currentYear = new Date().getFullYear();
copyrightYearElements.forEach((element) => {
element.innerHTML = currentYear;
});
// TOP NAVIGATION LINKS
const userIsAtBottomOfPage = () => {
return window.innerHeight + window.pageYOffset >= document.body.offsetHeight;
};
const userIsAtSection = (section) => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
return (
window.pageYOffset >= sectionTop &&
window.pageYOffset < sectionTop + sectionHeight - 1
);
};
const setSectionNavLinkAsActive = (section) => {
const sectionId = section.id === 'hero' ? 'home' : section.id;
const sectionNavLink = document.querySelectorAll(
`.site-nav-links li a[href="#${sectionId}"]`
)[0];
const sectionNavLinkIsNotActive =
!sectionNavLink.classList.contains('-active');
if (sectionNavLinkIsNotActive) {
clearActiveLinkStyles();
addActiveLinkStyles(sectionNavLink);
}
};
window.addEventListener('scroll', () => {
const sections = document.querySelectorAll('section');
if (userIsAtBottomOfPage()) {
setSectionNavLinkAsActive(sections[sections.length - 1]);
return;
}
sections.forEach((section) => {
if (userIsAtSection(section)) {
setSectionNavLinkAsActive(section);
}
});
});