跳至主要內容

汉堡菜单按钮动画

望间代码HTML&CSS按钮动画小于 1 分钟

汉堡菜单按钮动画

效果

<div class="container">
  <div class="btn-case">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
  </div>
</div>
.container {
  position: relative;
  height: 40px;
  width: 100%;
  display: flex;
  justify-content: center;
}
.btn-case {
  width: 48px;
  height: 36px;
  display: block;
  position: relative;
  cursor: pointer;
}
.btn-case .line {
  width: inherit;
  height: 4px;
  border-radius: 2px;
  display: block;
  background-color: #3d3d3d;
  position: absolute;
  top: 0;
  transition: all 0.2s ease-in-out;
}
.btn-case .line:nth-of-type(2) {
  top: 16px;
}
.btn-case .line:nth-of-type(3) {
  top: 32px;
}
.btn-case:hover .line:nth-of-type(1) {
  transform: rotate(45deg);
  top: 16px;
}
.btn-case:hover .line:nth-of-type(2) {
  width: 0;
}
.btn-case:hover .line:nth-of-type(3) {
  transform: rotate(-45deg);
  top: 16px;
}

实现思路

使用 transition 过渡属性和 transform 变形属性,让汉堡图标和关闭图标之间进行过渡、变形,形成视觉效果。

为了方便演示,我使用了鼠标悬浮 :hover 来触发动画,实际业务需要做出一些修改。

上次编辑于:
贡献者: ViewRoom