css 图标转换汉堡包到X关闭

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css 图标转换汉堡包到X关闭相关的知识,希望对你有一定的参考价值。

<button type="button" role="button" aria-label="Toggle Navigation" class="lines-button x">
  <span class="lines"></span>
</button>
    var anchor = document.querySelectorAll('button');
    
    [].forEach.call(anchor, function(anchor){
      var open = false;
      anchor.onclick = function(event){
        event.preventDefault();
        if(!open){
          this.classList.add('close');
          open = true;
        }
        else{
          this.classList.remove('close');
          open = false;
        }
      }
    }); 
button {
  display: inline-block;
  margin: 0 1em;
  border: none;
  background: none;
  outline: none;
}
button span {
  display: block;
}
.lines-button {
  padding: 2rem 1rem;
  transition: .3s;
  cursor: pointer;
  user-select: none;
  border-radius: 0.57143rem;
  /* */
}
.lines-button:hover {
  opacity: 1;
}
.lines-button:active {
  transition: 0;
}

.lines {
  display: inline-block;
  width: 4rem;
  height: 0.57143rem;
  background: #ecf0f1;
  border-radius: 0.28571rem;
  transition: 0.3s;
  position: relative;
}
.lines:before, .lines:after {
  display: inline-block;
  width: 4rem;
  height: 0.57143rem;
  background: #ecf0f1;
  border-radius: 0.28571rem;
  transition: 0.3s;
  position: absolute;
  left: 0;
  content: '';
  -webkit-transform-origin: 0.28571rem center;
  transform-origin: 0.28571rem center;
}
.lines:before {
  top: 1rem;
}
.lines:after {
  top: -1rem;
}

.lines-button:hover .lines:before {
  top: 1.14286rem;
}
.lines-button:hover .lines:after {
  top: -1.14286rem;
}

.lines-button.x.close .lines {
  background: transparent;
}
.lines-button.x.close .lines:before, .lines-button.x.close .lines:after {
  -webkit-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  top: 0;
  width: 4rem;
}
.lines-button.x.close .lines:before {
  -webkit-transform: rotate3d(0, 0, 1, 45deg);
  transform: rotate3d(0, 0, 1, 45deg);
}
.lines-button.x.close .lines:after {
  -webkit-transform: rotate3d(0, 0, 1, -45deg);
  transform: rotate3d(0, 0, 1, -45deg);
}

以上是关于css 图标转换汉堡包到X关闭的主要内容,如果未能解决你的问题,请参考以下文章

css CSS折叠导航栏到汉堡图标

css 动画汉堡包图标

css 在汉堡包图标后添加“菜单”文字

三行菜单?纳维康?抽屉?菜单图标?侧扫?三条纹?汉堡包? [关闭]

css 汉堡手机导航图标

scss Sass汉堡到“x”关闭