hugo-theme-terminal/source/js/menu.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-01-31 21:06:54 +00:00
const container = document.querySelector(".container");
2019-01-28 00:11:02 +00:00
const menu = document.querySelector(".menu");
2019-01-31 21:06:54 +00:00
const mobileMenuTrigger = document.querySelector(".menu-trigger");
const desktopMenu = document.querySelector(".menu__inner--desktop");
const desktopMenuTrigger = document.querySelector(".menu__sub-inner-more-trigger");
const menuMore = document.querySelector(".menu__sub-inner-more");
2019-01-28 00:11:02 +00:00
const mobileQuery = getComputedStyle(document.body).getPropertyValue("--phoneWidth");
const isMobile = () => window.matchMedia(mobileQuery).matches;
const isMobileMenu = () => {
2019-01-31 21:06:54 +00:00
mobileMenuTrigger && mobileMenuTrigger.classList.toggle("hidden", !isMobile());
2019-01-28 00:11:02 +00:00
menu && menu.classList.toggle("hidden", isMobile());
2019-01-31 21:06:54 +00:00
menuMore && menuMore.classList.toggle("hidden", !isMobile());
2019-01-28 00:11:02 +00:00
};
2019-01-31 21:06:54 +00:00
// Common
2019-02-01 07:39:12 +00:00
menu && menu.addEventListener("click", e => e.stopPropagation());
menuMore && menuMore.addEventListener("click", e => e.stopPropagation());
2019-01-31 21:06:54 +00:00
2019-01-28 00:11:02 +00:00
isMobileMenu();
2019-01-31 21:06:54 +00:00
document.body.addEventListener("click", () => {
if (!isMobile() && !menuMore.classList.contains("hidden")) {
menuMore.classList.add("hidden");
} else if (isMobile() && !menu.classList.contains("hidden")) {
menu.classList.add("hidden");
}
});
2019-01-28 00:11:02 +00:00
window.addEventListener("resize", isMobileMenu);
2019-01-31 21:06:54 +00:00
// Mobile menu
mobileMenuTrigger &&
mobileMenuTrigger.addEventListener("click", e => {
e.stopPropagation();
menu && menu.classList.toggle("hidden");
});
// Desktop menu
desktopMenuTrigger &&
desktopMenuTrigger.addEventListener("click", e => {
e.stopPropagation();
menuMore && menuMore.classList.toggle("hidden");
if (menuMore.getBoundingClientRect().right > container.getBoundingClientRect().right) {
menuMore.style.left = "auto";
menuMore.style.right = 0;
}
});