单击时汉堡包菜单没有响应 (HTML/CSS/JS)

Hambuger menu does not respond when clicked (HTML/CSS/JS)

创建基本 HTML/CSS 网页。我添加了一些 CSS 和 JavaScript 功能,以便可以单击汉堡菜单图标并显示侧边栏。在实施 JavaScript 后,侧边栏不再显示在屏幕上(这是我想要的),但现在汉堡包菜单图标没有响应并且不会显示侧边栏。我查看了我的 HTML 和 CSS 但找不到问题。我的代码与我所遵循的指南的源代码相匹配。任何帮助将不胜感激。

HTML:

<!DOCTYPE html>
<html lang="en"
<head>
        <meta charset="UTF-8:">
        <meta name="viewport"
              content="width=device-width,initial-scale=1.0">
        <title>Placeholder</title>
        <link rel="stylesheet" href="stylesheet2.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
</head>
<body>

        <div class="container">
                <div class="hamburger-menu">
                        <div class="line line-1"></div>
                        <div class="line line-2"></div>
                        <div class="line line-3"></div>
                </div>
        <header class="header">
            <div class="img-wrapper">
                <img src="ThinkDirBackground1.jpg" />
            </div>
          <div class="banner">
                  <h1>Placeholder</h1>
                   <p>Placeholder <br>
                    <em>placeholder</em> <br>
                    1. Placeholder <br>
                    2. Placeholder<br>
                   </p>
                   <button>Placeholder</button>
          </div>
        </header>

        <section class="sidebar">
                <ul class="menu">
                        <li class="menu-item">
                                <a href="#" class="menu-link" 
                                data-content="Home">Home</a>
                        </li>
                        <li class="menu-item">
                                <a href="#" class="menu-link"
                                data-content="PH2">PH2</a>
                        </li>
                        <li class="menu-item">
                                <a href="#" class="menu-link"
                                data-content="PH3">PH3</a>
                        </li>
                        <li class="menu-item">
                                <a href="#" class="menu-link"
                                data-content="PH4">PH4</a>
                        </li>
                </ul>
        </section>
        </div>

<script src="https://code.jquery.com/jquery-3.5.1.js"
      integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
      crossorigin="anonymous"></script>
        <script src="script.js"></script>

</body>
</html>

CSS

* {
         margin: 0;
         padding: 0;
         outline: none;
         box-sizing: border-box;
         list-style: none;
         text-decoration: none;
}


html {
        font-size: 62.5%;
}

.hamburger-menu {
        width: 3rem;
        height: 3rem;
        position: fixed;
        top: 5rem;
        right: 5rem;
        z-index: 200;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        cursor: pointer;
        transition: right 0.7s;
}

.change .hamburger-menu {
        right: 33rem;
}


.line {
        width: 100%;
        height: .2rem;
        background-color: #FFFFFF;
        box-shadow: 0.1rem 0.2rem rgba(0,0,0, .2);
}

.line {
        background-color: rgba(0,0,0, .8);
}

.change .line-1 {
        transform: rotate(45deg)
        translate(0.3rem, 0.8rem);
}

.change .line-2 {
        opacity: 0;
        visibility: hidden;
}

.change .line-3 {
        transform: rotate(-45deg)
        translate(0.3rem, -0.8rem);
}


.header {
             width: 100%;
        height: 100vh;
        position: relative;
        perspective: 100rem;
        overflow: hidden;
}

.img-wrapper {
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,.8);
        overflow: hidden;
}

.img-wrapper img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        opacity: 0.5;
        animation-name: scale;
        animation-duration: 25s;
        filter: blur(8px);
        -webkit-filter: blur(8px);
}

@keyframes scale {
        0% {
                transform: scale(1.3);
        }
        100% {
                transform: scale(1);
        }
}


.banner {
        position: absolute;
        top: 30%;
        left: 15%;
}

.banner h1 {
        font-family: "Sans-serif";
        font-size: 9rem;
        font-weight: 300;
        font-weight: bold;
        color: #FFFFFF;
        text-shadow: 0 .3rem .5rem rgba(0,0,0, .4);
        opacity: 0;
        animation: moveBanner 1s .5s;
         animation-fill-mode: forwards;
}

.banner p {
        font-family: "Sans-serif";
        font-size: 2.5rem;
        color: #FFFFFF;
        width: 70%;
        text-shadow: 0 .3rem .5rem rgba(0,0,0, .4);
        margin-bottom: 3rem;
        opacity: 0;
        animation: moveBanner 1s .7s;
        animation-fill-mode: forwards;
}

.banner button {
        width: 20rem;
        height: 5rem;
        background-color: #FFFFFF;
        border: none;
        font-family: "Sans-serif";
        text-transform: uppercase;
        text-shadow: 0.2rem .2rem rgba(0,0,0, 0.2);
        box-shadow: 0.3rem .5rem rgba(0,0,0, 0.4);
        cursor: pointer;
        opacity: 0;
        animation: moveBanner 1s .9s;
        animation-fill-mode: forwards;

}

@keyframes moveBanner {
        0% {
                transform: translateY(40rem) rotateY(-20deg);
        }
        100% {
                transform: translateY(0) rotateY(0);
                opacity: 1;
        }
}


.sidebar {
        width: 40rem;
        height: 100vh;
        position: fixed;
        top: 0;
        right: -40rem;
        background-color: #FFFFFF;
         transition: right .5s;
}

.change .sidebar {
        right: 0;
}

.menu {
        position: absolute;
        top: 40%;
        left: 50%;
        transform: translate(-50%, -50%);
}

.menu-item {
        text-align: center;
        margin: 0 0 40px 0;

}

.menu-link {
        font-family: "Sans-serif";
        font-size: 4rem;
        color: #555;
        position: relative;
}

.menu-link::before {
        content: attr(data-content);
        position: absolute;
        top: 0;
        left: 0;
        color: #555;
        overflow: hidden;
        white-space: nowrap;
        transition: width .3s ease-in-out;
}

.menu-link:hover::before {
        width: 100%;
}

.social-media {
        position: absolute;
        bottom: 3rem;
        width: 100%;
        display: flex;
        justify-content: center;
}

.social-media i {
        font-size: 2.2rem;
        margin: 3rem;
        width: 4.5rem;
        height: 4.5rem;
        background-color: #777;
        color: #FFF;
        isplay: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        transition: background-color 0.3s;
}

.social-media i:hover {
        background-color: #c29525;
}

JavaScript

document.querySelector('.hamburger-menu').
        addEventListener('click, () => {
                document.querySelector('container').classlist.toggle('change')
        }); 

几件事:

  • 此处缺少右括号:addEventListener('click,

  • classlist应该是classList,JS属性区分大小写

  • document.querySelector('container') 正在寻找第一个 container 元素。使用 document.querySelector('.container') 获取带有 class container

    的第一个元素

document.querySelector('.hamburger-menu').addEventListener('click', () => {
document.querySelector('.container').classList.toggle('change')
});
* {
  margin: 0;
  padding: 0;
  outline: none;
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
}

html {
  font-size: 62.5%;
}

.hamburger-menu {
  width: 3rem;
  height: 3rem;
  position: fixed;
  top: 5rem;
  right: 5rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  cursor: pointer;
  transition: right 0.7s;
}

.change .hamburger-menu {
  right: 33rem;
}

.line {
  width: 100%;
  height: .2rem;
  background-color: #FFFFFF;
  box-shadow: 0.1rem 0.2rem rgba(0, 0, 0, .2);
}

.line {
  background-color: rgba(0, 0, 0, .8);
}

.change .line-1 {
  transform: rotate(45deg) translate(0.3rem, 0.8rem);
}

.change .line-2 {
  opacity: 0;
  visibility: hidden;
}

.change .line-3 {
  transform: rotate(-45deg) translate(0.3rem, -0.8rem);
}

.header {
  width: 100%;
  height: 100vh;
  position: relative;
  perspective: 100rem;
  overflow: hidden;
}

.img-wrapper {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .8);
  overflow: hidden;
}

.img-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.5;
  animation-name: scale;
  animation-duration: 25s;
  filter: blur(8px);
  -webkit-filter: blur(8px);
}

@keyframes scale {
  0% {
    transform: scale(1.3);
  }
  100% {
    transform: scale(1);
  }
}

.banner {
  position: absolute;
  top: 30%;
  left: 15%;
}

.banner h1 {
  font-family: "Sans-serif";
  font-size: 9rem;
  font-weight: 300;
  font-weight: bold;
  color: #FFFFFF;
  text-shadow: 0 .3rem .5rem rgba(0, 0, 0, .4);
  opacity: 0;
  animation: moveBanner 1s .5s;
  animation-fill-mode: forwards;
}

.banner p {
  font-family: "Sans-serif";
  font-size: 2.5rem;
  color: #FFFFFF;
  width: 70%;
  text-shadow: 0 .3rem .5rem rgba(0, 0, 0, .4);
  margin-bottom: 3rem;
  opacity: 0;
  animation: moveBanner 1s .7s;
  animation-fill-mode: forwards;
}

.banner button {
  width: 20rem;
  height: 5rem;
  background-color: #FFFFFF;
  border: none;
  font-family: "Sans-serif";
  text-transform: uppercase;
  text-shadow: 0.2rem .2rem rgba(0, 0, 0, 0.2);
  box-shadow: 0.3rem .5rem rgba(0, 0, 0, 0.4);
  cursor: pointer;
  opacity: 0;
  animation: moveBanner 1s .9s;
  animation-fill-mode: forwards;
}

@keyframes moveBanner {
  0% {
    transform: translateY(40rem) rotateY(-20deg);
  }
  100% {
    transform: translateY(0) rotateY(0);
    opacity: 1;
  }
}

.sidebar {
  width: 40rem;
  height: 100vh;
  position: fixed;
  top: 0;
  right: -40rem;
  background-color: #FFFFFF;
  transition: right .5s;
}

.change .sidebar {
  right: 0;
}

.menu {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.menu-item {
  text-align: center;
  margin: 0 0 40px 0;
}

.menu-link {
  font-family: "Sans-serif";
  font-size: 4rem;
  color: #555;
  position: relative;
}

.menu-link::before {
  content: attr(data-content);
  position: absolute;
  top: 0;
  left: 0;
  color: #555;
  overflow: hidden;
  white-space: nowrap;
  transition: width .3s ease-in-out;
}

.menu-link:hover::before {
  width: 100%;
}

.social-media {
  position: absolute;
  bottom: 3rem;
  width: 100%;
  display: flex;
  justify-content: center;
}

.social-media i {
  font-size: 2.2rem;
  margin: 3rem;
  width: 4.5rem;
  height: 4.5rem;
  background-color: #777;
  color: #FFF;
  isplay: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.social-media i:hover {
  background-color: #c29525;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8:">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>Placeholder</title>
  <link rel="stylesheet" href="stylesheet2.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" />
  <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
</head>

<body>

  <div class="container">
    <div class="hamburger-menu">
      <div class="line line-1"></div>
      <div class="line line-2"></div>
      <div class="line line-3"></div>
    </div>
    <header class="header">
      <div class="img-wrapper">
        <img src="ThinkDirBackground1.jpg" />
      </div>
      <div class="banner">
        <h1>Placeholder</h1>
        <p>Placeholder <br>
          <em>placeholder</em> <br> 1. Placeholder <br> 2. Placeholder<br>
        </p>
        <button>Placeholder</button>
      </div>
    </header>

    <section class="sidebar">
      <ul class="menu">
        <li class="menu-item">
          <a href="#" class="menu-link" data-content="Home">Home</a>
        </li>
        <li class="menu-item">
          <a href="#" class="menu-link" data-content="PH2">PH2</a>
        </li>
        <li class="menu-item">
          <a href="#" class="menu-link" data-content="PH3">PH3</a>
        </li>
        <li class="menu-item">
          <a href="#" class="menu-link" data-content="PH4">PH4</a>
        </li>
      </ul>
    </section>
  </div>

  <script src="script.js"></script>

</body>

</html>

按f12进入开发者模式,查看console.if有问题,解决问题。 可以在源码页面单步调试,网络页面进行网络交互