You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

响应式Navbar汉堡图标点击无菜单显示问题排查求助

响应式汉堡菜单点击无反应问题修复

问题概述

搭建个人网站时,响应式Navbar在窗口缩小后汉堡图标正常显示,但点击后菜单无法弹出,相关代码及修复方案如下:

修复后Index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <link
      href="https://fonts.googleapis.com/css2?family=Lora&display=swap"
      rel="stylesheet"
    />
    <title>Myfirstwebsite</title>
  </head>
  <body>
    <header>
      <nav class="container">
        <!-- Logo -->
        <div class="logo">
          <a href="index.html">
            <img
              src="images/logo.png"
              alt=" Myfirstwebsitelogo"
              class="nav-logo"
            />
            Myfirstwebsite
          </a>
        </div>
        <!-- Navigation Menu -->
        <ul class="menu">
          <li><a href="about.html">About</a></li>
          <li><a href="articles.html">Articles</a></li>
          <li><a href="use-cases.html">Use Cases</a></li>
          <li><a href="resources.html">Resources</a></li>
          <li><a href="blog.html">Blog</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
        <!-- Hamburger Icon -->
        <div class="hamburger-menu">
          <span></span>
          <span></span>
          <span></span>
        </div>
        <!-- Mobile Navigation Menu -->
        <ul class="mobile-menu">
          <li><a href="about.html">About</a></li>
          <li><a href="articles.html">Articles</a></li>
          <li><a href="use-cases.html">Use Cases</a></li>
          <li><a href="resources.html">Resources</a></li>
          <li><a href="blog.html">Blog</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

    <footer>
      <!-- Add your footer content here -->
    </footer>

    <script src="scripts.js"></script>
  </body>
</html>

修复后styles.css

/* 全局样式置顶,确保盒模型规则优先生效 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Header */
header {
  width: 100%;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  position: relative; /* 给绝对定位的子元素提供参考容器 */
}

.nav-logo {
  max-width: 50px;
  max-height: 50px;
  margin-right: 8px;
  vertical-align: middle;
}

/* Navigation Menu */
.menu {
  list-style: none;
  display: flex;
  align-items: center;
}

.menu li a {
  text-decoration: none;
  color: #111131;
  padding: 0.5rem 1rem;
  display: inline-block;
}

.menu li a:hover {
  background-color: #ff3d0d;
  color: #fff;
  border-radius: 4px;
}

/* Main Content */
main {
  padding: 2rem;
}

/* Container */
.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* Hamburger Icon */
.hamburger-menu {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 25px;
  height: 15px;
  cursor: pointer;
}

.hamburger-menu span {
  width: 100%;
  height: 3px;
  background-color: #111131;
  transition: all 0.3s ease; /* 添加切换动画 */
}

/* Mobile Navigation Menu */
.mobile-menu {
  display: none;
  list-style: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #fff;
  width: 100%;
  padding: 1rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 999;
}

.mobile-menu li {
  margin-bottom: 1rem;
}

.mobile-menu li a {
  text-decoration: none;
  color: #111131;
  font-size: 1rem;
}

.mobile-menu li a:hover {
  color: #5c5cff;
}

/* Responsive styles */
@media screen and (max-width: 1024px) {
  .hamburger-menu {
    display: flex;
  }

  .menu {
    display: none;
  }

  .mobile-menu.active {
    display: block;
  }
}

/* 补全汉堡图标切换动画 */
.hamburger-menu.active span:nth-child(1) {
  transform: translateY(6px) rotate(45deg);
}

.hamburger-menu.active span:nth-child(2) {
  opacity: 0;
}

.hamburger-menu.active span:nth-child(3) {
  transform: translateY(-6px) rotate(-45deg);
}

/* 文章图片响应式样式 */
.article-image img {
  width: 100%;
  height: auto;
  max-width: 768px;
  display: block;
  margin: 0 auto;
}

保留原scripts.js逻辑

document.addEventListener('DOMContentLoaded', function () {
  const hamburgerMenu = document.querySelector('.hamburger-menu');
  const mobileMenu = document.querySelector('.mobile-menu');

  function toggleMenu() {
    hamburgerMenu.classList.toggle('active');
    mobileMenu.classList.toggle('active');
  }

  hamburgerMenu.addEventListener('click', toggleMenu);

  mobileMenu.querySelectorAll('a').forEach(function (menuItem) {
    menuItem.addEventListener('click', toggleMenu);
  });
});

核心修复点

  • CSS语法修复:补全未写完的汉堡图标动画样式,解决后续CSS规则失效问题
  • 布局规则调整:将全局样式移至最顶部,给nav添加相对定位,确保移动端菜单定位正确
  • 清理冗余代码:移除HTML头部重复的脚本引用,避免逻辑重复执行
  • 添加过渡动画:给汉堡图标添加平滑切换效果

内容的提问来源于stack exchange,提问作者Rob m2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:32:00