响应式导航栏一旦打开就会阻止正文内容

Posted

技术标签:

【中文标题】响应式导航栏一旦打开就会阻止正文内容【英文标题】:Responsive navbar blocks body content once it's opened 【发布时间】:2022-01-16 07:39:11 【问题描述】:

在您尝试制定解决方案之前,请确保您的 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" ><a>Hire me</a></button>
        </div>
        <div class="my-image">
            <img src="images/myphoto.jpg" >
        </div>
    </section>
    
</body>

</html>

【问题讨论】:

【参考方案1】:

当您单击菜单时,它会跳到顶部,因为您的菜单是一个 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;

【讨论】:

我需要另一个帮助。单击汉堡菜单时如何向导航栏添加过渡? 您的菜单通过将显示设置为无/弹性来工作。显示属性上的转换 - 据我所知 - 不可能。不要改变显示,而是改变 max-height: .navbar-links width: 100%;最大高度:0;溢出:隐藏;过渡:最大高度 1s; .navbar-links.active 最大高度:200px; 当我点击菜单时,除了我们之前添加的边距外,现在什么都没有显示。 没关系,我不小心使用了“max-width”而不是“max-height”。有效。非常感谢,我的朋友!

以上是关于响应式导航栏一旦打开就会阻止正文内容的主要内容,如果未能解决你的问题,请参考以下文章

响应式导航栏布局空间

为啥一旦我滚动标题部分,引导导航栏就会消失?

内容下方的导航栏

响应式导航栏隐藏其下方的元素

Bootstrap 导航栏响应式下拉菜单不响应触摸

如何使用 vuetify 制作移动响应式导航栏