如何使用classList toogle隐藏/显示菜单

时间:2018-06-08 08:11:30

标签: javascript html css

我正在创建一个显示和隐藏菜单的交互式汉堡包按钮。我想在这里实现的是当我点击汉堡包按钮,菜单出现时,当我点击转换“X”按钮时,菜单隐藏自己反之亦然。我会很感激提示。非常感谢你。

function myFunction(y) {
  y.classList.toggle("change");
}
.container {
  display: inline-block;
  cursor: pointer;
}

[class*="bar"] {
  width: 35px;
  height: 5px;
  background-color: #000a;
  margin: 6px 0;
  transition: 0.4s;
}


/* Rotate first bar */

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}


/* Menu */

.menu {
  padding: 50px 0;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50px;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  transition: 0.3s;
}

.menu>a {
  text-align: center;
  margin: 50px 10px;
  display: block;
  color: #fffa;
  font-size: 1.5rem;
  text-decoration: none;
  transition: 0.5s;
}
<div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="menu" class="menu">
  <a href="#">Home</a>
  <a href="#">Work</a>
  <a href="#">About</a>
</div>

2 个答案:

答案 0 :(得分:1)

除了连接这两个外,你已经完成了所有工作。您只需添加以下CSS:

.menu {
  display: none;
}

.change + .menu {
  display: block;
}

function myFunction(y) {
  y.classList.toggle("change");
}
.container {
  display: inline-block;
  cursor: pointer;
}

[class*="bar"] {
  width: 35px;
  height: 5px;
  background-color: #000a;
  margin: 6px 0;
  transition: 0.4s;
}


/* Rotate first bar */

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}


/* Menu */

.menu {
  padding: 50px 0;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50px;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  transition: 0.3s;
}

.menu>a {
  text-align: center;
  margin: 50px 10px;
  display: block;
  color: #fffa;
  font-size: 1.5rem;
  text-decoration: none;
  transition: 0.5s;
}

.menu {
  display: none;
}

.change + .menu {
  display: block;
}
<div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="menu" class="menu">
  <a href="#">Home</a>
  <a href="#">Work</a>
  <a href="#">About</a>
</div>

<强>加成

如果你想要一个漂亮的渐变动画,你也可以使用不透明度。

function myFunction(y) {
  y.classList.toggle("change");
}
.container {
  display: inline-block;
  cursor: pointer;
}

[class*="bar"] {
  width: 35px;
  height: 5px;
  background-color: #000a;
  margin: 6px 0;
  transition: 0.4s;
}


/* Rotate first bar */

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}


/* Menu */

.menu {
  padding: 50px 0;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50px;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  transition: 0.3s;
}

.menu>a {
  text-align: center;
  margin: 50px 10px;
  display: block;
  color: #fffa;
  font-size: 1.5rem;
  text-decoration: none;
  transition: 0.5s;
}

.menu {
  opacity: 0;
  transition: opacity 0.5s linear;
}

.change + .menu {
  opacity: 1;
}
<div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="menu" class="menu">
  <a href="#">Home</a>
  <a href="#">Work</a>
  <a href="#">About</a>
</div>

答案 1 :(得分:0)

请尝试此代码

function myFunction(y) {
	y.classList.toggle("change");
}
.container {
  display: inline-block;
  cursor: pointer;
}
[class*="bar"] {
  width: 35px;
  height: 5px;
  background-color: #000a;
  margin: 6px 0;
  transition: 0.4s;
}
.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 7px);
  transform: rotate(-45deg) translate(-9px, 7px);
}
.change .bar2 {
  opacity: 0;
}
.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -7px);
  transform: rotate(45deg) translate(-8px, -7px);
}
.menu {
  padding: 50px 0;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50px;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  transition: 0.3s;
}
.menu>a {
  text-align: center;
  margin: 50px 10px;
  display: block;
  color: #fffa;
  font-size: 1.5rem;
  text-decoration: none;
  transition: 0.5s;
}
.menu {
  max-height: 0;
  padding: 0;
}
.change + .menu {
  max-height: 500px;
} 
.change + .menu,
.change ,
.menu {
	-webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
   -ms-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}
<div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="menu" class="menu">
  <a href="#">Home</a>
  <a href="#">Work</a>
  <a href="#">About</a>
</div>