简体   繁体   English

JS,CSS菜单栏切换

[英]JS, CSS Menubar Toggle

Hello I want to make a Menu thats pops out of the side when I click a button. 您好,我想在单击按钮时弹出一个弹出菜单。 I have it all set up with the CSS but the Javascript part doesn't work. 我已经用CSS设置了所有功能,但Javascript部分无效。 I want to test if the width of menubarWrapper is equal to 300 then the width of the menubarWrapper needs to change to 0px and if it isn't equal to 300px than change it to 300px. 我想测试menubarWrapper的宽度是否等于300,那么menubarWrapper的宽度需要更改为0px,如果不等于300px,则需要将其更改为300px。 I have the following JS: 我有以下JS:

function menuBarToggle() {
var menuWrapper = document.getElementById('menuWrapper');
if menuWrapper.width == 300 {
    menuWrapper.style.width = "0";
} else {
    menuWrapper.style.width = "300";
    }
}

I also tried in the IF statement menuWrapper.style.width but that doesn't work also 我也在IF语句menuWrapper.style.width尝试过,但这也不起作用

There are other answers that are just fine -- 还有其他很好的答案-

  • use "300px", not "300" 使用“ 300px”,而不是“ 300”
  • Surround your conditional with a parentheses. 用括号括住您的条件。

(You'll need both, by the way.) (顺便说一下,您将同时需要两者。)

But I wanted to make sure that somewhere on this page, someone pointed out that this is a very brittle way of toggling. 但是我想确保在页面的某处有人指出这是一种非常脆弱的切换方式。 You have a magical, hardcoded integer for the size, which might break if you ever wanted to style things differently. 您有一个神奇的,硬编码的大小整数,如果您想对样式进行不同的样式,则该整数可能会中断。 And if you decide one day to fade out the menu, then the test won't work at all. 而且,如果您决定某一天淡出菜单,则该测试将根本无法进行。

Might I suggest that, instead, you create two classes in CSS: 可能我建议您在CSS中创建两个类:

.menu-item { width: 300px; }
.menu-item.collapsed { width: 0; }

And then in javascript, you'll only have to write the following: 然后,在javascript中,您只需编写以下内容:

function menuBarToggle() {
  var menuWrapper = document.getElementById('menuWrapper');
  menuWrapper.classList.toggle('collapsed');      
}

Not only is the intention easier to read, but this will allow you to swap out the behavior if you decide that, instead of purely narrowing the menu, you might want it to fade out, or animate it to the left, or... well... whatever can come up with. 意图不仅易于阅读,而且如果您决定,则可以交换行为,而不是纯粹缩小菜单范围,而可能希望它淡出或将其动画化到左侧,或者...好...什么都可以想到。

When changing the width of an element via style.width , you have to append px to the end of the string: 通过style.width更改元素的width时,必须将px附加到字符串的末尾:

function menuBarToggle() {

    var menuWrapper = document.getElementById('menuWrapper');
    if menuWrapper.width == 300 {
        menuWrapper.style.width = "0px";
    } else {
        menuWrapper.style.width = "300px";
        }
    }

Your script has a typo. 您的脚本有错字。 Add '()' for an if statement. 为if语句添加'()'。

function menuBarToggle() {
    var menuWrapper = document.getElementById('menuWrapper');
    if (menuWrapper.width == 300) {
        menuWrapper.style.width = "0";
    } else {
        menuWrapper.style.width = "300";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM