Home > Software engineering >  Navigation bar does not work (with JS, css, HTML)
Navigation bar does not work (with JS, css, HTML)

Time:01-19

[ADDITIONAL BELOW]

I have done my first portfolio on my won and my code of navigation bar with JS, css, HTML doesn't work even I tried everything out for many times.

The navigation bar shows up but the event doesn't happen.(and the menu shows up every time I load the page.) Maybe because the code of JavaScript wrong? Could anyone help me out please?

// JavaScript //

document
  .querySelector('.btn-mobile-nav')
  .addEventListener('click', function () {
    console.log('.btn-mobile-nav');
    document.querySelector('.mobile-menu').classList.toggle('.is-active');
  }); 
// HTML //

 <nav >
        <div >
          <ul >
            <li>
              <a  href="#intro"
                >introduce</a
              >
            </li>
            <li>
              <a  href="#aboutme"
                >about me</a
              >
            </li>
            <li>
              <a  href="#work">work</a>
            </li>
            <li>
              <a  href="#skill">skill</a>
            </li>
          </ul>
        </div>
      </nav>

      <button >
        <ion-icon  name="menu-outline"></ion-icon>
        
      </button>
// CSS //

  .btn-mobile-nav {
    opacity: 1;
    position: fixed;
    color: #ccc;
    background-color: rgba(255, 255, 255, 0.658);
    border: none;
    height: 3.2rem;
    width: 3.2rem;

    top: 0.2rem;
    right: 0.2rem;
    z-index: 2;
  }

  .icon-mobile-open {
    height: 2.5rem;
    width: 2.5rem;
  }

  .mobile-menu {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 1;
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;

    align-items: center;
    justify-content: center;
    background: #555;
  }

  .mobile-menu-item {
    width: 100%;
    height: auto;
    padding: 0.5em 1em;
    text-align: center;
    color: #fff;

    box-sizing: border-box;
  }

  /* before */
  .mobile-menu {
    pointer-events: none;
    transition: opacity 0.3s linear;
    opacity: 0;
  }

  /* after  */
  .mobile-menu .is-active {
    pointer-events: auto;
    opacity: 1;
  }

[ADDITIONAL QUESTION]

And one more question... I want to close this navigation when the list item is clicked but only the first list item works and other does not...

document.querySelector('a[href^="#"]').addEventListener('click', function () {
  document.querySelector('.mobile-menu').classList.remove('is-active');
});

CodePudding user response:

There are two issues here:

  • You need to pass in a class name (not a selector) to .toggle() (Remove the . from the toggle('.is-active'))
  • You need to select the same element with both the mobile-menu and is-active classes in your CSS (Done by removing the space between .mobile-menu .is-active)

document
  .querySelector('.btn-mobile-nav')
  .addEventListener('click', function() {
    console.log('.btn-mobile-nav');
    document.querySelector('.mobile-menu').classList.toggle('is-active');
  });
.btn-mobile-nav {
  opacity: 1;
  position: fixed;
  color: #ccc;
  background-color: rgba(255, 255, 255, 0.658);
  border: none;
  height: 3.2rem;
  width: 3.2rem;
  top: 0.2rem;
  right: 0.2rem;
  z-index: 2;
  
  /*Added to make the example clearer*/
  border: 1px solid #000;
}

.icon-mobile-open {
  height: 2.5rem;
  width: 2.5rem;
}

.mobile-menu {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 1;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #555;
}

.mobile-menu-item {
  width: 100%;
  height: auto;
  padding: 0.5em 1em;
  text-align: center;
  color: #fff;
  box-sizing: border-box;
}


/* before */

.mobile-menu {
  pointer-events: none;
  transition: opacity 0.3s linear;
  opacity: 0;
}


/* after  */

.mobile-menu.is-active {
  pointer-events: auto;
  opacity: 1;
}
<nav >
  <div >
    <ul >
      <li>
        <a  href="#intro">introduce</a>
      </li>
      <li>
        <a  href="#aboutme">about me</a>
      </li>
      <li>
        <a  href="#work">work</a>
      </li>
      <li>
        <a  href="#skill">skill</a>
      </li>
    </ul>
  </div>
</nav>

<button >
  <ion-icon  name="menu-outline"></ion-icon>
</button>

CodePudding user response:

Can I add one more question here? I want to close this navigation when the list item is clicked but only the first list item works and other does not...

document.querySelector('a[href^="#"]').addEventListener('click', function () {
  document.querySelector('.mobile-menu').classList.remove('is-active');
});
  •  Tags:  
  • Related