我正在用absolute位置在鼠标上为汉堡包菜单设置动画,效果很好,但我希望它平滑。我试着将转换设置为跨度,但没有成功。这是我的密码。

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>

我只希望当bar-1和bar-3从其原始位置移动时,转换是平滑的。

最佳答案

您可以这样简化代码:

#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>

关于html - 绝对位置平滑过渡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53036690/

10-13 01:36