使菜单图标可从窗帘菜单中单击

时间:2018-09-20 04:47:14

标签: javascript html css

我已经设置并按预期工作了网站标题和窗帘菜单的开始部分。但是,单击菜单按钮以拉下窗帘菜单后,我似乎无法使菜单按钮显示出来/可从窗帘菜单中单击。

https://codepen.io/jnbull/pen/EerxEg

function menuButton(x) {
  x.classList.toggle("change");
}

function toggleNav() {
  var x = document.getElementById("myNav");
  if (x.style.height === "100%") {
    x.style.height = "0%";
  } else {
    x.style.height = "100%";
  }
}
/* Global */

.white {
  color: #fff
}

html,
body {
  margin: 0;
  padding: 0;
  background-color: darkgray;
}


/* Header */

header {
  width: 100%;
  min-height: 80px;
  padding: 0;
  background-color: whitesmoke;
  color: black;
  position: fixed;
  margin: 0px;
  border-bottom: 5px solid black;
}

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
}

header .logo {
  float: left;
  margin-top: 4px;
}

header .menu {
  float: right;
  margin-top: 25px;
  cursor: pointer;
  position: relative;
  z-index: 2;
}

header .title h1 {
  margin: 21.5px 0px 0px 0px;
  padding: 0;
  text-align: center;
  font-family: 'Raleway', serif;
  font-size: 30px;
}

header .title a:link,
a:visited {
  color: black;
  text-decoration: none;
}

.bar1,
.bar2,
.bar3 {
  background-color: black;
  width: 35px;
  height: 5px;
  margin-bottom: 6px;
  transition: 0.4s;
}

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

.change .bar2 {
  opacity: 0;
}

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


/* Navigation */

.overlay {
  width: 100%;
  height: 0;
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 1;
  transition: 0.5s;
  overflow: hidden;
}

.overlayContent {
  position: relative;
  text-align: center;
  top: 25%;
  height: 100%;
  margin-top: 30px;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: whitesmoke;
  display: block;
  transition: 0.3s;
}

.overlay a:hover,
a:active {
  color: darkgrey;
}
<link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">
<!-- Header Start - Includes Title, Logo, and Menu -->
<header id="top">
  <div class="container">
    <!-- Logo -->
    <div class="logo">
      <a href="#top" class="logo"><img src="images/1x/Asset 
                     1.png" alt="Initials JB"></a>
    </div>
    <!-- Menu Button -->
    <div class='menu' onclick="menuButton(this); toggleNav()">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
    <!-- Title -->
    <div class="title">
      <h1>
        <a href="#top">JADON BULL</a>
      </h1>
    </div>
  </div>
</header>
<!-- Header End -->
<!-- Navigation Start -->
<div class="container">
  <nav id="myNav" class="overlay">
    <div class="overlayContent">
      <a href="#">Overview</a>
      <a href="#">Projects</a>
      <a href="#">Services</a>
      <a href="#">About Me</a>
      <a href="#">Contact</a>
    </div>
  </nav>
</div>

2 个答案:

答案 0 :(得分:0)

以下是指向代码分叉版本的链接: https://codepen.io/jaiko86/pen/mGvyJQ

这将完成您希望通过菜单按钮执行的操作。我删除了menuButton()(并注释掉了),并更改了z-index中包含.top的{​​{1}}。关于z-index的事情是它将改变同级元素的堆叠顺序,但是您不能将其堆叠在其祖先的同级元素之上或之下。

例如,假设我具有以下元素集:

.menu

在此示例中,更改<elem-a> <nested-a1> <nested-a2> <nested-a3> </elem-a> <elem-b> <nested-b1> <nested-b2> <nested-b3> </elem-b> 中的z-index仅会影响其同级兄弟<nested-a1><nested-a2>之间的堆叠顺序。它不会对<nested-a3>或其子项产生任何影响。

答案 1 :(得分:0)

只需将位置更改为固定在父容器上的绝对位置即可。

header {
  width: 100%;
  min-height: 80px;
  padding: 0;
  background-color: whitesmoke;
  color: black;
  position: absolute;
  margin: 0px;
  border-bottom: 5px solid black;
}

function menuButton(x) {
  x.classList.toggle("change");
}

function toggleNav() {
  var x = document.getElementById("myNav");
  if (x.style.height === "100%") {
    x.style.height = "0%";
  } else {
    x.style.height = "100%";
  }
}
/* Global */

.white {
  color: #fff
}

html,
body {
  margin: 0;
  padding: 0;
  background-color: darkgray;
}


/* Header */

header {
  width: 100%;
  min-height: 80px;
  padding: 0;
  background-color: whitesmoke;
  color: black;
  position: absolute;
  margin: 0px;
  border-bottom: 5px solid black;
}

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
}

header .logo {
  float: left;
  margin-top: 4px;
}

header .menu {
  float: right;
  margin-top: 25px;
  cursor: pointer;
  position: relative;
  z-index: 2;
}

header .title h1 {
  margin: 21.5px 0px 0px 0px;
  padding: 0;
  text-align: center;
  font-family: 'Raleway', serif;
  font-size: 30px;
}

header .title a:link,
a:visited {
  color: black;
  text-decoration: none;
}

.bar1,
.bar2,
.bar3 {
  background-color: black;
  width: 35px;
  height: 5px;
  margin-bottom: 6px;
  transition: 0.4s;
}

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

.change .bar2 {
  opacity: 0;
}

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


/* Navigation */

.overlay {
  width: 100%;
  height: 0;
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 1;
  transition: 0.5s;
  overflow: hidden;
}

.overlayContent {
  position: relative;
  text-align: center;
  top: 25%;
  height: 100%;
  margin-top: 30px;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: whitesmoke;
  display: block;
  transition: 0.3s;
}

.overlay a:hover,
a:active {
  color: darkgrey;
}
<link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">
<!-- Header Start - Includes Title, Logo, and Menu -->
<header id="top">
  <div class="container">
    <!-- Logo -->
    <div class="logo">
      <a href="#top" class="logo"><img src="images/1x/Asset 
                     1.png" alt="Initials JB"></a>
    </div>
    <!-- Menu Button -->
    <div class='menu' onclick="menuButton(this); toggleNav()">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
    <!-- Title -->
    <div class="title">
      <h1>
        <a href="#top">JADON BULL</a>
      </h1>
    </div>
  </div>
</header>
<!-- Header End -->
<!-- Navigation Start -->
<div class="container">
  <nav id="myNav" class="overlay">
    <div class="overlayContent">
      <a href="#">Overview</a>
      <a href="#">Projects</a>
      <a href="#">Services</a>
      <a href="#">About Me</a>
      <a href="#">Contact</a>
    </div>
  </nav>
</div>

相关问题