汉堡图标不响应

时间:2018-08-08 20:50:39

标签: javascript html css

我正在尝试使我的汉堡包图标掉到导航栏上。该图标可以工作并且具有响应能力,但是覆盖层不会掉落。

https://jsfiddle.net/run1kqmj/

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

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

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
    z-index: 3;

}

.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);
}
</style>
</head>
<body>

<div id="myNav" class="overlay">
  <div class="overlay-content">
    <h4>
    <a href="https://www.invictusbaby.co.uk/">Home</a><br>
    <a href="https://www.invictusbaby.co.uk/invictus-v-plus">V-Plus</a><br>
    <a href="https://www.invictusbaby.co.uk/carbon-edition">Carbon Edition</a><br>
    <a href="https://www.invictusbaby.co.uk/special-edition">Special Edition</a><br></h4>
  </div>
</div>
<div class="container" onclick="myFunction(this);runNav()">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<script>

function myFunction(x) {
    x.classList.toggle("change");
}
function runNav() {
    if (document.getElementById("myNav").style.height = "0%");
        {document.getElementById("myNav").style.height = "100%"};
        {document.getElementById("myNav").style.height = "0%"};
}
</script>

当我手动将覆盖层设为100%时,要预览外观,我将无法与汉堡包图标进行交互。它不会执行它的动作。

我设置了(“ myNav”)。style.height =“ 5%”};它将覆盖图标,而我将无法与覆盖位置互动。

1 个答案:

答案 0 :(得分:2)

您用于启用叠加层的JavaScript已损坏。我还向叠加层添加了一些margin-top,以使图标可访问。

function myFunction(x) {
    x.classList.toggle("change");
}
function runNav() {
    if (document.getElementById("myNav").style.height == 0 || document.getElementById("myNav").style.height == "0%") {
        	document.getElementById("myNav").style.height = "100%";
        }
        else {
        	document.getElementById("myNav").style.height = "0%";
        }
}
.overlay {
    height: 0%;
    margin-top: 50px;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-y: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

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

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
    z-index: 3;
    
}

.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);
}
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <div id="myNav" class="overlay">
      <div class="overlay-content">
        <h4>
        <a href="https://www.invictusbaby.co.uk/">Home</a>          <br>
        <a href="https://www.invictusbaby.co.uk/invictus-v-plus">V-Plus</a><br>
        <a href="https://www.invictusbaby.co.uk/carbon-edition">Carbon Edition</a><br>
        <a href="https://www.invictusbaby.co.uk/special-edition">Special Edition</a><br></h4>
      </div>
    </div>
    <div class="container" onclick="myFunction(this);runNav()">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>
  </body>
</html>