Translating JQuery to Javascript: Hamburger Animation

Trying to rewrite this JQuery into Javascript for the hamburger animation.

 <div class="hamburgerLines" id="nav-icon">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>

#nav-icon{
  width: 60px;
  height: 45px;
  position: relative;
  margin: 50px auto;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#nav-icon span {
  display: block;
  position: absolute;
  height: 9px;
  width: 100%;
  background: #ed1250;
  border-radius: 9px;
  opacity: 1;
  left: 0;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .25s ease-in-out;
  -moz-transition: .25s ease-in-out;
  -o-transition: .25s ease-in-out;
  transition: .25s ease-in-out;
}


#nav-icon span:nth-child(1) {
  top: 0px;
}

#nav-icon span:nth-child(2),#nav-icon span:nth-child(3) {
  top: 18px;
}

#nav-icon span:nth-child(4) {
  top: 36px;
}

#nav-icon.open span:nth-child(1) {
  top: 18px;
  width: 0%;
  left: 50%;
}

#nav-icon.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#nav-icon.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#nav-icon.open span:nth-child(4) {
  top: 18px;
  width: 0%;
  left: 50%;
}

$(document).ready(function(){
    $('#nav-icon').click(function(){
        $(this).toggleClass('open');
    });
});

What I’ve come up with so far is:

const menuBtn = document.querySelectorAll('hamburgerLines');
let menuOpen = false;

menuBtn.addEventListener('click',()=>{
  if(!menuOpen){
    menuBtn.classList.add('open');
    menuOpen = true;
  }else{
    menuBtn.classList.remove('open');
    menuOpen = false;
  }
})

But that doesn’t work. It also doesn’t work if I use getElementByID('#nav-icon'); instead. Do I need to create a loop to go through all of the span items? Am I not taking the keyword this into account? (still a little new to JS and the this keyword is a concept I’m still working to understand completely).

1 thought on “Translating JQuery to Javascript: Hamburger Animation”

  1. The word this refers to the current object.

    For example:

    <button onclick="btnClick(this)">test</button>
    <script>
        function btnClick(button)
        {
            alert(button.innerText)
        }
    </script>
    
    const menuBtn = document.getElementById('nav-icon');
    menuBtn.addEventListener("click", function(){ 
      if(!menuBtn.classList.contains("open")){
        menuBtn.classList.add('open');
      }else{
        menuBtn.classList.remove('open');
      }
    });
    #nav-icon{
      width: 60px;
      height: 45px;
      position: relative;
      margin: 50px auto;
      -webkit-transform: rotate(0deg);
      -moz-transform: rotate(0deg);
      -o-transform: rotate(0deg);
      transform: rotate(0deg);
      -webkit-transition: .5s ease-in-out;
      -moz-transition: .5s ease-in-out;
      -o-transition: .5s ease-in-out;
      transition: .5s ease-in-out;
      cursor: pointer;
    }
    
    #nav-icon span {
      display: block;
      position: absolute;
      height: 9px;
      width: 100%;
      background: #ed1250;
      border-radius: 9px;
      opacity: 1;
      left: 0;
      -webkit-transform: rotate(0deg);
      -moz-transform: rotate(0deg);
      -o-transform: rotate(0deg);
      transform: rotate(0deg);
      -webkit-transition: .25s ease-in-out;
      -moz-transition: .25s ease-in-out;
      -o-transition: .25s ease-in-out;
      transition: .25s ease-in-out;
    }
    
    
    #nav-icon span:nth-child(1) {
      top: 0px;
    }
    
    #nav-icon span:nth-child(2),#nav-icon span:nth-child(3) {
      top: 18px;
    }
    
    #nav-icon span:nth-child(4) {
      top: 36px;
    }
    
    #nav-icon.open span:nth-child(1) {
      top: 18px;
      width: 0%;
      left: 50%;
    }
    
    #nav-icon.open span:nth-child(2) {
      -webkit-transform: rotate(45deg);
      -moz-transform: rotate(45deg);
      -o-transform: rotate(45deg);
      transform: rotate(45deg);
    }
    
    #nav-icon.open span:nth-child(3) {
      -webkit-transform: rotate(-45deg);
      -moz-transform: rotate(-45deg);
      -o-transform: rotate(-45deg);
      transform: rotate(-45deg);
    }
    
    #nav-icon.open span:nth-child(4) {
      top: 18px;
      width: 0%;
      left: 50%;
    }
    <div class="hamburgerLines" id="nav-icon">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
    </div>
    Reply

Leave a Comment