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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With