如何使我的导航栏项目垂直,同时让徽标和图标保持在顶部

问题描述 投票:0回答:1

这是我第一次做一些响应式设计,我用它作为练习。但在尝试了几个小时之后,我现在很绝望。所以我有一个简单的

Navbar
,左上角有一个徽标,中间有四个导航项,右侧有三个按钮。

viewport
达到
912px
的宽度时,我将导航项隐藏在中间。现在只剩下左侧的Logo,右侧的三个按钮被替换为汉堡菜单图标。

当我点击汉堡包

menu icon
时,导航项应该出现并垂直堆叠在徽标和菜单图标下方。但它们只是出现在徽标和菜单之间。目前我还没有经验,无法解决这个问题。所以我恳请帮助。

JSFiddle 链接:https://jsfiddle.net/0nrc1wq4/1/

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap"
          rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="navbar.css" />
    </head>

<body>
    <header>
        <nav>
            <img src="../images/logo-small.svg" alt="Logo" id="icon-logo" class="corner-item-left" />
            <ul>
                <li class="nav-item"><a class="nav-link" href="#Willkommen">Willkommen</a></li>
                <li class="nav-item"><a class="nav-link" href="#Wohnen">Wohnen</a></li>
                <li class="nav-item"><a class="nav-link" href="#Kleinwalsertal">Kleinwalsertal</a></li>
                <li class="nav-item"><a class="nav-link" href="#Kontakt">Kontakt</a></li>
            </ul>
            <ul>
                <li class="icon-item"><button class="icon-button" id="icon-phone"><img src="../images/icon-phone.svg" alt="Telefon"/></button></li>
                <li class="icon-item"><button class="icon-button" id="icon-contact"><img src="../images/icon-contact.svg" alt="Kontakt"/></button></li>
                <li class="icon-item"><button class="icon-button corner-item-right" id="icon-booking"><img src="../images/icon-booking.svg" alt="Buchen"/></button></li>
            </ul>
            <button class="burger-menu-button" id="burger-menu">☰</button>
        </nav>
    </header>

    <script>
        const burgerMenu = document.getElementById("burger-menu");
        const logo = document.getElementById("icon-logo");
        const navUl = document.querySelector("nav ul");

        function toggleMenu() {
            toggleLogo();
            toggleBurgerMenuIcon();
            toggleNavVisibility();
        }

        function toggleLogo() {
                if (logo.src.includes("logo-small.svg")) {
                    logo.src = "../images/logo-filled.svg";
                } else {
                    logo.src = "../images/logo-small.svg";
                }
        }

        function toggleBurgerMenuIcon() {
            if (burgerMenu.innerHTML === "☰") {
                burgerMenu.innerHTML = "✖";
            } else {
                burgerMenu.innerHTML = "☰";
            }
        }

        function toggleNavVisibility() {
            navUl.classList.toggle("nav-visible");
        }

        burgerMenu.addEventListener("click", toggleMenu);
    </script>
</body>

HTML 和 JS:

提前谢谢您!

javascript html css navbar responsive
1个回答
0
投票

这不是最干净的方法,但它可以为您提供如何操作的指导。 我在这里还使用了一些变量只是为了记住它们,你应该在你的研究/示例中更多地使用它们。

            const burgerMenu = document.getElementById("burger-menu");
            const logo = document.getElementById("icon-logo");
            const navUl = document.querySelector("nav ul");

            function toggleMenu() {
                toggleLogo();
                toggleBurgerMenuIcon();
                toggleNavVisibility();
            }

            function toggleLogo() {
                    if (logo.src.includes("logo-small.svg")) {
                        logo.src = "../images/logo-filled.svg";
                    } else {
                        logo.src = "../images/logo-small.svg";
                    }
            }

            function toggleBurgerMenuIcon() {
                if (burgerMenu.innerHTML === "☰") {
                    burgerMenu.innerHTML = "✖";
                } else {
                    burgerMenu.innerHTML = "☰";
                }
            }

            function toggleNavVisibility() {
                navUl.classList.toggle("nav-visible");
            }

            burgerMenu.addEventListener("click", toggleMenu);
:root {
  --menu-height: 80px;  
  --left-space:20px;
}

body {
    background: #7A593E;
    margin:0px;
}

header {
    display: flex;
    justify-content: center;
    align-items: center;
}

.corner-item-left {
    margin-left: 20px;
}

.corner-item-right {
    margin-right: 20px;
}

 .menuLayer
 {
   width:100%;
   display:flex;
   align-items: center;    
 }

/* Navbar */
nav {
    background: #FFFFFF 0 0 no-repeat padding-box;
    display: flex;    
    width: 100%;
    height: var(--menu-height);
}

nav ul {
    display: flex;
    list-style: none;
    padding: 0;
    margin: 0;
}

nav ul li {
    margin-left: 20px;
}

nav img {
    height: 60px;
}

/* Mitte */
.nav-item {
    margin-left: 50px;
    font: normal normal normal 20px/80px Montserrat;
}

.nav-item a {
    text-decoration: none;
    color: #AF8C6E;
}

.nav-link {
    position: relative;
}

.nav-link::after {
    content: '';
    height: 4px;
    width: 100%;
    background: #C59E7C;
    display: block;
    position: absolute;
    left: 0;
    bottom: -4px;
    opacity: 0;
    transition: all 0.2s;
    pointer-events: none;
}

.nav-link:hover::after {
    opacity: 1;
}

/* Buttons rechts */
.icon-item {
    margin-left: 10px;
}

.icon-button {
    background-color: #C59E7C;
    border: none;
    border-radius: 30px;
    padding: 15px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon-button img {
    width: 24px;
    height: 24px;
}

.burger-menu-button {
    color: #C59E7C;
    background: none;
    border: none;
    padding: 15px;
    font-size: 24px;
    cursor: pointer;
    display: none;
}

#icon-booking {
    background-color: #BEA56C !important;
}

.nav-visible {
    display: flex !important;
}

/* Responsive */

@media screen and (max-width: 912px) {
    
    nav
    {
      justify-content:space-between;
    }
    
    .menuLayer
    {      
      position:absolute;
      top:var(--menu-height);
      left:0;
      z-index:10;
      background-color:green;
      overflow-y:auto;
      overflow-x:hidden;
      justify-content:space-between;      
      max-width:300px;       
      min-width:200px;
    }
    
    .nav-item {
      margin:0px;        
      padding-left:var(--left-space);      
    }
    
    nav ul {
        display: none;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }

    nav ul li {
        margin: 0;
        width:100%;        
    }

    .burger-menu-button {
        display: inline-block;
    }
    
    
}
<?php
?>

<html>
    <head>
        <title>Aufgabe 3</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap"
              rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="navbar.css" />
    </head>

    <body>
        <header>
            <nav>
                <img src="../images/logo-small.svg" alt="Logo" id="icon-logo" class="corner-item-left" />
                <div class="menuLayer">
                                
                  <ul>
                      <li class="nav-item"><a class="nav-link" href="#Willkommen">Willkommen</a></li>
                      <li class="nav-item"><a class="nav-link" href="#Wohnen">Wohnen</a></li>
                      <li class="nav-item"><a class="nav-link" href="#Kleinwalsertal">Kleinwalsertal</a></li>
                      <li class="nav-item"><a class="nav-link" href="#Kontakt">Kontakt</a></li>
                  </ul>
                  <ul>
                      <li class="icon-item"><button class="icon-button" id="icon-phone"><img src="../images/icon-phone.svg" alt="Telefon"/></button></li>
                      <li class="icon-item"><button class="icon-button" id="icon-contact"><img src="../images/icon-contact.svg" alt="Kontakt"/></button></li>
                      <li class="icon-item"><button class="icon-button corner-item-right" id="icon-booking"><img src="../images/icon-booking.svg" alt="Buchen"/></button></li>
                  </ul>                
                
                </div>
                <button class="burger-menu-button" id="burger-menu">☰</button>
            </nav>
        </header>
    </body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.