我有一个切换菜单的代码,当我点击切换时,侧面菜单被隐藏,当我再次点击切换时,它会出现。但我想稍微改变一下,目前第一次加载页面时,默认情况下会出现侧边菜单,当我点击切换然后它会消失我希望第一次加载页面边框菜单时应该隐藏,当我点击切换然后它应该出现。任何人都可以告诉你该怎么做
<div id="wrapper">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li>
<a href="#">Dashboard</a>
</li>
</ul>
</div>
<a href="#menu-toggle" class="btn btn-default" id="menu-toggle">toggle</a>
</div>
<script src="js/jquery-1.11.0.js"></script>
<!-- Menu Toggle Script -->
<script>
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
</script>
css用于整个切换
#wrapper {
padding-left: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled {
padding-left: 250px;
}
#sidebar-wrapper {
z-index: 1000;
position: fixed;
left: 250px;
width: 0;
height: 100%;
margin-left: -250px;
overflow-y: auto;
background: #000;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled #sidebar-wrapper {
width: 250px;
}
#page-content-wrapper {
width: 100%;
padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
position: absolute;
margin-right: -250px;
}
/* Sidebar Styles */
.sidebar-nav {
position: absolute;
top: 0;
width: 250px;
margin: 0;
padding: 0;
list-style: none;
}
.sidebar-nav li {
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav li a:hover {
text-decoration: none;
color: #fff;
background: rgba(255,255,255,0.2);
}
.sidebar-nav li a:active,
.sidebar-nav li a:focus {
text-decoration: none;
}
.sidebar-nav > .sidebar-brand {
height: 65px;
font-size: 18px;
line-height: 60px;
}
.sidebar-nav > .sidebar-brand a {
color: #999999;
}
.sidebar-nav > .sidebar-brand a:hover {
color: #fff;
background: none;
}
@media(min-width:768px) {
#wrapper {
padding-left: 250px;
}
#wrapper.toggled {
padding-left: 0;
}
#sidebar-wrapper {
width: 250px;
}
#wrapper.toggled #sidebar-wrapper {
width: 0;
}
#page-content-wrapper {
padding: 20px;
}
#wrapper.toggled #page-content-wrapper {
position: relative;
margin-right: 0;
}
}
答案 0 :(得分:3)
您可以在绑定后触发click事件:
IQueryable
答案 1 :(得分:1)
您应该在display:none
div
sidebar-wrapper
#sidebar-wrapper{
display:none;
}
答案 2 :(得分:0)
您错过了$("#wrapper").hide();
参数
你可以使用它
<div id="wrapper">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li>
<a href="#">Dashboard</a>
</li>
</ul>
</div>
</div>
<a href="#menu-toggle" class="btn btn-default" id="menu-toggle">toggle menu</a>
<script>
$("#wrapper").hide();
$("#menu-toggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggle();
});
</script>
答案 3 :(得分:-1)
$("#wrapper").hide();
$("#menu-toggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggle();
});
我删除了包装器中的toggle
锚点,因此在隐藏包装器期间,切换链接仍可用于点击,然后我使用.toggle()