为什么不't我的Javascript,如果其他方法有效的话
Why doesn't my Javascript if else work?
我一直在摆弄这个移动友好的导航菜单,出于某种原因,我的菜单会打开(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>
相关文章:
- 如果其他是咖啡脚本
- 对象获胜'如果qml中的其他地方定义了数字动画属性,则t设置动画
- 用户名输入如果其他块不能正常工作/Javascript-jQuery-AJAX
- 如果匈牙利的记法大多被贬低;建设者的UpperCamel与其他一切的lowerCamel“;如此受欢迎
- 如果一个手风琴发出咔嗒声,如何最大限度地减少其他手风琴
- 如果没有其他函数链接到promise,则默认行为
- 如果..其他 引号内的语句
- 如果选择了其他选择项目,则隐藏选择项目
- 如果条件,则在其他操作之后调用 Redux 操作
- 如果单击离开页面,请重定向到其他页面
- j查询关闭 ul 如果打开了其他
- 找到所有元素,如果没有类,则使用 jQuery 添加其他类
- 如果React.js是V,那么在MVC中,其他字母是什么
- 如果所有其他元素都填写在AngularJS中,如何动态添加新的输入元素
- 如果匹配,则对数组中的每个项执行其他操作,如果使用jvectormaps
- 这是关于Jquery/JS的,如果我改变元素's HTML-我可以对它执行其他Jquery/JS操作吗
- 速记与其他如果
- 多功能 javascript 停止其他如果警报取消
- 如果工作,但不是其他如果
- 麻烦与javascript测量转换功能.“如果. .其他如果. .else"