3D Perspective Hamburger Menu With JavaScript And CSS3

Category: Javascript , Menu & Navigation , Recommended | October 28, 2021
Author:omar-frontend
Views Total:654 views
Official Page:Go to website
Last Update:October 28, 2021
License:MIT

Preview:

3D Perspective Hamburger Menu With JavaScript And CSS3

Description:

A fancy 3D perspective hamburger navigation built with CSS/CSS3 and a little bit of JavaScript.

How to use it:

1. Add the hamburger menu to your webpage following the HTML structure:

<div class="container">
  <!-- Navbar -->
  <div class="navbar">
    <div class="menu">
      <h3 class="logo">Site Logo</h3>
      <div class="hamburger-menu">
        <div class="bar"></div>
      </div>
    </div>
  </div>
  <!-- Main Content Here -->
  <div class="main-container">
    <div class="main">
      <header>
        <div class="overlay">
          <div class="inner">
            Site Content Here
          </div>
        </div>
      </header>
    </div>
    <div class="shadow one"></div>
    <div class="shadow two"></div>
  </div>
  <!-- Nav Links -->
  <div class="links">
    <ul>
      <li>
        <a href="#" style="--i: 0.05s;">Home</a>
      </li>
      <li>
        <a href="#" style="--i: 0.1s;">Services</a>
      </li>
      <li>
        <a href="#" style="--i: 0.15s;">Portfolio</a>
      </li>
      <li>
        <a href="#" style="--i: 0.2s;">Testimonials</a>
      </li>
      <li>
        <a href="#" style="--i: 0.25s;">About</a>
      </li>
      <li>
        <a href="#" style="--i: 0.3s;">Contact</a>
      </li>
    </ul>
  </div>
</div>

2. The necessary CSS styles.

.container {
  min-height: 100vh;
  width: 100%;
  background-color: #485461;
  background-image: linear-gradient(135deg, #485461 0%, #28313b 74%);
  overflow-x: hidden;
  transform-style: preserve-3d;
}
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
  height: 3rem;
}
.menu {
  max-width: 72rem;
  width: 100%;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
}
.hamburger-menu {
  height: 4rem;
  width: 3rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.bar {
  width: 1.9rem;
  height: 1.5px;
  border-radius: 2px;
  background-color: #eee;
  transition: 0.5s;
  position: relative;
}
.bar:before,
.bar:after {
  content: "";
  position: absolute;
  width: inherit;
  height: inherit;
  background-color: #eee;
  transition: 0.5s;
}
.bar:before {
  transform: translateY(-9px);
}
.bar:after {
  transform: translateY(9px);
}
.main {
  position: relative;
  width: 100%;
  left: 0;
  z-index: 5;
  overflow: hidden;
  transform-origin: left;
  transform-style: preserve-3d;
  transition: 0.5s;
}
.overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgba(43, 51, 59, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
}
.container.active .bar {
  transform: rotate(360deg);
  background-color: transparent;
}
.container.active .bar:before {
  transform: translateY(0) rotate(45deg);
}
.container.active .bar:after {
  transform: translateY(0) rotate(-45deg);
}
.container.active .main {
  animation: main-animation 0.5s ease;
  cursor: pointer;
  transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
}
@keyframes main-animation {
  from {
    transform: translate(0);
  }
  to {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
}
.links {
  position: absolute;
  width: 30%;
  right: 0;
  top: 0;
  height: 100vh;
  z-index: 2;
  display: flex;
  justify-content: center;
  align-items: center;
}
ul {
  list-style: none;
}
.links a {
  text-decoration: none;
  color: #eee;
  padding: 0.7rem 0;
  display: inline-block;
  font-size: 1rem;
  font-weight: 300;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: 0.3s;
  opacity: 0;
  transform: translateY(10px);
  animation: hide 0.5s forwards ease;
}
.links a:hover {
  color: #fff;
}
.container.active .links a {
  animation: appear 0.5s forwards ease var(--i);
}
@keyframes appear {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0px);
  }
}
@keyframes hide {
  from {
    opacity: 1;
    transform: translateY(0px);
  }
  to {
    opacity: 0;
    transform: translateY(10px);
  }
}
.shadow {
  position: absolute;
  width: 100%;
  height: 100vh;
  top: 0;
  left: 0;
  transform-style: preserve-3d;
  transform-origin: left;
  transition: 0.5s;
  background-color: white;
}
.shadow.one {
  z-index: -1;
  opacity: 0.15;
}
.shadow.two {
  z-index: -2;
  opacity: 0.1;
}
.container.active .shadow.one {
  animation: shadow-one 0.6s ease-out;
  transform: perspective(1300px) rotateY(20deg) translateZ(215px) scale(0.5);
}
@keyframes shadow-one {
  0% {
    transform: translate(0);
  }
  5% {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
  100% {
    transform: perspective(1300px) rotateY(20deg) translateZ(215px) scale(0.5);
  }
}
.container.active .shadow.two {
  animation: shadow-two 0.6s ease-out;
  transform: perspective(1300px) rotateY(20deg) translateZ(120px) scale(0.5);
}
@keyframes shadow-two {
  0% {
    transform: translate(0);
  }
  20% {
    transform: perspective(1300px) rotateY(20deg) translateZ(310px) scale(0.5);
  }
  100% {
    transform: perspective(1300px) rotateY(20deg) translateZ(120px) scale(0.5);
  }
}
.container.active .main:hover + .shadow.one {
  transform: perspective(1300px) rotateY(20deg) translateZ(230px) scale(0.5);
}
.container.active .main:hover {
  transform: perspective(1300px) rotateY(20deg) translateZ(340px) scale(0.5);
}

3. Enable the hamburger button to toggle the 3D menu.

const hamburger_menu = document.querySelector(".hamburger-menu");
const container = document.querySelector(".container");
    hamburger_menu.addEventListener("click", () => {
    container.classList.toggle("active");
});

You Might Be Interested In:


Leave a Reply