如何在汉堡菜单内移动导航栏链接?

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

我在如何重新排列HTML / CSS代码以在汉堡导航菜单内移动一些链接时遇到了困难。我希望始终可以看到“家”,但是,我希望其他链接的页面位于汉堡菜单内,仅在单击菜单时才可见...因此,在汉堡内可以是:

关于联系投资组合等

关于如何实现这一目标的任何建议?

    * {
        text-decoration: none;
    }
    
    body {
        background-color: #f3f3f3;
    }
    
    header {
       background-color: #fff; 
       width: 100%;
       height: 100px;
       display: table-cell;
    }
    
    .header-logo img {
        height:100px;
        padding: 10px 0px 10px 10px;
        float: left;
    }
    
    header nav ul {
        display: block;
        margin: 0 auto;
        width: fit-content;
        padding-top: 30px;
    }
    
    header nav ul li {
        display: inline-block;
        padding: 0 5px;
    }
    
    header nav ul li a {
        font-family:'Sorts Mill Goudy', serif;
        font-size: 16px;
        color: #111;
        text-transform: uppercase;
    }
    
    .sub {
        display: none;
        background-color: rgb(70, 149, 223);
        margin-left: 5%;
        height: auto;
    }
    
    /* HAMBURGER MENU */
    .nav div {
        height: 4px;
        background-color: rgb(20, 15, 15);
        margin:  5px 0;
        border-radius: 25px;
        transition: 0.3s;
    }
    
    .nav {
        width: 30;
        display: block;
        float: right;
        margin: 1em 0 0 1em;
        padding-right: 10px;
    }
    
    .one {
        width: 30px;
    } 
    
    .two {
        width: 20px;
    } 
    
    .three {
        width: 25px;
    } 
    
    .nav:hover div{
        width: 30px;
    }
    
    
    
    
    ul li a:hover {
        color: rgb(255, 255, 255);
    }
<header>
<div class="header-logo">
<a href="index.html"> <img src="img/Milestonehackers.jpg" alt="Milestonehackers logo"></a>
</div>
            <nav>
                <ul> <li><a href="index.html">Home</a></li></ul>
            <ul>
              <a href="#" class="nav">
                    <div class="one"></div>
                    <div class="two"></div>
                    <div class="three"></div>        
             
                <li><a href="podcasts.html">Podcast</a></li>
                <li><a href="newsletter.html">Newsletter</a></li>
                <li><a href="blog.html">Blog</a></li>
                <li><a href="contact.html">Contact</a></li>
    
                <div class="sub">
                <li><a href="subscribe.html">Subscribe</a></li>
                </div>
                </a>  
            </ul> 
            </nav>
        </header>
    
html css navbar hamburger-menu
2个回答
1
投票

您正在寻找的被称为toggle。为此,您需要使用javascript或jquery(简化的javascript“版本”)。为了便于说明,例如,为要切换的子元素添加一个父div。然后在您的CSS中显示此父div无。然后,您可以使用jquery告诉您要单击的内容,然后再告诉您要切换的内容。

$(".nav").click(function(){
	$("#hamburger").toggle(250);
});
   * {
    text-decoration: none;
}

body {
    background-color: #f3f3f3;
}

header {
   background-color: #fff; 
   width: 100%;
   height: 100px;
   display: table-cell;
}

.header-logo img {
    height:100px;
    padding: 10px 0px 10px 10px;
    float: left;
}

header nav ul {
    display: block;
    margin: 0 auto;
    width: fit-content;
    padding-top: 30px;
}

header nav ul li {
    display: inline-block;
    padding: 0 5px;
}

header nav ul li a {
    font-family:'Sorts Mill Goudy', serif;
    font-size: 16px;
    color: #111;
    text-transform: uppercase;
}

.sub {
    display: none;
    background-color: rgb(70, 149, 223);
    margin-left: 5%;
    height: auto;
}

/* HAMBURGER MENU */
.nav div {
    height: 4px;
    background-color: rgb(20, 15, 15);
    margin:  5px 0;
    border-radius: 25px;
    transition: 0.3s;
}

.nav {
    width: 30;
    display: block;
    float: right;
    margin: 1em 0 0 1em;
    padding-right: 10px;
}

.one {
    width: 30px;
} 

.two {
    width: 20px;
} 

.three {
    width: 25px;
} 

.nav:hover div{
    width: 30px;
}

#hamburger{
  display:none;
}



ul li a:hover {
    color: rgb(255, 255, 255);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
        <div class="header-logo">
        <a href="index.html"> <img src="https://milestonehackers.com/img/Milestonehackers.jpg" alt="Milestonehackers logo"></a>
        </div>
        <nav>
            <ul> <li><a href="index.html">Home</a></li></ul>
        <ul>
          <a href="#" class="nav">
                <div class="one"></div>
                <div class="two"></div>
                <div class="three"></div>        
<div id = "hamburger">

            <li><a href="podcasts.html">Podcast</a></li>
            <li><a href="newsletter.html">Newsletter</a></li>
            <li><a href="blog.html">Blog</a></li>
            <li><a href="contact.html">Contact</a></li>

</div>
            <div class="sub">
            <li><a href="subscribe.html">Subscribe</a></li>
            </div>
            </a>  
        </ul> 
        </nav>
    </header>

1
投票

不要以为只有使用CSS才能实现您想要的功能,也许会有很多CSS“ hacks”。我建议添加一些JavaScript以在点击时显示。

我建议您检查此页面https://www.w3schools.com/howto/howto_js_mobile_navbar.asp,因为它们的示例与您尝试实现的示例相同。

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