卡在导航栏功能上

时间:2020-08-25 19:47:20

标签: javascript

我试图创建折叠的导航栏,我做了HTML和CSS部分,但是当我执行javascript部分时,却给了我未捕获的参考错误。我的代码如下,我提供了HTML CSS和Javascript。如果有人可以帮忙,我将不胜感激。先感谢您。我在下面提供了所有代码。

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link

      href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;1,100&display=swap"

      rel="stylesheet"

    />

    <link rel="stylesheet" href="style.css" />

    <title>Document</title>

  </head>

  <body>

    <header>

      <img src="logo/logo.png" alt="" />

      <div class="container" onclick="btnOpen()">

        <div class="line-1"></div>

        <div class="line-2"></div>

        <div class="line-3"></div>

      </div>

      <nav>

        <ul>

          <a href="javascript:void(0)" class="closebtn" onclick="closeNav()"

            >&times;</a

          >

          <li><a href="index.html">Home</a></li>

          <li><a href="pages/about.html">About us</a></li>

          <li><a href="pages/contact.html">How to reach me</a></li>

          <li><a href="pages/portfolio.html">Portfolio</a></li>

        </ul>

      </nav>

      <section class="sm">

        <a href="#"><i class="fab fa-facebook-f" class="facebook"></i></a>

        <a href="#"><i class="fab fa-twitter" class="twitter"></i></a>

        <a href="#"><i class="fab fa-google" class="google"></i></a>

      </section>

      <main></main>

      <section></section>

      <footer></footer>

    </header>

  </body>

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

  <script

    src="https://kit.fontawesome.com/dd4d991431.js"

    crossorigin="anonymous"

  ></script>

</html>

* {

  box-sizing: border-box;

}

header {

  width: 100%;

  background-color: #e1e1e1;

}


header img {

  width: 100px;

  height: 100px;

}


header .container {

  display: inline-block;

  float: right;

}


.container .line-1,

.line-2,

.line-3 {

  width: 35px;

  height: 5px;

  background-color: #111;

  margin: 6px 0;

}


nav {

  display: flex;

  position: absolute;

  top: 100;

  right: 0;

  background-color: #e1e1e1;

  width: 40vw;

  height: 30vh;

}


nav .closebtn {

  position: absolute;

  top: 0;

  right: 25px;

  font-size: 36px;

  margin-left: 50px;

  text-decoration: none;

  color: #111;

}


nav ul {

  display: flex;

  flex-direction: column;

  align-items: center;

}


nav ul li {

  margin: 10px;

}


nav ul li a {

  text-decoration: none;

  color: #111;

  font-style: bold;

  font-weight: 600;

  font-size: 15px;

  font-family: Roboto;

}


function openBtn() {

  document.getElementsByName("nav").style.width = "40vw";

  document.getElementsByName("nav").style.height = "30vh";

}

openBtn();


function closeNav() {

  document.getElementsByName("nav").style.width = "0";

  document.getElementsByName("nav").style.height = "0";

}

closeNav();

2 个答案:

答案 0 :(得分:0)

我已将ID添加到nav并更正了js。请检查。

function btnOpen() {

  document.getElementById("nav").style.width = "40vw";

  document.getElementById("nav").style.height = "30vh";

}

btnOpen();


function closeNav() {

  document.getElementById("nav").style.width = "0";

  document.getElementById("nav").style.height = "0";

}
closeNav();
* {

  box-sizing: border-box;

}

header {

  width: 100%;

  background-color: #e1e1e1;

}


header img {

  width: 100px;

  height: 100px;

}


header .container {

  display: inline-block;

  float: right;

}


.container .line-1,

.line-2,

.line-3 {

  width: 35px;

  height: 5px;

  background-color: #111;

  margin: 6px 0;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link

      href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;1,100&display=swap"

      rel="stylesheet"

    />

    <link rel="stylesheet" href="style.css" />

    <title>Document</title>

  </head>

  <body>

    <header>

      <img src="logo/logo.png" alt="" />

      <div class="container" onclick="btnOpen()">

        <div class="line-1"></div>

        <div class="line-2"></div>

        <div class="line-3"></div>

      </div>

      <nav id='nav'>

        <ul>

          <a href="javascript:void(0)" class="closebtn" onclick="closeNav()"

            >&times;</a

          >

          <li><a href="index.html">Home</a></li>

          <li><a href="pages/about.html">About us</a></li>

          <li><a href="pages/contact.html">How to reach me</a></li>

          <li><a href="pages/portfolio.html">Portfolio</a></li>

        </ul>

      </nav>

      <section class="sm">

        <a href="#"><i class="fab fa-facebook-f" class="facebook"></i></a>

        <a href="#"><i class="fab fa-twitter" class="twitter"></i></a>

        <a href="#"><i class="fab fa-google" class="google"></i></a>

      </section>

      <main></main>

      <section></section>

      <footer></footer>

    </header>

  </body>

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

  <script

    src="https://kit.fontawesome.com/dd4d991431.js"

    crossorigin="anonymous"

  ></script>

</html>

答案 1 :(得分:0)

需要修复几件事,并且不需要任何外部库。首先,第一个div出现语法错误。您的onclick属性应指向函数openBtn(),而不是btnOpen()

第二,您的参考错误源于两个函数openBtn()closeNav()。调用document.getElementsByName('nav')时,您无法编辑返回的style的{​​{1}}属性。这就是我所做的更改。

NodeList

通过for-of循环,您可以编辑const navs = document.getElementsByTagName("nav") function openBtn() { for (const nav of navs) { nav.style.width = "40vw" nav.style.height = "30vh" } } openBtn() function closeNav() { for (const nav of navs) { nav.style.width = "0" nav.style.height = "0" } } closeNav() 返回的nav中的每个HTMLCollection元素。

这是一个可行的例子。

document.getElementsByTagName('nav')
const navs = document.getElementsByTagName("nav")
function openBtn() {
  for (const nav of navs) {
    nav.style.width = "40vw"
    nav.style.height = "30vh"
  }
}
openBtn()

function closeNav() {
  for (const nav of navs) {
    nav.style.width = "0"
    nav.style.height = "0"
  }
}
* {

      box-sizing: border-box;

}

header {

      width: 100%;

      background-color: #e1e1e1;

}


header img {

      width: 100px;

      height: 100px;

}


header .container {

      display: inline-block;

      float: right;

}


.container .line-1,

.line-2,

.line-3 {

      width: 35px;

      height: 5px;

      background-color: #111;

      margin: 6px 0;

}


nav {

      display: flex;

      position: absolute;

      top: 100;

      right: 0;

      background-color: #e1e1e1;

      width: 40vw;

      height: 30vh;

}


nav .closebtn {

      position: absolute;

      top: 0;

      right: 25px;

      font-size: 36px;

      margin-left: 50px;

      text-decoration: none;

      color: #111;

}


nav ul {

      display: flex;

      flex-direction: column;

      align-items: center;

}


nav ul li {

      margin: 10px;

}


nav ul li a {

      text-decoration: none;

      color: #111;

      font-style: bold;

      font-weight: 600;

      font-size: 15px;

      font-family: Roboto;

}