|
| 1 | +import sublinks from './data.js'; |
| 2 | + |
| 3 | +const toggleBtn = document.querySelector('.toggle-btn'); |
| 4 | +const closeBtn = document.querySelector('.close-btn'); |
| 5 | +const sidebarWrapper = document.querySelector('.sidebar-wrapper'); |
| 6 | +const sidebar = document.querySelector('.sidebar-links'); |
| 7 | +const linkBtns = [...document.querySelectorAll('.link-btn')]; |
| 8 | +const submenu = document.querySelector('.submenu'); |
| 9 | +const hero = document.querySelector('.hero'); |
| 10 | +const nav = document.querySelector('.nav'); |
| 11 | +// hide/show sideabar |
| 12 | +toggleBtn.addEventListener('click', () => { |
| 13 | + sidebarWrapper.classList.add('show'); |
| 14 | +}); |
| 15 | +closeBtn.addEventListener('click', () => { |
| 16 | + sidebarWrapper.classList.remove('show'); |
| 17 | +}); |
| 18 | + |
| 19 | +// set sidebar |
| 20 | +sidebar.innerHTML = sublinks |
| 21 | + .map((item) => { |
| 22 | + const { links, page } = item; |
| 23 | + return `<article > |
| 24 | +<h4>${page}</h4> |
| 25 | +<div class="sidebar-sublinks"> |
| 26 | +${links |
| 27 | + .map((link) => { |
| 28 | + return `<a href="${link.url}"><i class="${link.icon}"></i>${link.label}</a>`; |
| 29 | + }) |
| 30 | + .join('')} |
| 31 | +</div> |
| 32 | +</article>`; |
| 33 | + }) |
| 34 | + .join(''); |
| 35 | + |
| 36 | +linkBtns.forEach((btn) => { |
| 37 | + btn.addEventListener('mouseover', function (e) { |
| 38 | + const text = e.currentTarget.textContent; |
| 39 | + const tempBtn = e.currentTarget.getBoundingClientRect(); |
| 40 | + const center = (tempBtn.left + tempBtn.right) / 2; |
| 41 | + const bottom = tempBtn.bottom - 3; |
| 42 | + |
| 43 | + const tempPage = sublinks.find((link) => link.page === text); |
| 44 | + if (tempPage) { |
| 45 | + const { page, links } = tempPage; |
| 46 | + submenu.classList.add('show'); |
| 47 | + submenu.style.left = `${center}px`; |
| 48 | + submenu.style.top = `${bottom}px`; |
| 49 | + // OPTIONAL |
| 50 | + let columns; |
| 51 | + if (links.length <= 2) { |
| 52 | + columns = 'col-2'; |
| 53 | + } |
| 54 | + if (links.length === 3) { |
| 55 | + columns = 'col-3'; |
| 56 | + } |
| 57 | + if (links.length > 3) { |
| 58 | + columns = 'col-4'; |
| 59 | + } |
| 60 | + submenu.innerHTML = ` |
| 61 | + <section> |
| 62 | + <h4>${page}</h4> |
| 63 | + <div class="submenu-center ${columns}"> |
| 64 | + ${links |
| 65 | + .map((link) => { |
| 66 | + return `<a href="${link.url}"><i class="${link.icon}"></i>${link.label}</a>`; |
| 67 | + }) |
| 68 | + .join('')} |
| 69 | + </div> |
| 70 | + </section> |
| 71 | + `; |
| 72 | + } |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +hero.addEventListener('mouseover', function (e) { |
| 77 | + submenu.classList.remove('show'); |
| 78 | +}); |
| 79 | +nav.addEventListener('mouseover', function (e) { |
| 80 | + if (!e.target.classList.contains('link-btn')) { |
| 81 | + submenu.classList.remove('show'); |
| 82 | + } |
| 83 | +}); |
0 commit comments