Home > Software design >  CSS Navbar transition from Kebab to X
CSS Navbar transition from Kebab to X

Time:11-22

Hey out there reading,

to make my navbar looking fancier on mobile i wanted a Kebab that transforms into a X when clicking. Unfortunately the transition isnt turning out that well probably because of my CSS settings of the Kebab. I already tried changing the width and the translate height still the problem remains. You can watch the snippet to see the problem. Anyone knows a Solution for this?

Thanks and have a great day

Noel

const navToggle = document.querySelector(".hamburger")
navToggle.addEventListener("click", () => {
  const visibility = navToggle.getAttribute("aria-expanded");

  if (visibility == "false"){
    navToggle.setAttribute("aria-expanded", true);
  }
  else {
    navToggle.setAttribute("aria-expanded", false);
  }
});
body {
  width: 100vw;
  height: 100vh;
  margin: 0;
  background: #000;
  overflow-x: hidden;
}
header {
  position: fixed;
  width: 100%;
  height: 5rem;
  z-index: 10000;
  padding: 0 2rem 5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.hamburger {
  display: block;
  position: relative;
  z-index: 1;
  user-select: none;
  border: none;
  appearance: none;
  background: none;
  cursor: pointer;
  overflow: hidden;
}
.hamburger span {
  display: block;
  width: 4px;
  height: 4px;
  margin-bottom: 5px;
  background-color: #fff;
  border-radius: 6px;
  z-index: 1px;
  transform-origin: 0 0;
  transition: 0.4s;
}
.hamburger[aria-expanded="true"] span:nth-child(1) {
  width: 33px;
  transform: translateY(0px) rotate(45deg);
  transition-delay: 0.125s;
}
.hamburger[aria-expanded="true"] span:nth-child(2) {
  transform: translateX(40px);
}
.hamburger[aria-expanded="true"] span:nth-child(3) {
  width: 33px;
  transform: translateY(5px) rotate(315deg);
  transition-delay: 0.125s;
}
<body>
  <header>
    <nav>
      <button aria-controls="primary-navbar" aria-expanded="false" class="hamburger">
        <span></span>
        <span></span>
        <span></span>
      </button>        
    </nav>
  </header>
</body>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

To .hamburger[aria-expanded="true"] span:nth-child(3) I have added translateX(-2px).

const navToggle = document.querySelector(".hamburger")
navToggle.addEventListener("click", () => {
  const visibility = navToggle.getAttribute("aria-expanded");

  if (visibility == "false"){
    navToggle.setAttribute("aria-expanded", true);
  }
  else {
    navToggle.setAttribute("aria-expanded", false);
  }
});
body {
  width: 100vw;
  height: 100vh;
  margin: 0;
  background: #000;
  overflow-x: hidden;
}
header {
  position: fixed;
  width: 100%;
  height: 5rem;
  z-index: 10000;
  padding: 0 2rem 5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.hamburger {
  display: block;
  position: relative;
  z-index: 1;
  user-select: none;
  border: none;
  appearance: none;
  background: none;
  cursor: pointer;
  overflow: hidden;
}
.hamburger span {
  display: block;
  width: 4px;
  height: 4px;
  margin-bottom: 5px;
  background-color: #fff;
  border-radius: 6px;
  z-index: 1px;
  transform-origin: 0 0;
  transition: 0.4s;
}
.hamburger[aria-expanded="true"] span:nth-child(1) {
  width: 33px;
  transform: translateY(0px) rotate(45deg);
  transition-delay: 0.125s;
}
.hamburger[aria-expanded="true"] span:nth-child(2) {
  transform: translateX(40px);
}
.hamburger[aria-expanded="true"] span:nth-child(3) {
  width: 33px;
  transform: translateY(5px) translateX(-2px) rotate(315deg);
  transition-delay: 0.125s;
}
<body>

  <header>
    <nav>
      <button aria-controls="primary-navbar" aria-expanded="false" class="hamburger">
        <span></span>
        <span></span>
        <span></span>
      </button>        
    </nav>
  </header>

</body>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

The main problem was with transform-origin: 0 0 your margin-bottom. Also the translateY on the last bar was a bad idea.

I managed to change some details and use absolute position to fix your problem, here is the result pen : https://codepen.io/camillewemajin/pen/YzxMrQJ

  •  Tags:  
  • css
  • Related