【发布时间】:2017-04-01 15:24:13
【问题描述】:
我有一个问题,我想用这个很酷的动画汉堡菜单按钮(“navicon1”)编写一个侧边栏。 Menu-Button 的一个很酷的动画效果是使用“open”类。如果单击菜单按钮,我也想切换功能“openNav”和“closeNav”。
这就是我想要的:
- 如果我第一次单击菜单按钮 (navicon1) -> 菜单按钮 更改为 X(可行)并执行 javascript 函数“openNav”
- 如果我第二次单击菜单按钮 (navicon1) -> 菜单按钮 更改为正常(这有效)并执行 javascript 函数 “关闭导航”
这是我的代码:
$(document).ready(function() {
$('#navicon1,#navicon2,#navicon3,#navicon4').click(function() {
$(this).toggleClass('open');
});
});
/* Set the width of the side navigation to 250px and the left margin of the page content to 250px and add a black background color to body */
function openNav() {
document.getElementById("mySidenav").style.width = "75%";
document.getElementById("main").style.marginLeft = "75%";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
/* Set the width of the side navigation to 0 and the left margin of the page content to 0, and the background color of body to white */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
check = 0;
}
您可以忽略 navicon2-3,它们仅适用于 css...
感谢您的帮助:)
【问题讨论】:
标签: javascript function menu toggle sidebar