垂直导航栏-Onclick功能不起作用

时间:2018-11-25 09:12:22

标签: javascript html css navbar

在单击该toggleSideBar()函数时应打开,但不起作用。我将代码粘贴到下面。我不知道问题出在哪里。我认为JS部分很好。请让我知道问题所在。在过去的2个小时里,我一直在尝试解决这一问题。但是,请问问题出在哪里

function toggleSideBar() {
  document.getElementById('sidebar').classList.toggle('active');
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

img {
  position: relative;
  top: 30px;
  left: 68px;
}

#sidebar {
  position: fixed;
  width: 250px;
  height: 100vh;
  background-color: aqua;
  left: -250px;
}

#sidebar .toggle-btn {
  position: absolute;
  left: 270px;
  top: 20px;
  cursor: pointer;
}

#sidebar .toggle-btn span {
  display: block;
  width: 30px;
  height: 5px;
  background: #000;
  margin-bottom: 5px;
}

#sidebar .active {
  left: 0px;
}

ul {
  width: 100%;
  margin-top: 50px;
}

li {
  list-style: none;
  padding: 20px 70px;
  border-bottom: 1px solid antiquewhite;
  text-align: center;
}

a {
  text-decoration: none;
  color: rebeccapurple;
  font-family: 'roboto';
  font-size: 1.2rem;
  text-transform: uppercase;
  font-weight: 900;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Vertical Navigation Bar</title>
  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <div id="sidebar">
    <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/HP_logo_630x630.png" alt="hp_logo" width="100px" height="100px" class="logo" />
    <div class="toggle-btn" onclick="toggleSideBar()">
      <span></span> <span></span> <span></span>
    </div>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Trending</a></li>
      <li><a href="#">Technology</a></li>
      <li><a href="#">Websites</a></li>
      <li><a href="#">Contact</a></li>
      <li><a href="#">About Me</a></li>
    </ul>
  </div>

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

</html>

1 个答案:

答案 0 :(得分:2)

您的问题在于:

#sidebar .active {
  left: 0px;
}

#sidebar.active之间有空格表示您将ID为active的元素定位为类为sidebar的元素。相反,您需要定位同时具有ID sidebar和类active的元素。要使用类active和ID sidebar定位元素,您需要删除空格:

#sidebar.active {
  left: 0px;
}

请参见下面的工作示例:

function toggleSideBar() {
  document.getElementById('sidebar').classList.toggle('active');
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

img {
  position: relative;
  top: 30px;
  left: 68px;
}

#sidebar {
  position: fixed;
  width: 250px;
  height: 100vh;
  background-color: aqua;
  left: -250px;
}

#sidebar .toggle-btn {
  position: absolute;
  left: 270px;
  top: 20px;
  cursor: pointer;
}

#sidebar .toggle-btn span {
  display: block;
  width: 30px;
  height: 5px;
  background: #000;
  margin-bottom: 5px;
}

#sidebar.active { /* REMOVE THE SPACE ON THIS LINE */
  left: 0px;
}

ul {
  width: 100%;
  margin-top: 50px;
}

li {
  list-style: none;
  padding: 20px 70px;
  border-bottom: 1px solid antiquewhite;
  text-align: center;
}

a {
  text-decoration: none;
  color: rebeccapurple;
  font-family: 'roboto';
  font-size: 1.2rem;
  text-transform: uppercase;
  font-weight: 900;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Vertical Navigation Bar</title>
  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <div id="sidebar">
    <img src="https://upload.wikimedia.org/wikipedia/commons/6/6f/HP_logo_630x630.png" alt="hp_logo" width="100px" height="100px" class="logo" />
    <div class="toggle-btn" onclick="toggleSideBar()">
      <span></span> <span></span> <span></span>
    </div>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Trending</a></li>
      <li><a href="#">Technology</a></li>
      <li><a href="#">Websites</a></li>
      <li><a href="#">Contact</a></li>
      <li><a href="#">About Me</a></li>
    </ul>
  </div>

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

</html>