关闭我的汉堡菜单会删除导航栏的响应能力

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

我目前正在制作汉堡菜单,效果很好。当我尝试使我的网站具有响应能力时,问题就出现了。如果我缩小页面,按钮就会消失,汉堡菜单图标就会出现。如果我在打开菜单之前展开页面,汉堡菜单就会消失并且按钮会重新出现。还没有问题。如果我打开菜单然后展开页面,也会发生同样的事情。还没有问题。

如果我打开菜单,然后关闭它,然后展开页面,按钮不会重新出现,但汉堡菜单会消失。我在这里做错了什么?

function Burgermenu() {
  var x = document.getElementById("myLinks");
  if (x.style.display == "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }

}
body {
  font-family: Arial, Helvetica, sans-serif;
}

.mobile-container {
  max-width: 480px;
  margin: auto;
  background-color: #555;
  height: 500px;
  color: white;
  border-radius: 10px;
}

.navbar {
  overflow: hidden;
  background-color: #333;
  position: relative;
}

.navbar a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

.navbar a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}

.navbar a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #04AA6D;
  color: white;
}

@media only screen and (max-width: 600px) {
  .navbar #myLinks {
    display: none;
  }
  .navbar a.icon {
    display: block;
    position: absolute;
    right: 0;
    top: 0;
  }
}

@media only screen and (min-width: 600px) {
  .navbar #myLinks {
    display: block;
  }
  .navbar a.icon {
    display: none;
  }
}
<div class="mobile-container">
  <div class="navbar">
    <a href="#home" class="active">Logo</a>
    <div id="myLinks">
      <a href="#stuff">Stuff</a>
      <a href="#stuff">Stuff</a>
      <a href="#stuff">Stuff</a>
    </div>
    <a href="javascript:void(0);" class="icon" onclick="Burgermenu()">
      <i class="fa fa-bars"></i>
    </a>
  </div>
</div>

html css hamburger-menu responsiveness
1个回答
0
投票

内联样式优先于匹配样式表样式。

当您关闭菜单时,您将直接在此处的元素上设置

display: none

if (x.style.display == "block") {
  x.style.display = "none";
}

当您展开视图并覆盖此处的 CSS 规则时,会保留该规则:

@media only screen and (min-width: 600px) {
  .navbar #myLinks {
    display: block;
  }
}

您可以做的是在

open
元素上切换
#myLinks
类:

function Burgermenu() {
  var x = document.getElementById("myLinks");
  x.classList.toggle('open');
}
body {
  font-family: Arial, Helvetica, sans-serif;
}

.mobile-container {
  max-width: 480px;
  margin: auto;
  background-color: #555;
  height: 500px;
  color: white;
  border-radius: 10px;
}

.navbar {
  overflow: hidden;
  background-color: #333;
  position: relative;
}

.navbar a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;
}

.navbar a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;
}

.navbar a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #04AA6D;
  color: white;
}

@media only screen and (max-width: 600px) {
  .navbar #myLinks {
    display: none;
  }
  .navbar #myLinks.open {
    display: block;
  }
  .navbar a.icon {
    display: block;
    position: absolute;
    right: 0;
    top: 0;
  }
}

@media only screen and (min-width: 600px) {
  .navbar #myLinks {
    display: block;
  }
  .navbar a.icon {
    display: none;
  }
}
<div class="mobile-container">
  <div class="navbar">
    <a href="#home" class="active">Logo</a>
    <div id="myLinks">
      <a href="#stuff">Stuff</a>
      <a href="#stuff">Stuff</a>
      <a href="#stuff">Stuff</a>
    </div>
    <a href="javascript:void(0);" class="icon" onclick="Burgermenu()">
      <i class="fa fa-bars"></i>
    </a>
  </div>
</div>

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