JavaScript:关于在打开/关闭菜单时改进此代码的任何提示
JavaScript: any tips on improving this code on opening/closing menu?
我潜伏w3schools并研究javaScript已经有一段时间了。几天来,我一直在为一个代码而挣扎,该代码的功能是打开,然后再次点击关闭打开的菜单。我不能用一首单曲做到这一点,但我用两首歌做到了。
我用以下方法做到了这一点:
<div id="menuClosed" style="background: blue; color: white; width: 500px; height: 50px; transition: 0.3s">
<p id="menuString" style="margin: auto; text-align:center;">
Click on the button to open the menu
</p>
<button id="menuButton" onclick="changeStyle('Closed')" style="margin-left:250px;">Open</button>
<div>
<p style="font-size: 30px; text-align:center;">Bonefish</p>
</div>
<button id="menuButton2" onclick="changeStyle('Open')" style = "margin-left:250px; display: none;">Close</button>
</div>
<script>
function changeStyle(idMenu) {
//compresses OPEN and CLOSE buttons ID into a var
var menuButton = document.getElementById("menuButton");
var menuBotton2 = document.getElementById("menuButton2");
//Compresses menu DIV's ID into a var
var menuConfig = document.getElementById("menu" + idMenu);
//styles that will serve as factor for opening/closing the menu
var style1 = "background: blue; color: white; width: 500px; height: 50px; transition: 0.3s";
var style2 = "background: blue; color: white; width: 500px; height: 150px; transition: 0.3s";
//opens Menu and changes ID to "menuOpen"
if (idMenu === "Closed") {
menuConfig.style = style2;
menuConfig.id = "menuOpen";
menuButton.style = "display: none; margin-left:250px;";
menuButton2.style = "margin-left:250px; display: initial;"
}
//Closes menu and chages ID to "menuClosed"
if (idMenu === "Open") {
menuConfig.style = style1;
menuConfig.id = "menuClosed";
menuButton.style = "display: initial; margin-left:250px;";
menuButton2.style = "margin-left:250px; display: none;";
}
}
</script>
我真正想做的是用同一个按钮打开和关闭菜单,但我不知道怎么做。
我相信可以通过将<button id="menuButton" onclick="changeStyle('Closed')" style="margin-left:250px;">Open</button>
changeStyle("Closed")更改为changeStyle("Open")并进行必要的调整来完成,但我的尝试再次失败。
提前谢谢。
如果你可以使用jQuery
和一些css
,你会得到你想要的使用Java脚本更新
var divmenu=document.getElementById('menu');
function toggleMenu(btn){
if(btn.className=='open'){
btn.className='close';
divmenu.className='close';
btn.innerText='Open';
}else{
btn.className='open';
divmenu.className='open';
btn.innerText='Close';
}
}
div{
padding:10px 0;
color: white;
transition: 0.3s;
background: blue;
}
div.open{
height: 150px;
}
div.close{
height: 20px;
overflow:hidden;
}
<div id="menu" class="close">
<p id="menuString" style="margin: auto; text-align:center;">
Click on the button to open the menu
</p>
<p style="font-size: 30px; text-align:center;">Bonefish</p>
</div>
<p style="text-align:center; margin:0; padding:5px 0;"><button type="button" class="close" onclick="toggleMenu(this);">Open</button></p>
相关文章:
- 模态提示-如何重写此代码
- 我需要一个关于如何让我的插入代码在php和mysql中工作的提示
- 获取用于外部JavaScript文件(.js)的代码提示
- 没有错误消息提示,但代码无法工作
- 带有javascript代码提示的Eclipse Android项目
- 代码中的 JQuery 淡入淡出具有错误提示循环
- 适用于 Netbeans IDE 的 JQuery-Mobile 代码辅助(代码(自动)完成、代码提示)
- 向代码镜像添加动态提示
- 来自动态包含的 js 文件的 Dreamweaver 代码提示
- 在javascript上计算提示.我的代码有什么问题
- on单击提示Href Javascript代码
- 有没有任何方法可以在Dreamweaver中自动显示javascript的代码提示
- 哪个版本的Eclipse显示CSS代码提示
- 提示在岩石,纸张,剪刀代码不'不起作用
- 在 JavaScript 中使用 for 循环编写代码,提示用户输入任意数量的数字,打印这些数字中最大和最小的数字
- 有可能在dc js中添加html代码到工具提示吗?
- 运行这段简单的javascript代码时没有提示
- 三个js代码提示/自动完成
- Jquery工具提示传递html样式/格式化代码时需要调整.html()方法
- 输入HTML页面时提示代码名称