-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
128 lines (103 loc) · 3.13 KB
/
scripts.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
window.addEventListener("DOMContentLoaded", () => {
const tabs = document.querySelectorAll('[role="tab"]');
const tabList = document.querySelector('[role="tablist"]');
const panelsList = document.querySelector('#panels');
const planetImageInternal = document.querySelector(".planet-image__internal");
// remove preload class that prevents anim
document.querySelector(".planet-image__geology").classList.remove("preload");
// Hide all but first tab panel
panelsList
.querySelectorAll('[role="tabpanel"]')
.forEach((p,index) => index > 0 ? p.classList.add("hidden") : null);
fadeOut(planetImageInternal);
// Add a click event handler to each tab
tabs.forEach(tab => {
tab.addEventListener("click", changeTabs);
});
// Enable arrow navigation between tabs in the tab list
let tabFocus = 0;
tabList.addEventListener("keydown", e => {
// Move right
if (e.keyCode === 39 || e.keyCode === 37) {
tabs[tabFocus].setAttribute("tabindex", -1);
if (e.keyCode === 39) {
tabFocus++;
// If we're at the end, go to the start
if (tabFocus >= tabs.length) {
tabFocus = 0;
}
// Move left
} else if (e.keyCode === 37) {
tabFocus--;
// If we're at the start, move to the end
if (tabFocus < 0) {
tabFocus = tabs.length - 1;
}
}
tabs[tabFocus].setAttribute("tabindex", 0);
tabs[tabFocus].focus();
}
});
});
function changeTabs(e) {
const target = e.target;
const parent = target.parentNode;
const panelsList = document.querySelector('#panels');
const panel = panelsList
.querySelector(`#panel__${target.dataset.info}`);
const planetImageInternal = document.querySelector(".planet-image__internal");
const planetImageGeology = document.querySelector(".planet-image__geology");
// Remove all current selected tabs
parent
.querySelectorAll('[aria-selected="true"]')
.forEach(t => t.setAttribute("aria-selected", false));
// Set this tab as selected
target.setAttribute("aria-selected", true);
// Hide all tab panels
panelsList
.querySelectorAll('[role="tabpanel"]')
.forEach(p => p.classList.add("hidden"));
show(panel);
if (target.id === "tab__structure") {
fadeIn(planetImageInternal)
}
else {
fadeOut(planetImageInternal)
}
if (target.id === "tab__geology") {
fadeIn(planetImageGeology)
}
else {
fadeOut(planetImageGeology);
}
}
function show(element) {
element.classList.remove("hidden");
}
function hide(element) {
element.classList.add("hidden");
}
function fadeOut(element) {
element.classList.add("invisible");
}
function fadeIn(element) {
element.classList.remove("invisible");
}
function popIn(element) {
element.classList.add("popped")
element.classList.remove("unpopped")
}
function popOut(element) {
element.classList.remove("popped");
element.classList.add("unpopped");
}
function toggleMobileMenu() {
const mobileNav = document.querySelector(".mobile-nav");
console.log("bum")
if (mobileNav.classList.contains("hidden")) {
mobileNav.classList.remove("hidden")
}
else {
mobileNav.classList.add("hidden")
}
}