Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

absolute position smooth transition

Tags:

html

css

I am animating a hamburger menu on mouseover with absolute position it works fine but I want it to be smooth. I tried to set transition to the span but it' didn't work. Here is my code.

const mainHeader = document.querySelector('#header');
const barOne = document.querySelector('.bar-1');
const barThree = document.querySelector('.bar-3');
mainHeader.addEventListener('mouseover', () => {
  barOne.style.top = '6px';
  barThree.style.top = '-6px';
})
#header {
  position: fixed;
  top: 5vh;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
  cursor: pointer;
}

nav.menu span {
  background: #3d3d3d;
  width: 30px;
  height: 1px;
  display: block;
  margin-bottom: 6px;
  position: relative;
  transition: all 1s;
}
<header id="header">
  <nav class="menu">
    <span class="bar-1"></span>
    <span></span>
    <span class="bar-3"></span>
  </nav>
</header>

I just want the transition to be smooth when bar -1 and bar-3 move from its original position.

like image 678
Taste of Leaving Avatar asked Mar 11 '26 04:03

Taste of Leaving


1 Answers

You can simplify your code like this:

#header {
  position: fixed;
  top: 5vh;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1;
  cursor: pointer;
}

nav.menu {
  background: 
    linear-gradient(#3d3d3d,#3d3d3d) top,
    linear-gradient(#3d3d3d,#3d3d3d) center,
    linear-gradient(#3d3d3d,#3d3d3d) bottom;
  background-size:100% 1px;
  background-repeat:no-repeat;
  width: 30px;
  height: 15px;
  transition: background-position 1s;
}
nav.menu:hover {
  background-position:center;
}
<header id="header">
  <nav class="menu">
  </nav>
</header>
like image 195
Temani Afif Avatar answered Mar 12 '26 20:03

Temani Afif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!