汉堡菜单-为什么我的代码不起作用?

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

[我正在尝试使用jQuery或JS做一个简单的汉堡菜单(无论哪种方式都很容易,但是我的代码根本行不通,而且我一生都无法找出原因。

其结构为:左上角为汉堡包,中间为徽标,右上角为轮廓和购物篮。

目前,我要做的只是从字面上获取导航列表,然后可以对其进行样式设置。我正在尝试使用.hidden类来执行此操作,以在单击汉堡时将导航显示为块。

这里是代码:

HTML:

       <section class="header-section">
  <div class="header-container">
   <div class="header-content">
     <div class="hamburger-container">
    <i id="burger" class="fas fa-bars fa-2x"></i>
     </div>
     <div class="header-logo-container">
       <h2>Logo goes here</h2>
     </div>
     <div class="header-profile-and-signin">
       <i class="fas fa-user-circle fa-2x"></i>
       <i class="fas fa-suitcase-rolling fa-2x"></i>
     </div>
       <ul id="nav">
          <li>
            <a href="search-page.html" style="text-decoration: none">Holidays</a>
          </li>
          <li>
            <a href="sign-in.html" style="text-decoration: none">Sign In / Register</a>
          </li>
        </ul>
    </div>
  </div>
</section>

CSS:

body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.header-section {
  height: 100px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #999;
}

.header-container {
  height: 100%;
  width: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: blue;
}

.header-content {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: yellow;
}

.hamburger-container {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  background-color: red;
}

.hamburger-container i {
  margin-left: 20px;
}

.header-logo-container {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: green;
}

.header-profile-and-signin {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

#nav {
  list-style: none;
  display: none;
}

.hidden {
  display: block;
}

#burger {
  cursor: pointer;
}

jQuery:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

  <script>

$("burger").click(function() {
  $("nav").toggleClass("hidden");
});

  </script>

任何帮助将不胜感激。谢谢。

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

您的代码中至少有三个问题:

  • [$('burger')必须为$('#burger')才能包含ID选择器前缀
  • 类似地,$('nav')必须为$('#nav')
  • CSS中的
  • .hidden必须为#nav.hidden,以便它足够具体以覆盖默认样式。我也建议将其更改为.visible以符合其用途,但这纯粹是一个语义问题。

另一个可能的问题是,您可能需要将document.ready事件处理程序添加到JS逻辑中,具体取决于您在页面中的放置位置。这将确保jQuery代码执行after DOM已完全加载。

jQuery(function($) {
  $("#burger").click(function() {
    $("#nav").toggleClass("hidden");
  });
});
body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.header-section {
  height: 100px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #999;
}

.header-container {
  height: 100%;
  width: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: blue;
}

.header-content {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: yellow;
}

.hamburger-container {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  background-color: red;
}

.hamburger-container i {
  margin-left: 20px;
}

.header-logo-container {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: green;
}

.header-profile-and-signin {
  height: 100%;
  width: 33.33%;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

#nav {
  list-style: none;
  display: none;
}

#nav.hidden {
  display: block;
}

#burger {
  cursor: pointer;
}
<section class="header-section">
  <div class="header-container">
    <div class="header-content">
      <div class="hamburger-container">
        <i id="burger" class="fas fa-bars fa-2x">Burger</i>
      </div>
      <div class="header-logo-container">
        <h2>Logo goes here</h2>
      </div>
      <div class="header-profile-and-signin">
        <i class="fas fa-user-circle fa-2x"></i>
        <i class="fas fa-suitcase-rolling fa-2x"></i>
      </div>
      <ul id="nav">
        <li>
          <a href="search-page.html" style="text-decoration: none">Holidays</a>
        </li>
        <li>
          <a href="sign-in.html" style="text-decoration: none">Sign In / Register</a>
        </li>
      </ul>
    </div>
  </div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.