gpt4 book ai didi

javascript - 单击黑暗区域时如何关闭侧面菜单?

转载 作者:行者123 更新时间:2023-11-28 16:52:15 25 4
gpt4 key购买 nike

我构建了一个简单的侧面导航。如果运行该代码片段并将窗口调整为较小的尺寸,您将看到一个红色方 block 。如果单击它,则会打开菜单。

菜单打开得很好,但是我想在单击黑暗区域而不是 X 时关闭菜单。我尝试向主体本身添加一个“click”事件监听器并删除“is-open”类,但是没用。我花了几个小时思考可能是什么问题,最后决定在这里发帖并征求您的建议。

"use strict";

const menuToggle = document.querySelector(".menu-toggle");
const menuClose = document.querySelector(".menu-close");
const nav = menuToggle.parentElement;

menuToggle.addEventListener("click", event => {
event.preventDefault();
nav.classList.add("is-open");
document.body.style.backgroundColor = "rgba(0,0,0,0.5)";
});

menuClose.addEventListener("click", event => {
event.preventDefault();
menuToggle.nextElementSibling.style.width = null;
document.body.style.backgroundColor = null;
nav.classList.remove("is-open");
});
:root {
box-sizing: border-box;
}

*, *::before, *::after {
box-sizing: inherit;
}

body {
margin: 0;
padding: 0;
}

.menu-toggle {
width: 40px;
height: 40px;
border: 1px solid red;
cursor: pointer;
}

.menu-container {
position: absolute;
background: lightskyblue;
height: 100vh;
width: 0;
transition: width 0.4s ease-in-out;
top: 0;
left: 0;
overflow: auto;
z-index: 1;
}

.menu-close {
position: absolute;
right: 1em;
}

.nav-menu {
list-style: none;
padding-left: 0;
margin: 50px 0 0 0;
}

.nav-menu > li + li {
border-top: 1px solid #fff;
}

.nav-menu > li > a {
display: block;
color: #000;
padding: 0.8em 1em;
font-size: 1.1rem;
text-decoration: none;
text-transform: uppercase;
}


.nav.is-open .menu-container {
width: 200px;
}

.menu-close::before {
content: "\00d7";
font-size: 2.6rem;
}

/*@media screen and (min-width: 37.5em) {*/
@media screen and (min-width: 40.5em) {
body {
background: #fff !important;
}

.menu-toggle {
display: none;
}

.nav.is-open .menu-container {
width: auto;
height: auto;
}

.menu-container {
position: initial;
height: auto;
width: auto;
overflow: hidden;

}

.menu-close {
display: none;
}

.nav-menu {
display: flex;
position: static;
justify-content: center;
margin: 0;
}

.nav-menu > li {
margin-left: 1em;
}

.nav-menu > li + li {
border-top: initial;
}
}
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="hamburgerside.css">
<title>Hamburger Menu Side</title>
</head>
<body>
<nav class="nav">
<div class="menu-toggle">
<span class="menu-toggle__linecenter"></span>
</div>

<div class="menu-container">
<span class="menu-close"></span>
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Menu Item 1</a></li>
<li><a href="#">Menu Item 2</a></li>
<li><a href="#">Menu Item 3</a></li>
</ul>
</div>
</nav>

<main>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Accusamus accusantium aliquid consequatur facere illum
incidunt magnam magni maiores nam neque numquam omnis
perferendis porro quae quibusdam, quos sed tenetur ullam.
</p>
</main>
<script src="hamburgerside.js"></script>
</body>
</html>

最佳答案

因此,如果我正确理解您要执行的操作,则您将需要使用 document 而不是 body 来进行点击事件。您还需要使用 event.stopImmediatePropagation() 而不是 event.preventDefault() ,后者将允许第一次点击,但不允许第二次点击。同样在这种情况下,您只需要在单击菜单以外的任何地方运行单击事件。

注意:我必须删除您的媒体查询,因为它阻止显示切换菜单。

"use strict";

const menuToggle = document.querySelector(".menu-toggle");
const menuClose = document.querySelector(".menu-close");
const nav = menuToggle.parentElement;

menuToggle.addEventListener("click", event => {
event.stopImmediatePropagation();

nav.classList.add("is-open");
document.body.style.backgroundColor = "rgba(0,0,0,.5)";
});

document.addEventListener("click", event => {
if (nav.classList.contains("is-open") && !event.target.classList.contains("nav-menu")) {
menuToggle.nextElementSibling.style.width = null;
document.body.style.backgroundColor = "#fff";
nav.classList.remove("is-open");
}
});
:root {
box-sizing: border-box;
}

*, *::before, *::after {
box-sizing: inherit;
}

body {
margin: 0;
padding: 0;
}

.menu-toggle {
width: 40px;
height: 40px;
border: 1px solid red;
cursor: pointer;
}

.menu-container {
position: absolute;
background: lightskyblue;
height: 100vh;
width: 0;
transition: width 0.4s ease-in-out;
top: 0;
left: 0;
overflow: auto;
z-index: 1;
}

.menu-close {
position: absolute;
right: 1em;
}

.nav-menu {
list-style: none;
padding-left: 0;
margin: 50px 0 0 0;
}

.nav-menu > li + li {
border-top: 1px solid #fff;
}

.nav-menu > li > a {
display: block;
color: #000;
padding: 0.8em 1em;
font-size: 1.1rem;
text-decoration: none;
text-transform: uppercase;
}


.nav.is-open .menu-container {
width: 200px;
}

.menu-close::before {
content: "\00d7";
font-size: 2.6rem;
}

}
<nav class="nav">
<div class="menu-toggle">
<span class="menu-toggle__linecenter"></span>
</div>

<div class="menu-container">
<span class="menu-close"></span>
<ul class="nav-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Menu Item 1</a></li>
<li><a href="#">Menu Item 2</a></li>
<li><a href="#">Menu Item 3</a></li>
</ul>
</div>
</nav>

<main>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Accusamus accusantium aliquid consequatur facere illum
incidunt magnam magni maiores nam neque numquam omnis
perferendis porro quae quibusdam, quos sed tenetur ullam.
</p>
</main>

关于javascript - 单击黑暗区域时如何关闭侧面菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59774680/

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