-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
67 lines (56 loc) · 1.6 KB
/
logic.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
const closeButtons = document.querySelectorAll('.close');
const sideBar = document.querySelector('#sidebar');
const tabs = document.querySelectorAll('.tab');
const sidebarContents = document.querySelectorAll('.sidebar-contents');
const toggleSidebar = () => {
const currentLeft = sideBar.style.left;
const width = sideBar.offsetWidth;
sideBar.style.left = currentLeft < 0 ? 0 : -width;
};
const openSidebar = () => {
sideBar.style.left = 0;
Array.from(closeButtons).forEach(b => {
b.style.display = 'flex';
});
};
const closeSidebar = () => {
sideBar.style.left = -sideBar.offsetWidth;
Array.from(closeButtons).forEach(b => {
b.style.display = 'none';
});
setTabActive(null);
};
const setSidebarContent = key => {
let elem = document.querySelector(`#${key}`);
clearAll();
elem.style.display = 'flex';
setTabActive(key);
openSidebar();
};
const clearAll = () => {
Array.from(sidebarContents).forEach(c => {
c.style.display = 'none';
});
};
const setTabActive = tag => {
Array.from(tabs).forEach(tab => {
if (tab.dataset.tag === tag) {
tab.classList.add('active');
} else {
tab.classList.remove('active');
}
});
};
Array.from(closeButtons).forEach(b => {
b.addEventListener('click', e => closeSidebar());
b.addEventListener('touchstart', () => closeSidebar());
});
Array.from(tabs).forEach(tab => {
tab.addEventListener('click', () => setSidebarContent(tab.dataset.tag));
tab.addEventListener('touchstart', () => setSidebarContent(tab.dataset.tag));
});
window.onload = () => {
if (window.innerWidth >= 1400) {
setSidebarContent('bio');
}
};