简体   繁体   English

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

[英]Hamburger menu - why is my code not working?

I'm trying to do a simple hamburger menu using jQuery or JS (I'm easy either way) but my code simply won't work and I can't for the life of me work out why.我正在尝试使用 jQuery 或 JS 做一个简单的汉堡菜单(无论哪种方式我都很简单),但我的代码根本无法工作,我终生无法找出原因。

It's structured as hamburger top left, logo in the middle then a profile and basket top right.它的结构是左上角的汉堡包,中间的标志,然后是轮廓和右上角的篮子。

At the moment all I'm trying to do is literally get the nav list to appear on click and then I can style it however.目前我要做的就是让导航列表在点击时出现,然后我可以设置它的样式。 I'm trying to do this using the.hidden class to display the nav as block when the burger is clicked.我正在尝试使用.hidden class 来执行此操作,以便在单击汉堡时将导航显示为块。

Here's the code:这是代码:

HTML: 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: 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: 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>

Any help would be really appreciated.任何帮助将非常感激。 Thank you.谢谢你。

There's at least three issues in your code:您的代码中至少存在三个问题:

  • $('burger') needs to be $('#burger') to include the id selector prefix $('burger')需要是$('#burger')才能包含 id 选择器前缀
  • Similarly, $('nav') needs to be $('#nav')同样, $('nav')需要是$('#nav')
  • .hidden in your CSS needs to be #nav.hidden so that it's specific enough to override the default style. .hidden在您的 CSS 中需要为#nav.hidden ,以便它足够具体以覆盖默认样式。 I'd also suggest changing it to .visible to match its purpose, but that's purely a semantics issue.我还建议将其更改为.visible以匹配其目的,但这纯粹是语义问题。

The other possible issue is that you may need to add a document.ready event handler to your JS logic, depending on where you've placed it in the page.另一个可能的问题是您可能需要将 document.ready 事件处理程序添加到您的 JS 逻辑中,具体取决于您在页面中放置它的位置。 This will ensure the jQuery code executes after the DOM has been fully loaded.这将确保 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM