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

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-02-01 00:06:54 +03:00
const container = document.querySelector(".container");
2019-01-28 03:11:02 +03:00
const menu = document.querySelector(".menu");
2019-02-01 00:06:54 +03: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 03:11:02 +03:00
const mobileQuery = getComputedStyle(document.body).getPropertyValue("--phoneWidth");
const isMobile = () => window.matchMedia(mobileQuery).matches;
const isMobileMenu = () => {
2019-02-01 00:06:54 +03:00
mobileMenuTrigger && mobileMenuTrigger.classList.toggle("hidden", !isMobile());
2019-01-28 03:11:02 +03:00
menu && menu.classList.toggle("hidden", isMobile());
2019-02-01 00:06:54 +03:00
menuMore && menuMore.classList.toggle("hidden", !isMobile());
2019-01-28 03:11:02 +03:00
};
2019-02-01 00:06:54 +03:00
// Common
menu.addEventListener("click", e => e.stopPropagation());
menuMore.addEventListener("click", e => e.stopPropagation());
2019-01-28 03:11:02 +03:00
isMobileMenu();
2019-02-01 00:06:54 +03:00
document.body.addEventListener("click", () => {
if (!isMobile() && !menuMore.classList.contains("hidden")) {
console.log("desktop");
menuMore.classList.add("hidden");
} else if (isMobile() && !menu.classList.contains("hidden")) {
console.log("mobile");
menu.classList.add("hidden");
}
});
2019-01-28 03:11:02 +03:00
window.addEventListener("resize", isMobileMenu);
2019-02-01 00:06:54 +03: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;
}
});