Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - Why aren't all the items in my ul displayed?

I'm trying to create a responsive navbar, but not all the items from my ul are showing,

PROBLEM:

enter image description here

    console.log('works')
    
    document.querySelector('.container__hamburger').addEventListener('click', ()=>{
        document.querySelector('.container__navbar').classList.toggle('showMenu')
    })
    @import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
    
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    .container {
      display: flex;
      background-color: #2b2f33;
      width: 100%;
      height: 65px;
    }
    
    .container__navbar {
      display: flex;
      font-family: "Roboto", sans-serif;
      width: 100%;
    }
    
    .container__logo {
      display: inline;
      width: 25%;
      margin-right: 30px;
      margin-left: 10px;
    }
    
    .navbar__list {
      display: flex;
      width: 100%;
      list-style: none;
      align-items: center;
      color: #ffffff;
    }
    
    .list__item{
      margin-right: 30px;
    }
    
    
    .container__hamburger {
      display: none;
      color: #ffffff;
      align-self: center;
    }
    
    @media (max-width: 890px) {
    
      .container {
        position: fixed;
      }
    
      .container__navbar {
        position: fixed;
        height: 100%;
        left: 0;
        top: 65px;
        transition: 0.5s;
        background-color: #2b2f33;
        transform: translateX(-100%);
        width: 250px;
      }
    
      .showMenu {
        transform: translateX(0);
      }
    
      .navbar__list {
        display: block;
        overflow-x: hidden;
        overflow-y: scroll;
      }
    
      .container__hamburger {
        display: block;
        margin-left: auto;
        margin-right: 30px;
      }
    
      .list__item {
        width: 100%;
        padding: 10px;
      }
    
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="styles.css" />
      </head>
      <body>
        <div class="container">
          <img
            class="container__logo"
            src="assets/images/gamezonia.png"
            alt="logo"
          />
          <nav class="container__navbar">
            <ul class="navbar__list">
              <li class="list__item">Home</li>
              <li class="list__item">Category 1</li>
              <li class="list__item">Category 2</li>
              <li class="list__item">Category 3</li>
              <li class="list__item">Contact</li>
              <li class="list__item">Item</li>
              <li class="list__item">Item2</li>
              <li class="list__item">Item3</li>
              <li class="list__item">Item4</li>
              <li class="list__item">Item5</li>
              <li class="list__item">Item6</li>
            </ul>
          </nav>
    
          <div class="container__hamburger">
            <i class="fas fa-bars"></i>
          </div>
        </div>
    
        <!-- FONT AWESOME -->
        <script
          src="https://kit.fontawesome.com/96fadf0e69.js"
          crossorigin="anonymous"
        ></script>
        <!-- FONT AWESOME -->
    
        <script src="./index.js"></script>
      </body>
    </html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've dropped the .container__navbar down the viewport by 65 pixels. You need to take this off the 100% height using height:calc(100% - 65px);

console.log('works')
    
    document.querySelector('.container__hamburger').addEventListener('click', ()=>{
        document.querySelector('.container__navbar').classList.toggle('showMenu')
    })
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
    
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    .container {
      display: flex;
      background-color: #2b2f33;
      width: 100%;
      height: 65px;
    }
    
    .container__navbar {
      display: flex;
      font-family: "Roboto", sans-serif;
      width: 100%;
    }
    
    .container__logo {
      display: inline;
      width: 25%;
      margin-right: 30px;
      margin-left: 10px;
    }
    
    .navbar__list {
      display: flex;
      width: 100%;
      list-style: none;
      align-items: center;
      color: #ffffff;
    }
    
    .list__item{
      margin-right: 30px;
    }
    
    
    .container__hamburger {
      display: none;
      color: #ffffff;
      align-self: center;
    }
    
    @media (max-width: 890px) {
    
      .container {
        position: fixed;
      }
    
      .container__navbar {
        position: fixed;
        height: calc(100% - 65px);
        left: 0;
        top: 65px;
        transition: 0.5s;
        background-color: #2b2f33;
        transform: translateX(-100%);
        width: 250px;
      }
    
      .showMenu {
        transform: translateX(0);
      }
    
      .navbar__list {
        display: block;
        overflow-x: hidden;
        overflow-y: scroll;
      }
    
      .container__hamburger {
        display: block;
        margin-left: auto;
        margin-right: 30px;
      }
    
      .list__item {
        width: 100%;
        padding: 10px;
      }
    
    }
<!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="styles.css" />
      </head>
      <body>
        <div class="container">
          <img
            class="container__logo"
            src="assets/images/gamezonia.png"
            alt="logo"
          />
          <nav class="container__navbar">
            <ul class="navbar__list">
              <li class="list__item">Home</li>
              <li class="list__item">Category 1</li>
              <li class="list__item">Category 2</li>
              <li class="list__item">Category 3</li>
              <li class="list__item">Contact</li>
              <li class="list__item">Item</li>
              <li class="list__item">Item2</li>
              <li class="list__item">Item3</li>
              <li class="list__item">Item4</li>
              <li class="list__item">Item5</li>
              <li class="list__item">Item6</li>
            </ul>
          </nav>
    
          <div class="container__hamburger">
            <i class="fas fa-bars"></i>
          </div>
        </div>
    
        <!-- FONT AWESOME -->
        <script
          src="https://kit.fontawesome.com/96fadf0e69.js"
          crossorigin="anonymous"
        ></script>
        <!-- FONT AWESOME -->
    
        <script src="./index.js"></script>
      </body>
    </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...