gpt4 book ai didi

javascript - 如何在单击时关闭此菜单?

转载 作者:行者123 更新时间:2023-12-04 01:14:12 28 4
gpt4 key购买 nike

到目前为止,我已经成功地通过菜单图标打开和关闭菜单。但我想在任何点击时关闭它。我不知所措如何解决这个问题。我试图在 window/document/header/nav 上添加一个点击事件监听器......但它似乎立即关闭菜单,然后才选择任何东西。你会如何处理这个问题?

let nav = document.querySelector(".header__nav");

function toggleNavOn() {
if (nav.style.visibility === "") {
nav.style.visibility = "visible";
} else {
nav.style.visibility = "";
}
navToggle.classList.toggle("header__nav_toggle--toggled")
}

let navToggle = document.querySelector(".header__nav_toggle");
navToggle.addEventListener("click", toggleNavOn);
* {
margin: 0;
padding: 0;
}

:root {
--color-white: #fffff0;
--color-black: #000;
--color-primary: #5f5e63;
--color-primary-light: #8c8b90;
--color-primary-dark: #353439;
--color-primary-dark-2: #131313;
--color-secondary: #ca3722;
--color-secondary-light: #ff6a4c;
--color-secondary-dark: #920000;
--cells: 40;
--cellSize: calc(100vw / var(--cells));
}

.header {
-ms-grid-column: 1;
-ms-grid-column-span: 40;
grid-column: 1 / span 40;
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 10;
margin: 0 auto;
-webkit-box-shadow: 0 0.9rem 1.8rem rgba(0, 0, 0, 0.25), 0 0.625rem 0.625rem rgba(0, 0, 0, 0.22);
box-shadow: 0 0.9rem 1.8rem rgba(0, 0, 0, 0.25), 0 0.625rem 0.625rem rgba(0, 0, 0, 0.22);
padding: 1.1rem 1.4rem;
width: 100%;
background: var(--color-primary-dark-2);
}

.header__container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
z-index: 11;
}

.header__container_logo {
z-index: 1;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
}

.header__logo_link {
-webkit-box-flex: 2;
-ms-flex-positive: 2;
flex-grow: 2;
}

.header__logo_img {
margin: 0 auto;
max-height: 1.3rem;
width: 2.6rem;
}

.header__nav_toggle {
display: block;
height: 24px;
width: 24px;
}

.header__nav_toggle span {
display: inline-block;
height: 100%;
width: 100%;
}

.header__nav_toggle span::before {
content: "";
position: relative;
display: block;
top: 6px;
height: 2px;
width: 100%;
background-color: var(--color-white);
-webkit-transition: all 0.15s ease-in-out;
transition: all 0.15s ease-in-out;
}

.header__nav_toggle span::after {
content: "";
position: relative;
display: block;
top: 12px;
height: 2px;
width: 100%;
background-color: var(--color-white);
-webkit-transition: all 0.15s ease-in-out;
transition: all 0.15s ease-in-out;
}

.header__nav_toggle--toggled span::before {
top: 13px;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transition: all 0.15s ease-in-out;
transition: all 0.15s ease-in-out;
}

.header__nav_toggle--toggled span::after {
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transition: all 0.15s ease-in-out;
transition: all 0.15s ease-in-out;
}

.header__nav_link:hover,
.header__logo_link:hover,
.header__logo_img:hover,
.header__nav_toggle:hover {
opacity: 0.8;
-webkit-transition: all 0.1s ease;
transition: all 0.1s ease;
}

.header__nav_link {
display: none;
color: var(--color-white);
font-size: 0.9rem;
font-weight: 400;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 0.1rem;
}

.header__nav {
visibility: hidden;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
margin-bottom: -0.9rem;
padding-top: 16px;
text-align: center;
background-color: var(--color-primary-dark);
}

.header__nav_link {
display: block;
border-top: 2px solid;
-o-border-image: linear-gradient(to right, rgba(0, 0, 0, 0), var(--color-primary-dark), rgba(0, 0, 0, 0)) 1;
border-image: -webkit-gradient(linear, left top, right top, from(rgba(0, 0, 0, 0)), color-stop(var(--color-primary-dark)), to(rgba(0, 0, 0, 0))) 1;
border-image: linear-gradient(to right, rgba(0, 0, 0, 0), var(--color-primary-dark), rgba(0, 0, 0, 0)) 1;
padding: 0.5rem 0;
}
<header class="header">
<div class="header__container">
<div class="header__container_logo">
<a class="header__logo_link" href="#"><img class="header__logo_img" src="img/vtol-logo.svg" alt="Vtol Logo"></a>
<a class="header__nav_toggle" href="javascript:void(0);">
<span></span>
</a>
</div>

<nav class="header__nav" id="nav-bar">
<a class="header__nav_link" href="#video-container">Watch</a>
<a class="header__nav_link" href="#features">Features</a>
<a class="header__nav_link" href="#about">About</a>
</nav>

</div>
</header>

最佳答案

您可以查看“聚焦”和“模糊”事件。

https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event

关于javascript - 如何在单击时关闭此菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60902416/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com