我正在构建一个带有响应菜单的网站,该菜单当前具有4个菜单项,其中“关于”菜单项还有一个包含4个子菜单的子菜单。当我在手机上查看该菜单并单击“关于'子菜单显示,但是它将主菜单项向下推,无法访问它们,这意味着我无法向下滚动。
大多数代码来自一个Youtube视频,我的确对它有所了解,但是我添加了子菜单部分,但还没有弄清楚。感谢您的帮助。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>navbar demo 4</title>
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<header>
<nav class="navbar">
<div class="logo-brand">
<div class="logo">
<img src="assets/img/physics.png" width="75" height="75" alt="physics" />
</div>
<div class="brand">
<a href="#">Science & Math with Dave</a>
</div>
</div>
<ul>
<li><a href="#">About</a>
<ul>
<li><a href="#">Bio</a></li>
<li><a href="#">Skills</a></li>
<li><a href="#">Education</a></li>
<li><a href="#">Resume</a></li>
</ul>
</li>
<li><a href="#">Blog</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Contact</a></li>
</ul>
<button class="navbar-toggler">
<span></span>
</button>
</nav>
</header>
<script type="text/javascript" src="assets/js/main.js"></script>
</body>
</html>
CSS
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
font-family: Calibri, Arial, sans-serif;
background-color: rgb(235, 227, 227);
color: #333;
}
header {
width: 100%;
height: 100vh;
background: url(../img/TheRoad.jpg) no-repeat center center;
background-color: black;
background-size: cover;
position: fixed;
top: 0px;
left: 0px;
}
.logo,
.brand {
float: left;
height: inherit;
line-height: inherit;
padding: 0 30px;
font-size: 22px;
font-weight: 400;
letter-spacing: 4px;
}
.navbar {
background-color: #ff5733;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
width: inherit;
height: 80px;
line-height: 80px;
font-size: 18px;
padding: 0 10px;
position: fixed;
top: 0;
left: 0;
opacity: 0.8;
}
.navbar a {
text-decoration: none;
color: cornsilk;
}
.navbar ul {
float: right;
list-style: none;
height: inherit;
line-height: inherit;
padding: 0 5px;
}
.navbar ul li {
display: inline-block;
}
.navbar ul li a {
display: block;
text-align: center;
min-width: 30px;
padding: 0 30px;
}
.navbar ul li a:hover {
background-color: rgba(255, 255, 255, 0.15);
}
.navbar ul li ul li {
display: none;
}
.navbar ul li:hover ul li {
display: block;
background-color: #ff5733;
}
.navbar .navbar-toggler {
display: none;
}
@media (max-width: 768px) {
/* Style for screens with 766 pixels or less - tablets and mobile devices */
.navbar {
padding: 0;
}
.logo {
display: flex;
justify-content: center;
width: 100%;
}
.brand {
background-color: #ff5733;
display: flex;
justify-content: center;
font-size: 25px;
font-weight: 400;
letter-spacing: 2px;
width: 100%;
}
.navbar ul {
width: 100%;
padding: 0;
background-color: #4a7383;
height: 100vh;
max-height: 0;
overflow: hidden;
transition: all ease-in-out 0.5s;
}
.navbar ul.open {
max-height: 100vh;
}
.navbar ul li {
width: 100%;
}
.navbar ul li a {
padding: 0;
}
.navbar ul li:hover ul li {
background-color: #5b98b1;
}
.navbar ul li ul.open {
display: block;
height: 320px;
max-height: 320px;
transition: all ease-in-out 0.9s;
}
.navbar ul li ul li {
display: block;
background-color: #5b98b1;
}
.navbar .navbar-toggler {
display: block;
position: absolute;
height: 40px;
top: 20px;
right: 20px;
background-color: transparent;
color: #fff;
border: 3px solid #fff;
border-radius: 10px;
outline: none;
padding: 0 5px;
cursor: pointer;
}
.navbar .navbar-toggler span,
.navbar .navbar-toggler span::before,
.navbar .navbar-toggler span::after {
display: block;
content: '';
background-color: #fff;
height: 3px;
width: 28px;
border-radius: 5px;
transition: all ease-in-out 0.8s;
}
.navbar .navbar-toggler span::before {
transform: translateY(-8px);
}
.navbar .navbar-toggler span::after {
transform: translateY(+5px);
}
.navbar .navbar-toggler.open-navbar-toggler span {
/* transform: rotate(90deg); */
background-color: transparent;
}
.navbar .navbar-toggler.open-navbar-toggler span::before {
transform: translateY(0px) rotate(45deg);
}
.navbar .navbar-toggler.open-navbar-toggler span::after {
transform: translateY(-3px) rotate(-45deg);
}
}
JS
const navbarToggler = document.querySelector(".navbar-toggler");
const navbarMenu = document.querySelector(".navbar ul");
const navbarLinks = document.querySelectorAll(".navbar a");
const navbarSubMenu = document.querySelector(".navbar ul li ul")
navbarToggler.addEventListener("click", navbarTogglerClick);
function navbarTogglerClick(){
navbarToggler.classList.toggle("open-navbar-toggler");
if(navbarMenu.classList.contains("open")){
navbarMenu.classList.remove("open");
navbarSubMenu.classList.remove("open");
}else{
navbarMenu.classList.add("open");
}
}
navbarLinks.forEach(elem => elem.addEventListener("click", navbarLinkClick));
function navbarLinkClick(e){
if(navbarMenu.classList.contains("open")){
if(e.target.text == "About"){
navbarSubMenu.classList.toggle("open");
}
else{
navbarToggler.click();
}
}
}