响应式导航栏在打开后会阻止正文内容
Responsive navbar blocks body content once it's opened
在您尝试制定解决方案之前,请确保在您的 chrome 浏览器上启用了响应式工具栏。我正在尝试首先为移动设备进行响应式设计,然后为桌面设备进行响应式设计。但是我在打开汉堡包菜单时遇到了这个问题,导航栏阻止了我的内容。我试图通过做 position: relative; 来解决这个问题但我想让导航栏固定,所以当我滚动时它就在那里。此外,当我在滚动时单击汉堡菜单时,它会回到顶部。请帮助我
const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
})
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@100&family=Oswald&display=swap');
@import url("style.css");
*{
box-sizing: border-box;
}
body{
padding-top: 65px;
margin: 0px;
background-image: url(images/banner.jpg);
}
/* toggle button desiging */
.toggle-button{
position: absolute;
top: .75rem;
right: 1rem;
display: flex;
justify-content: space-between;
flex-direction: column;
width: 30px;
height: 21px;
}
.toggle-button .bar{
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
/* brand title decoration*/
.brand-title{
font-size: 32px;
margin: .5rem;
}
.brand-title a{
margin-left: 5px;
font-family: "Jetbrains Mono", sans-serif;
font-size: 28px;
}
/* navbar decoration */
.navbar {
width: 100%;
display: flex;
justify-content: space-between;
background: black;
color: white;
position: fixed;
top: 0;
flex-direction: column;
align-items: flex-start;
}
.navbar-links{
width: 100%;
display: none;
}
.navbar-links ul{
margin: 0;
padding: 0;
display: flex;
font-family: "Jetbrains Mono", sans-serif;
flex-direction: column;
width: 100%;
}
.navbar-links li{
list-style: none;
}
.navbar-links li a{
text-decoration: none;
color: white;
padding: 1rem;
display: block;
transition: .4s ease;
font-size: 15px;
text-align: center;
}
.navbar-links li a:hover{
color: crimson;
}
.navbar-links.active{
display: flex;
position: relative;
}
.navbar-links ul.active{
color: black;
}
/* landing page */
.introduction{
padding: 0px;
margin: 32px 32px;
display: block;
color: white;
}
.introduction .first-text{
margin: 0px;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 100;
font-size: 6vh;
}
.first-text a{
font-weight: bold;
}
.introduction .second-text{
margin-top: 16px;
margin-bottom: 16px;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 600;
font-size: 4vh;
color:rgba(255, 255, 255, 0.800);
}
.introduction .third-text{
margin: 0;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 600;
font-size: 3vh;
color:rgba(255, 255, 255, 0.800);
}
.btn-hire-me{
background-color: rgba(0, 255, 255, 0.800);
padding: 1rem;
border: 1px solid black;
margin-top: 32px;
border-radius: 8px;
box-shadow: rgba(255, 255, 255, 0.431) 0 2px 4px;
font-family: "Jetbrains Mono", sans-serif;
width: 150px;
height: 75px;
}
.btn-hire-me img{
position: relative;
right: 6px;
height: 16px;
width: 16px;
}
.btn-hire-me a{
font-weight: bold;
font-size: 22px;
}
.btn-hire-me:hover{
background: crimson;
color: white;
}
/* my image */
.my-image img{
display: block;
margin: auto;
width: 320px;
height: 320px;
border-radius: 50%;
border: 3px solid crimson;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
<script src="https://use.fontawesome.com/72f2c1bea1.js"></script>
<title>Talha Bin Hasan ||</title>
</head>
<body>
<header>
<nav class="navbar" id="nav">
<div class="brand-title"><i class="fa fas fa-code"></i><a>Talha Bin Hasan</a></div>
<a href="#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="navbar-links">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>
</header>
<section>
<div class="introduction">
<h1 class="first-text">Hello I am <a>Talha Bin Hasan</a></h1>
<h3 class="second-text">Front End Web Developer</h3>
<h3 class="third-text">Highly experienced in designing and developing responsive website.</h3>
<button type="button" class="btn-hire-me"><img src="images/hire-icon.png" alt=""><a>Hire me</a></button>
</div>
<div class="my-image">
<img src="images/myphoto.jpg" alt="Talha Bin Hasan's photo">
</div>
</section>
</body>
</html>
当您点击菜单时,它会跳到顶部,因为您的菜单是 a 元素。只需将其更改为 div:
<div class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
为了在打开菜单时使内容可见,您可以在第一部分添加边距。您可以添加一个检查页面滚动的距离,以防止在向下滚动时打开菜单时添加和删除边距。将您的 .js 更改为:
const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]
const firstSection = document.querySelector("section:first-of-type")
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
firstSection.classList.toggle('marginTop');
})
window.addEventListener('scroll', function(){
if(window.scrollY > 100){
firstSection.classList.add("scrolled");
}
else {
firstSection.classList.remove("scrolled");
}
});
在你的.css中(过渡使其更平滑):
section:first-of-type {
transition-duration: 1s;
}
section.marginTop:not(.scrolled) {
margin-top: 200px;
}
在您尝试制定解决方案之前,请确保在您的 chrome 浏览器上启用了响应式工具栏。我正在尝试首先为移动设备进行响应式设计,然后为桌面设备进行响应式设计。但是我在打开汉堡包菜单时遇到了这个问题,导航栏阻止了我的内容。我试图通过做 position: relative; 来解决这个问题但我想让导航栏固定,所以当我滚动时它就在那里。此外,当我在滚动时单击汉堡菜单时,它会回到顶部。请帮助我
const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
})
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@100&family=Oswald&display=swap');
@import url("style.css");
*{
box-sizing: border-box;
}
body{
padding-top: 65px;
margin: 0px;
background-image: url(images/banner.jpg);
}
/* toggle button desiging */
.toggle-button{
position: absolute;
top: .75rem;
right: 1rem;
display: flex;
justify-content: space-between;
flex-direction: column;
width: 30px;
height: 21px;
}
.toggle-button .bar{
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
/* brand title decoration*/
.brand-title{
font-size: 32px;
margin: .5rem;
}
.brand-title a{
margin-left: 5px;
font-family: "Jetbrains Mono", sans-serif;
font-size: 28px;
}
/* navbar decoration */
.navbar {
width: 100%;
display: flex;
justify-content: space-between;
background: black;
color: white;
position: fixed;
top: 0;
flex-direction: column;
align-items: flex-start;
}
.navbar-links{
width: 100%;
display: none;
}
.navbar-links ul{
margin: 0;
padding: 0;
display: flex;
font-family: "Jetbrains Mono", sans-serif;
flex-direction: column;
width: 100%;
}
.navbar-links li{
list-style: none;
}
.navbar-links li a{
text-decoration: none;
color: white;
padding: 1rem;
display: block;
transition: .4s ease;
font-size: 15px;
text-align: center;
}
.navbar-links li a:hover{
color: crimson;
}
.navbar-links.active{
display: flex;
position: relative;
}
.navbar-links ul.active{
color: black;
}
/* landing page */
.introduction{
padding: 0px;
margin: 32px 32px;
display: block;
color: white;
}
.introduction .first-text{
margin: 0px;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 100;
font-size: 6vh;
}
.first-text a{
font-weight: bold;
}
.introduction .second-text{
margin-top: 16px;
margin-bottom: 16px;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 600;
font-size: 4vh;
color:rgba(255, 255, 255, 0.800);
}
.introduction .third-text{
margin: 0;
font-family: "Jetbrains Mono", sans-serif;
font-weight: 600;
font-size: 3vh;
color:rgba(255, 255, 255, 0.800);
}
.btn-hire-me{
background-color: rgba(0, 255, 255, 0.800);
padding: 1rem;
border: 1px solid black;
margin-top: 32px;
border-radius: 8px;
box-shadow: rgba(255, 255, 255, 0.431) 0 2px 4px;
font-family: "Jetbrains Mono", sans-serif;
width: 150px;
height: 75px;
}
.btn-hire-me img{
position: relative;
right: 6px;
height: 16px;
width: 16px;
}
.btn-hire-me a{
font-weight: bold;
font-size: 22px;
}
.btn-hire-me:hover{
background: crimson;
color: white;
}
/* my image */
.my-image img{
display: block;
margin: auto;
width: 320px;
height: 320px;
border-radius: 50%;
border: 3px solid crimson;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
<script src="https://use.fontawesome.com/72f2c1bea1.js"></script>
<title>Talha Bin Hasan ||</title>
</head>
<body>
<header>
<nav class="navbar" id="nav">
<div class="brand-title"><i class="fa fas fa-code"></i><a>Talha Bin Hasan</a></div>
<a href="#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="navbar-links">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>
</header>
<section>
<div class="introduction">
<h1 class="first-text">Hello I am <a>Talha Bin Hasan</a></h1>
<h3 class="second-text">Front End Web Developer</h3>
<h3 class="third-text">Highly experienced in designing and developing responsive website.</h3>
<button type="button" class="btn-hire-me"><img src="images/hire-icon.png" alt=""><a>Hire me</a></button>
</div>
<div class="my-image">
<img src="images/myphoto.jpg" alt="Talha Bin Hasan's photo">
</div>
</section>
</body>
</html>
当您点击菜单时,它会跳到顶部,因为您的菜单是 a 元素。只需将其更改为 div:
<div class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
为了在打开菜单时使内容可见,您可以在第一部分添加边距。您可以添加一个检查页面滚动的距离,以防止在向下滚动时打开菜单时添加和删除边距。将您的 .js 更改为:
const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]
const firstSection = document.querySelector("section:first-of-type")
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
firstSection.classList.toggle('marginTop');
})
window.addEventListener('scroll', function(){
if(window.scrollY > 100){
firstSection.classList.add("scrolled");
}
else {
firstSection.classList.remove("scrolled");
}
});
在你的.css中(过渡使其更平滑):
section:first-of-type {
transition-duration: 1s;
}
section.marginTop:not(.scrolled) {
margin-top: 200px;
}