odin-css-exercises/animation/03-dropdown-menu/script.js

15 lines
No EOL
473 B
JavaScript

const dropdownContainer = document.querySelector(".dropdown-container");
const menuTitle = document.querySelector(".menu-title");
const dropdownMenu = document.querySelector(".dropdown-menu");
menuTitle.addEventListener("click", (e) => {
if (e.target === e.currentTarget) {
dropdownMenu.classList.toggle("visible");
}
})
window.addEventListener("click", (e) => {
if (!dropdownContainer.contains(e.target)) {
dropdownMenu.classList.remove("visible")
}
})