【问题标题】:Hamburger menu - why is my code not working?汉堡菜单 - 为什么我的代码不起作用?
【发布时间】:2020-04-11 23:32:08
【问题描述】:

我正在尝试使用 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】:

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

    • $('burger') 必须是 $('#burger') 才能包含 id 选择器前缀
    • 同样,$('nav') 必须是 $('#nav')
    • 您的 CSS 中的 .hidden 需要为 #nav.hidden 以便它足够具体以覆盖默认样式。我还建议将其更改为 .visible 以匹配其目的,但这纯粹是语义问题。

    另一个可能的问题是您可能需要将 document.ready 事件处理程序添加到您的 JS 逻辑中,具体取决于您在页面中放置它的位置。这将确保 jQuery 代码在 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>

    【讨论】:

    • 知道这很简单 - 我已经在脚本中更改了 ID,并且它立即生效。谢谢 - 非常感谢您的快速回答。
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2018-03-16
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多