-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (75 loc) · 2.22 KB
/
app.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
import sublinks from './data.js';
const toggleBtn = document.querySelector('.toggle-btn');
const closeBtn = document.querySelector('.close-btn');
const sidebarWrapper = document.querySelector('.sidebar-wrapper');
const sidebar = document.querySelector('.sidebar-links');
const linkBtns = [...document.querySelectorAll('.link-btn')];
const submenu = document.querySelector('.submenu');
const hero = document.querySelector('.hero');
const nav = document.querySelector('.nav');
// hide/show sideabar
toggleBtn.addEventListener('click', () => {
sidebarWrapper.classList.add('show');
});
closeBtn.addEventListener('click', () => {
sidebarWrapper.classList.remove('show');
});
// set sidebar
sidebar.innerHTML = sublinks
.map((item) => {
const { links, page } = item;
return `<article >
<h4>${page}</h4>
<div class="sidebar-sublinks">
${links
.map((link) => {
return `<a href="${link.url}"><i class="${link.icon}"></i>${link.label}</a>`;
})
.join('')}
</div>
</article>`;
})
.join('');
linkBtns.forEach((btn) => {
btn.addEventListener('mouseover', function (e) {
const text = e.currentTarget.textContent;
const tempBtn = e.currentTarget.getBoundingClientRect();
const center = (tempBtn.left + tempBtn.right) / 2;
const bottom = tempBtn.bottom - 3;
const tempPage = sublinks.find((link) => link.page === text);
if (tempPage) {
const { page, links } = tempPage;
submenu.classList.add('show');
submenu.style.left = `${center}px`;
submenu.style.top = `${bottom}px`;
// OPTIONAL
let columns = 'col-2';
if (links.length === 3) {
columns = 'col-3';
}
if (links.length > 3) {
columns = 'col-4';
}
submenu.innerHTML = `
<section>
<h4>${page}</h4>
<div class="submenu-center ${columns}">
${links
.map((link) => {
return `<a href="${link.url}"><i class="${link.icon}"></i>${link.label}</a>`;
})
.join('')}
</div>
</section>
`;
}
});
});
hero.addEventListener('mouseover', function (e) {
submenu.classList.remove('show');
});
nav.addEventListener('mouseover', function (e) {
if (!e.target.classList.contains('link-btn')) {
submenu.classList.remove('show');
}
});