Search code examples
javascriptcsshamburger-menu

Clicking menu icon makes the page jump back up to the top


First post here. Sorry if I am making silly mistakes. But the problem I am having is the page jumping back up to the top whenever I click the burger icon. I really want the page to stay where the user is while the menu slides from the right. I don't know if I am tackling this burger menu in the most efficient way.

Any advice at all will be great

Thanks in advance

function openSlideMenu(){
    document.getElementById('menu').style.width = '250px';
    document.getElementById('content').style.marginRight = '250px';
}

function closeSlideMenu(){
    document.getElementById('menu').style.width = '0';
    document.getElementById('content').style.marginRight = '0';
}
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 100;
  background-color: #000;
  color: #fff;
  padding: 1rem;
}

.nav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  right: 0;
  background-color: #000;
  overflow-x: hidden;
  padding-top: 60px;
  transition: 0.7s;
}

.nav a {
  display: block;
  padding: 20px 30px;
  font-size: 20px;
  text-decoration: none;
  color: #ccc;
}

.nav a:hover {
  color: #fff;
  transition: 0.4s;
}

.nav .close {
  position: absolute;
  top: 0;
  right: 0;
}

.slide a {
  color: #fff;
  position: absolute;
  top: 10px;
  right: 10px;
}

.slide a .menu {
  padding-left: 0.4rem;
}

.overlay {
  background-color: #000;
  z-index: 5;
}
<body>
    <div class="overlay">
      <header id="content">
        <div class="logo">Industrious</div>
        <nav>
          <span class="slide">
            <a href="#" onclick="openSlideMenu()">
              <i class="fas fa-bars"></i><span class="menu">Menu</span>
            </a>
          </span>

          <div id="menu" class="nav">
            <a href="#" class="close" onclick="closeSlideMenu()">
              <i class="fas fa-times"></i>
            </a>
            <a href="#">Home</a>
            <a href="#">Elements</a>
            <a href="#">Generic</a>
          </div>
        </nav>
      </header>
    </div>


Solution

  • You have to stop the default event from happening. Two ways of doing this. One simple way is to href to javascript:;.

    function openSlideMenu() {
      document.getElementById('menu').style.width = '250px';
      document.getElementById('content').style.marginRight = '250px';
    }
    
    function closeSlideMenu() {
      document.getElementById('menu').style.width = '0';
      document.getElementById('content').style.marginRight = '0';
    }
    header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 100;
      background-color: #000;
      color: #fff;
      padding: 1rem;
    }
    
    .nav {
      height: 100%;
      width: 0;
      position: fixed;
      z-index: 1;
      top: 0;
      right: 0;
      background-color: #000;
      overflow-x: hidden;
      padding-top: 60px;
      transition: 0.7s;
    }
    
    .nav a {
      display: block;
      padding: 20px 30px;
      font-size: 20px;
      text-decoration: none;
      color: #ccc;
    }
    
    .nav a:hover {
      color: #fff;
      transition: 0.4s;
    }
    
    .nav .close {
      position: absolute;
      top: 0;
      right: 0;
    }
    
    .slide a {
      color: #fff;
      position: absolute;
      top: 10px;
      right: 10px;
    }
    
    .slide a .menu {
      padding-left: 0.4rem;
    }
    
    .overlay {
      background-color: #000;
      z-index: 5;
    }
    <div class="overlay">
      <header id="content">
        <div class="logo">Industrious</div>
        <nav>
          <span class="slide">
            <a href="javascript:;" onclick="openSlideMenu()">
              <i class="fas fa-bars"></i>
              <span class="menu">Menu</span>
            </a>
          </span>
    
          <div id="menu" class="nav">
            <a href="javascript:;" class="close" onclick="closeSlideMenu()">
              <i class="fas fa-times"></i>
            </a>
            <a href="#">Home</a>
            <a href="#">Elements</a>
            <a href="#">Generic</a>
          </div>
        </nav>
      </header>
    </div>

    The second and right way is to do event.preventDefault(). If you're using an unobtrusive event listener, you can do something like:

    elem.onClick = function (e) {
      e.preventDefault();
      document.getElementById('menu').style.width = '250px';
      document.getElementById('content').style.marginRight = '250px';
    }