Hamburger

A simple hamburger menu with transition

by Arjun Singh

HTML

<div class="hamburger">
	<div class="bar"></div>
  <div class="bar"></div>
  <div class="bar"></div>
</div>

SCSS

.hamburger{
  cursor: pointer;
  
  position: absolute;
  top: 16px;
  right: 16px;
  
  height: 40px;
  width: 40px;
  
  border: 2px solid black;
  border-radius: 4px;
  
  padding: 16px;
  
  display: flex;
  align-items: center;
  justify-content: center;
  
  gap: 8px;
  
  flex-direction: column;
  
  .bar{
    background-color: black;
    height: 2px;
    width: 100%;
    
    transition: all ease 0.25s;
    
    transform-origin: 50% 50%;
  }
  
  &.opened{
    .bar:first-of-type{
      transform: translateY(10px) rotate(45deg);
    }
    
    .bar:last-of-type{
      transform: translateY(-10px) rotate(-45deg);
    }
    
    .bar:nth-of-type(2){
      opacity: 0;
    }
  }
}

JavaScript

const hamburger = document.querySelector(".hamburger");

hamburger.onclick = handle;

function handle(){
	hamburger.classList.toggle("opened");
}