Home > Net >  Small screen menu ( show and hide ) with javascript
Small screen menu ( show and hide ) with javascript

Time:10-28

im learning css and html (my second project )and want a menu on small screen.I copied a code but it didnt really work. The menu dont want to hide its always Showed.I have no knowledge about javascript and need help and explication if you can^^. this is how its looks on small screen:

show function
hide function

var navLinks = document.getElementById("navLinks");

function showMenu() {
  navLinks.style.right = "0";
}

function hideMenu() {
  navLinks.style.right = "-200px";
}
nav {
  display: flex;
  padding: 2% 6%;
  justify-content: space-between;
  align-items: center;
}

nav img {
  width: 150px;
}

.nav-links {
  flex: 1;
  text-align: right;
}

.nav-links ul li {
  display: inline-block;
  position: relative;
  padding: 8px 12px;
}

.nav-links ul li a {
  color: var(--white);
  font-size: 16px;
}

.nav-links ul li a::after {
  content: "";
  width: 0%;
  height: 2px;
  background: var(--primary-500);
  display: block;
  margin: auto;
  transition: width 0.5s;
}

.nav-links ul li:hover a::after {
  width: 100%;
}


/*
        =============== 
        TEXT-zone
        ===============a
        */

.text-zone {
  width: 90%;
  color: var(--white);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.text-zone h1 {
  font-size: 62px;
  font-weight: bold;
}

.text-zone p {
  margin: 10px 0 40px;
  max-width: none;
  font-size: 14px;
}

.hero-btn {
  display: inline-block;
  color: var(--white);
  border: 1px solid var(--white);
  padding: 12px 34px;
  font-size: 13px;
  background: transparent;
  position: relative;
  cursor: pointer;
}

.hero-btn:hover {
  border: 1px solid var(--primary-500);
  background: var(--primary-500);
  transition: var(--transition);
}

nav .fas {
  display: none;
}

@media screen and (max-width: 992px) {
  .text-zone h1 {
    font-size: 24px;
  }
  .nav-links ul li {
    display: block;
  }
  .nav-links {
    position: absolute;
    background: var(--primary-500);
    height: 100vh;
    width: 200px;
    top: 0;
    right: -200px;
    text-align: left;
    z-index: 2;
    transition: var(--transition);
  }
  nav .fas {
    display: block;
    color: var(--white);
    font-size: 22px;
    margin: 10px;
    cursor: pointer;
  }
  .nav-links ul {
    padding: 30px;
  }
}
<section class="header">
  <nav>
    <a href="./index.html"><img src="./eduford_img/logo.png" alt="Uni logo" /></a>
    <div class="nav-links" id="navLinks">
      <i class="fas fa-times" onclick="hideMenu()"></i>
      <ul>
        <li><a href="./index.html">Home</a></li>
        <li><a href="./about.html">About</a></li>
        <li><a href="./course.html">Course</a></li>
        <li><a href="./blog.html">Blog</a></li>
        <li><a href="./contact.html">Contact</a></li>
      </ul>
    </div>
    <i class="fas fa-bars" onclick="showMenu()"></i>
  </nav>
  <div class="text-zone">
    <h1>World's bigest University</h1>
    <p>
      Lorem ipsum, dolor sit amet consectetur adipisicing elit. Tempore consequatur perferendis, facilis similique. <br /> ipsam nihil ab doloribus recusandae cupiditate earum ipsa optio est blanditiis delectus.
    </p>
    <a href="#" class="hero-btn">Visit us to know more</a>
  </div>
</section>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You just need to add a position-property. I just added in this example: #navLinks { position: fixed; }

Also note:
You should avoid to use .style function in JS in 2021. The modern approach is to use .classList .add('class-name') , .remove('class-name') or .toggle('class-name') to apply changes through CSS. This causes far less issues incl. possible specificity weight issues.

var navLinks = document.getElementById("navLinks");

function showMenu() {
  navLinks.style.right = "0";
}

function hideMenu() {
  navLinks.style.right = "-200px";
}
#navLinks {
  position: fixed;
}

/* for demonstration purpose only */
#navLinks {
  background-color: red;
}


/* original css */
nav {
  display: flex;
  padding: 2% 6%;
  justify-content: space-between;
  align-items: center;
}

nav img {
  width: 150px;
}

.nav-links {
  flex: 1;
  text-align: right;
}

.nav-links ul li {
  display: inline-block;
  position: relative;
  padding: 8px 12px;
}

.nav-links ul li a {
  color: var(--white);
  font-size: 16px;
}

.nav-links ul li a::after {
  content: "";
  width: 0%;
  height: 2px;
  background: var(--primary-500);
  display: block;
  margin: auto;
  transition: width 0.5s;
}

.nav-links ul li:hover a::after {
  width: 100%;
}


/*
        =============== 
        TEXT-zone
        ===============a
        */

.text-zone {
  width: 90%;
  color: var(--white);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.text-zone h1 {
  font-size: 62px;
  font-weight: bold;
}

.text-zone p {
  margin: 10px 0 40px;
  max-width: none;
  font-size: 14px;
}

.hero-btn {
  display: inline-block;
  color: var(--white);
  border: 1px solid var(--white);
  padding: 12px 34px;
  font-size: 13px;
  background: transparent;
  position: relative;
  cursor: pointer;
}

.hero-btn:hover {
  border: 1px solid var(--primary-500);
  background: var(--primary-500);
  transition: var(--transition);
}

nav .fas {
  display: none;
}

@media screen and (max-width: 992px) {
  .text-zone h1 {
    font-size: 24px;
  }
  .nav-links ul li {
    display: block;
  }
  .nav-links {
    position: absolute;
    background: var(--primary-500);
    height: 100vh;
    width: 200px;
    top: 0;
    right: -200px;
    text-align: left;
    z-index: 2;
    transition: var(--transition);
  }
  nav .fas {
    display: block;
    color: var(--white);
    font-size: 22px;
    margin: 10px;
    cursor: pointer;
  }
  .nav-links ul {
    padding: 30px;
  }
}
<section class="header">
  <nav>
    <a href="./index.html"><img src="./eduford_img/logo.png" alt="Uni logo" /></a>
    <div class="nav-links" id="navLinks">
      <i class="fas fa-times" onclick="hideMenu()">HIDE MENU</i>
      <ul>
        <li><a href="./index.html">Home</a></li>
        <li><a href="./about.html">About</a></li>
        <li><a href="./course.html">Course</a></li>
        <li><a href="./blog.html">Blog</a></li>
        <li><a href="./contact.html">Contact</a></li>
      </ul>
    </div>
    <i class="fas fa-bars" onclick="showMenu()">SHOW MENU</i>
  </nav>
  <div class="text-zone">
    <h1>World's bigest University</h1>
    <p>
      Lorem ipsum, dolor sit amet consectetur adipisicing elit. Tempore consequatur perferendis, facilis similique. <br /> ipsam nihil ab doloribus recusandae cupiditate earum ipsa optio est blanditiis delectus.
    </p>
    <a href="#" class="hero-btn">Visit us to know more</a>
  </div>
</section>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related