본문 바로가기

Web design

[Navbar] 햄버거 메뉴 애니메이션2 - Html, CSS, JS

728x90
반응형

⭐ 햄버거 메뉴 애니메이션2

2nd bar가 옆으로 out되고, 1st, 3rd bar가 회전하며 X표시.

 

 

See the Pen qBqZEje by Eunji Jeon (@emcjrl) on CodePen.

 

 


<Html>

<a class="menu-trigger" href="#">
  <span></span>
  <span></span>
  <span></span>
</a>

<CSS>

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #333;
}

.menu-trigger,
.menu-trigger span {
  display: inline-block;
  transition: all 0.4s;
  box-sizing: border-box;
}

.menu-trigger {
  position: relative;
  width: 50px;
  height: 44px;
}

.menu-trigger span {
  position: absolute;
  left: 0;
  width: 100%;
  height: 4px;
  background-color: #fff;
  border-radius: 4px;
}

.menu-trigger span:nth-of-type(1) {
  top: 0;
}

.menu-trigger span:nth-of-type(2) {
  top: 20px;
}

.menu-trigger span:nth-of-type(3) {
  bottom: 0;
}

/* 2th bar out 되고, 1st 3rd bar 회전하며 X */

.menu-trigger.active span:nth-of-type(1) {
  transform: translateY(20px) rotate(-45deg);
}
.menu-trigger.active span:nth-of-type(2) {
  left: 50%;
  opacity: 0;
  animation: active-menu-bar05 0.8s forwards;
}
@keyframes active-menu-bar05 {
  100% {
    height: 0;
  }
}
.menu-trigger.active span:nth-of-type(3) {
  transform: translateY(-20px) rotate(45deg);
}

<JavaScript>

const menuTrigger = document.querySelector('.menu-trigger');

menuTrigger.addEventListener('click', (event) => {
  event.currentTarget.classList.toggle('active');
});
728x90
반응형