css 响应导航汉堡菜单不工作

quhf5bfb  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(304)

我试着让我的导航响应,但当我试图调整我的窗口,我的汉堡包不允许下拉功能工作。我把导航和响应汉堡包在线,所以有什么地方可能会覆盖汉堡包?
这是我的HTML

<header>
  <div class="logo">
    <p>LEGEND</p>
    <div class= "hamburger">
      <div class="line"></div>
      <div class="line"></div>
      <div class="line"></div>
    </div>
  </div>
  <nav class="nav-bar">
    <ul>
      <li><a href="#">HOME</a></li>
      <li><a href="#">STORE</a></li>
      <li><a href="#">MY ACCOUNT</a></li>
      <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
  <button>
  <a href="#">
  <h4 style="color: #f5f5f5">PLAY DIVINE</h4>
  </a>
  </button>
</header>

这是我的css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    background-color: #12171c;
}
header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 10%;
    background: rgba(0, 0, 0, 0.2);
    position: fixed;
    height: 10%;
}
.logo {
    font-size: 30px;
    font-weight: bold;
    color: white;
    letter-spacing: 1.5px;
    cursor: pointer;
}
.nav-bar li {
    display: inline-block;
    list-style: none;
    padding: 0px 15px;
}
a, button {
    font-size: 16px;
    font-weight: 500;
    color: #b7b9bb;
    text-decoration: none;
    cursor: pointer;
}
button {
    background: #967526;
    border: 2px solid #ffce1f;
    padding: 9px 25px;
}
.header-pic {
    width: 100%;
    height: 100%;
    background-size: cover;
}
.hamburger {
    display: none;
}

这是我的css当它的响应

@media only screen and (max-width: 1320px) {
.hamburger {
    display: block;
    cursor: pointer;
}
.hamburger .line {
    width: 30px;
    height: 3px;
    background: #fefefe;
    margin: 6px 0;
}
.nav-bar {
    height: 0;
    position: absolute;
    top: 80px;
    left: 0;
    right: 0;
    width: 100vw;
    background: #11101b;
    transition: 0.2s;
    overflow: hidden;
}
.nav-bar.active {
    height: 450px;
}
.nav-bar ul {
    display: block;
    width: fit-content;
    margin: 80px auto 0 auto;
    text-align: center;
    transition: 0.5s;
    opacity: 0;
}
.nav-bar.active ul {
    opacity: 1;
}
.nav-bar ul li a {
    margin-botton: 12px;
}
}

这是我的javascript

<script>
 hamburger = document.querySelector(".hamburger");
 hamburger.onClick = function() {
  navBar = document.querySelector(".nav-bar");
  navbar.classList.toggle("active");
  }
 </script>
kmb7vmvb

kmb7vmvb1#

    • 更新**

我修改了解决方案,以便2个块用于:
1.在小屏幕中,但active未打开
1.在小屏幕中,active打开
active切换时发生过渡时,这种分离保护样式不被破坏。
可以向第一个块中添加更多样式,以定义此列表的外观,例如使其按列显示列表。
示例:

.nav-bar ul {
  display: flex;
  flex-direction: column;
  width: fit-content;
  margin: 80px auto 0 auto;
  text-align: center;
  transition: 0.5s;
  opacity: 0;
}

.nav-bar.active ul {
  opacity: 1;
}
    • 原件**

这是因为您的JS代码有语法错误。下面是处理方法:
1.使用const声明hamburgernavbar
1.在hamburger上调用addEventListener,添加切换navbar类的函数
按照下面的示例修改您的代码,或访问此处修复的实时演示:codepen

const hamburger = document.querySelector(".hamburger");
const navbar = document.querySelector(".nav-bar");
hamburger.addEventListener("click", () => navbar.classList.toggle("active"));

希望这个解决方案能有所帮助。

zujrkrfu

zujrkrfu2#

您的代码没有任何错误,只是JavaScript中有一些打字错误:
1.您使用的是onClick,而不是onclick
1.在navbar.classList.toggle("active")处使用navbar代替navBar

    • 更正版本**
<script>
  hamburger = document.querySelector(".hamburger");
  hamburger.onclick = function() {
    navBar = document.querySelector(".nav-bar");
    navBar.classList.toggle("active");
  }
</script>
    • 额外**(* 在CSS媒体查询的.nav-bar ul选择器中将display: block;更改为display: grid;,以在垂直列表中显示下拉列表 *)
.nav-bar ul {
  display: grid;
  width: fit-content;
  margin: 80px auto 0 auto;
  text-align: center;
  transition: 0.5s;
  opacity: 0;
}

一个一个二个一个一个一个三个一个一个一个一个一个四个一个

更好的解决方案

引用MDN Web文档,如下链接所示:
建议使用addEventListener()方法在网页中添加事件处理程序
您可以使用addEventListener()来实现相同的结果:

<script>
  hamburger = document.querySelector(".hamburger");
  hamburger.addEventListener("click", () => {
    navBar = document.querySelector(".nav-bar");
    navBar.classList.toggle("active");
  });
</script>
hamburger = document.querySelector(".hamburger");
hamburger.addEventListener("click", () => {
  navBar = document.querySelector(".nav-bar");
  navBar.classList.toggle("active");
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: #12171c;
}

header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10%;
  background: rgba(0, 0, 0, 0.2);
  position: fixed;
  height: 10%;
}

.logo {
  font-size: 30px;
  font-weight: bold;
  color: white;
  letter-spacing: 1.5px;
  cursor: pointer;
}

.nav-bar li {
  display: inline-block;
  list-style: none;
  padding: 0px 15px;
}

a,
button {
  font-size: 16px;
  font-weight: 500;
  color: #b7b9bb;
  text-decoration: none;
  cursor: pointer;
}

button {
  background: #967526;
  border: 2px solid #ffce1f;
  padding: 9px 25px;
}

.header-pic {
  width: 100%;
  height: 100%;
  background-size: cover;
}

.hamburger {
  display: none;
}

@media only screen and (max-width: 1320px) {
  .hamburger {
    display: block;
    cursor: pointer;
  }
  .hamburger .line {
    width: 30px;
    height: 3px;
    background: #fefefe;
    margin: 6px 0;
  }
  .nav-bar {
    height: 0;
    position: absolute;
    top: 80px;
    left: 0;
    right: 0;
    width: 100vw;
    background: #11101b;
    transition: 0.2s;
    overflow: hidden;
  }
  .nav-bar.active {
    height: 450px;
  }
  .nav-bar ul {
    display: grid;
    width: fit-content;
    margin: 80px auto 0 auto;
    text-align: center;
    transition: 0.5s;
    opacity: 0;
  }
  .nav-bar.active ul {
    opacity: 1;
  }
  .nav-bar ul li a {
    margin-bottom: 12px;
  }
}
<header>
  <div class="logo">
    <p>LEGEND</p>
    <div class="hamburger">
      <div class="line"></div>
      <div class="line"></div>
      <div class="line"></div>
    </div>
  </div>
  <nav class="nav-bar">
    <ul>
      <li><a href="#">HOME</a></li>
      <li><a href="#">STORE</a></li>
      <li><a href="#">MY ACCOUNT</a></li>
      <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
  <button>
    <a href="#">
      <h4 style="color: #f5f5f5">PLAY DIVINE</h4>
    </a>
  </button>
</header>

替代溶液

对于更熟悉add()/remove()的读者,这里有一些替代方法:

<script>
  hamburger = document.querySelector(".hamburger");
  hamburger.addEventListener("click", () => {
    navBar = document.querySelector(".nav-bar");
    if (navBar.classList.contains("active")) {
      navBar.classList.remove("active");
    } else {
      navBar.classList.add("active");
    }
  });
</script>

一个10块一个11块一个12块一个

相关问题