汉堡菜单问题 - 导航菜单没有消失

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

我在保留汉堡菜单和导航菜单消失时遇到问题。有人可以帮忙吗?

参见html

 <nav>
            <a href="#" class="toggle-button">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>    
            </a>
            <!-- Navigation Links -->
           <a href="index.html">Home</a>
            <a href="whatson.html">Whats on</a> 
            <a href="businesses.html">Businesses</a> 
            <a href="gallery.html">Gallery</a>
            <a href="contactus.html">Contact us</a>
            
            

        </nav>

和CSS


nav {
    grid-area: nav;
   
   
    align-items: center;
    background-color: rgb(0,0,0);
    padding: 10px;
    border: 3px solid rgb(0,0,0);
    display:inline-block;
    background-size: 100% auto;
    background-repeat: no-repeat;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    width: 100%;
    
}
nav a {
    display: inline-block;
    background-color: rgb(252, 252, 252); 
    color: rgb(0); 
    padding: 10px 15px; 
    
    border: 2px solid black; 
    border-radius: 5px; 
    font-size: 18px; 
    font-weight: bold; 
    transition: background-color 0.3s, transform 0.3s; /* Smooth transition for hover effects */
    text-decoration: none;
    margin: 0 10px;
        }

nav a:hover {
    background-color: rgb(140, 71, 147); /* Change background on hover */
    color: rgb(255, 255, 255); /* Change text color on hover */
    transform: scale(1.05); /* Slightly increase size on hover */
}

/*MOBILE*/

.toggle-button
{
    position: absolute;
    top:18.2rem;
    right:1rem;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    width: 30px;
    height: 21px;
}


.toggle-button .bar 
{
    height: 3px;
    width: 100%;
    background-color: purple;
    border-radius: 10px;
}

@media (max-width: 700px)
{
    .toggle-button
    {
        display:flex;
    }
    
    .nav
    {
        display: none;
    }
    
    .nav a
    {
        display: none;
    }
}


我已经尝试了几个小时,但我无法理解发生了什么。预先感谢

我已经尝试了我能想到的一切!任何帮助将不胜感激。

html css hamburger-menu
2个回答
0
投票

尝试这样的事情:

toggle-button
中取出
nav
,这样当您将其切换为隐藏时,它就不会变得不可见。
nav
提供
id: nav
class: hide
,这意味着由于您的媒体查询,
nav
不会显示在手机等较小设备上。
然后使用 JS 在用户单击按钮时打开和关闭隐藏类。

document.querySelector(".toggle-button").addEventListener("click", () => {
  document.querySelector("#nav").classList.toggle("hide")
})
nav {
  grid-area: nav;
  align-items: center;
  background-color: rgb(0, 0, 0);
  padding: 10px;
  border: 3px solid rgb(0, 0, 0);
  display: inline-block;
  background-size: 100% auto;
  background-repeat: no-repeat;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  width: 100%;
}

nav a {
  display: inline-block;
  background-color: rgb(252, 252, 252);
  color: rgb(0);
  padding: 10px 15px;
  border: 2px solid black;
  border-radius: 5px;
  font-size: 18px;
  font-weight: bold;
  transition: background-color 0.3s, transform 0.3s;
  /* Smooth transition for hover effects */
  text-decoration: none;
  margin: 0 10px;
}

nav a:hover {
  background-color: rgb(140, 71, 147);
  /* Change background on hover */
  color: rgb(255, 255, 255);
  /* Change text color on hover */
  transform: scale(1.05);
  /* Slightly increase size on hover */
}


/*MOBILE*/

.toggle-button {
  position: absolute;
  top: 18.2rem;
  right: 1rem;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
}

.toggle-button .bar {
  height: 3px;
  width: 100%;
  background-color: purple;
  border-radius: 10px;
}

@media (max-width: 700px) {
  .toggle-button {
    display: flex;
  }
  .hide {
    display: none
  }
}
<a href="#" class="toggle-button">
  <span class="bar"></span>
  <span class="bar"></span>
  <span class="bar"></span>
</a>
<nav id="nav" class="hide">
  <!-- Navigation Links -->
  <a href="index.html">Home</a>
  <a href="whatson.html">Whats on</a>
  <a href="businesses.html">Businesses</a>
  <a href="gallery.html">Gallery</a>
  <a href="contactus.html">Contact us</a>
</nav>


0
投票

您在 CSS 中使用了类

.nav
,而 HTML 中没有这样的类,标签
nav
没有类。

您还需要删除媒体查询中的第一个显示:没有,因为您已经隐藏了 a 标签

那么,你的 CSS 将会是:

nav {
    grid-area: nav;
   
   
    align-items: center;
    background-color: rgb(0,0,0);
    padding: 10px;
    border: 3px solid rgb(0,0,0);
    display:inline-block;
    background-size: 100% auto;
    background-repeat: no-repeat;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    width: 100%;
    
}
nav a {
    display: inline-block;
    background-color: rgb(252, 252, 252); 
    color: rgb(0); 
    padding: 10px 15px; 
    
    border: 2px solid black; 
    border-radius: 5px; 
    font-size: 18px; 
    font-weight: bold; 
    transition: background-color 0.3s, transform 0.3s; /* Smooth transition for hover effects */
    text-decoration: none;
    margin: 0 10px;
        }

nav a:hover {
    background-color: rgb(140, 71, 147); /* Change background on hover */
    color: rgb(255, 255, 255); /* Change text color on hover */
    transform: scale(1.05); /* Slightly increase size on hover */
}

/*MOBILE*/

.toggle-button
{
    position: absolute;
    top:18.2rem;
    right:1rem;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    width: 30px;
    height: 21px;
}


.toggle-button .bar 
{
    height: 3px;
    width: 100%;
    background-color: purple;
    border-radius: 10px;
}

@media (max-width: 1000px)
{
    .toggle-button
    {
        display:flex;
    }
   
    
    nav a
    {
        display: none;
    }
}

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