为什么不't我的Javascript,如果其他方法有效的话

Why doesn't my Javascript if else work?

本文关键字:其他 如果 方法 有效 Javascript 我的 为什么不      更新时间:2023-09-26

我一直在摆弄这个移动友好的导航菜单,出于某种原因,我的菜单会打开(JS IF语句),但不会关闭(JS ELSE语句)。所以我一直在想为什么它打开但没有关闭?

附言:我是JavaScript的新手,所以它可能很简单,我仔细看了一遍,谢谢!

这是我正在使用的代码:

function myFunction(x) {
    x.classList.toggle("change");
}
function navChange() {
    var x = document.getElementById("myNav").style.height
    if (x = "0%"){
     document.getElementById("myNav").style.height = "100%";
    } else {
     document.getElementById("myNav").style.height = "0%";
    }
}
.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;
}
@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .closebtn {
    font-size: 40px !important;
    top: 15px;
    right: 35px;
  }
}
.container {
    position: absolute;
    top: 25px;
    left: 25px;
    display: inline-block;
    cursor: pointer;
    margin: 20px;
    z-index: 12;
}
.bar1, .bar2 {
    width: 35px;
    height: 4px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}
.change .bar1 {
    transform: rotate(-45deg) translate(0px, 0px) ;
    background-color: gray;
}
.change .bar2 {
    transform: translate(1px, -10px) rotate(45deg);
    background-color: gray;
}
<div id="myNav" class="overlay">
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>
<div class="container" onclick="myFunction(this), navChange()">
  <div class="bar1"></div>
  <div class="bar2"></div>
</div>

感谢您的时间和精力!

我看到的错误的原因是你在你的条件下做作业。

a = 15;
b = 10;
if(a = b) alert("hello world");

a现在是10,因为我们使用了赋值,而不是测试等式,这看起来像。

if( a == b)

此外,您正在测试样式属性,该属性看起来像是通过CSS设置的,style.height不会返回您想要的内容。你需要测试PX或其他什么东西。

var height =  window.getComputedStyle(document.getElementById("element")).height;
var height = parseInt(height,10);
if( height > 1510 ) {
    //do whatever
}

EasyBB所说的一切都是真的,但这里有一个如何干净地完成的例子:

function myFunction(x) {
    x.classList.toggle("change");
}
function navChange() {
    var x = document.getElementById("myNav").clientHeight; // Use clientHeight to get the actual height, ignoring style rules. //.style.height
    if (x == "0"){ // == to compare, = here would set the value, so x would always equal 0.
     document.getElementById("myNav").style.height = "100%";
    } else {
     document.getElementById("myNav").style.height = "0"; //No reason to use percentage here, as 0% =0px.
    }
}
.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;
}
@media screen and (max-height: 450px) {
  .overlay {overflow-y: auto;}
  .overlay a {font-size: 20px}
  .closebtn {
    font-size: 40px !important;
    top: 15px;
    right: 35px;
  }
}
.container {
    position: absolute;
    top: 25px;
    left: 25px;
    display: inline-block;
    cursor: pointer;
    margin: 20px;
    z-index: 12;
}
.bar1, .bar2 {
    width: 35px;
    height: 4px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}
.change .bar1 {
    transform: rotate(-45deg) translate(0px, 0px) ;
    background-color: gray;
}
.change .bar2 {
    transform: translate(1px, -10px) rotate(45deg);
    background-color: gray;
}
<div id="myNav" class="overlay">
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>
<div class="container" onclick="myFunction(this), navChange()">
  <div class="bar1"></div>
  <div class="bar2"></div>
</div>