我使用菜单按钮管理了垂直滑动侧面菜单。 当我点击它时,菜单会从左侧出现/消失。 当我点击具有子菜单的菜单项时,我希望达到相同的效果。我做了很多没有运气的尝试。 我感谢任何有关如何修改或添加到我的代码的建议。 (我知道我必须添加一些转换..)
显而易见,我对这一切都很陌生。在此先感谢,最好!HTML code:
<!DOCTYPE html>
<html lang="sp" >
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Menúes</title>
<link rel="stylesheet" type="text/css" href="css/menup.css" />
<script src="js/jquery203.js"></script>
<script src="js/menup.js"></script>
</head>
<body>
<!-- Navigation -->
<div class="container">
<div id="sidebar">
<ul class="menu">
<li class="parent"><a href="#" class="home">Home ></a></li>
<ul class="submenu">
<li><a href="#" class="home">Home 11</a></li>
<li><a href="#" class="users">Home 12</a></li>
<li><a href="#" class="signout">Home 13</a></li>
</ul>
<li><a href="#" class="explore">Explore</a></li>
<li><a href="#" class="users"> Users</a></li>
<li><a href="#" class="signout"> Sign Out</a></li>
</ul>
</div>
<div class="main-content">
<a href="#" data-toggle=".container" id="sidebar-toggle">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="content">
</div>
</div>
</div>
</body>
</html>
CSS3代码:
body, html {
height: 100%;
margin: 0;
overflow:hidden;
font-family: helvetica;
font-weight: 100;
}
.container {
position: relative;
height: 100%;
width: 100%;
right: 0;
-webkit-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
transition: right 0.4s ease-in-out;
}
.container.open-sidebar {
right: 240px;
}
#sidebar {
position: absolute;
right: -240px;
background: #DF314D;
width: 240px;
height: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
border-radius: 4px;
}
#sidebar ul {
margin: 0;
padding: 0;
list-style: none;
}
#sidebar ul li {
margin: 0;
}
#sidebar ul li a {
padding: 15px 20px 15px 35px;
font-size: 16px;
font-weight: 100;
color: white;
text-decoration: none;
display: block;
border-bottom: 1px solid #C9223D;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
-ms-transition: background 0.3s ease-in-out;
-o-transition: background 0.3s ease-in-out;
transition: background 0.3s ease-in-out;
}
#sidebar li:hover {
background: #C9223D;
border-radius: 4px;
}
.main-content {
width: 100%;
height: 100%;
padding: 10px;
box-sizing: border-box;
-moz-box-sizing: border-box;
position: relative;
}
.main-content .content{
box-sizing: border-box;
-moz-box-sizing: border-box;
padding-left: 60px;
width: 100%;
}
.main-content .content h1{
font-weight: 100;
}
.main-content .content p{
width: 100%;
line-height: 160%;
}
.main-content #sidebar-toggle {
background: #DF314D;
border-radius: 3px;
display: block;
position: relative;
padding: 10px 7px;
float: right;
}
.main-content #sidebar-toggle .bar{
display: block;
width: 18px;
margin-bottom: 3px;
height: 2px;
background-color: #fff;
border-radius: 1px;
}
.main-content #sidebar-toggle .bar:last-child{
margin-bottom: 0;
}
.main-content #sidebar-toggle .bar{
display: block;
width: 18px;
margin-bottom: 3px;
height: 2px;
background-color: #fff;
border-radius: 1px;
}
.main-content #sidebar-toggle .bar:last-child{
margin-bottom: 0;
}
.menu a.home {
display: inline-block;
background: url(../imagenes/bpi.png) no-repeat 4px 10px;
}
.menu a.explore {
display: inline-block;
background: url(../imagenes/gasi.png) no-repeat 4px 10px;
}
.menu a.users {
display: inline-block;
background: url(../imagenes/bicici.png) no-repeat 4px 10px;
}
.menu a.signout {
display: inline-block;
background: url(../imagenes/bai.png) no-repeat 4px 10px;
}
#sidebar .menu .submenu {
display: none;
}
#sidebar ul li ul.visible{
display: block;
}
.submenu a.home {
display: inline-block;
background: url(../imagenes/bpi.png) no-repeat 4px 10px;
}
.submenu a.users {
display: inline-block;
background: url(../imagenes/bicici.png) no-repeat 4px 10px;
}
.submenu a.signout {
display: inline-block;
background: url(../imagenes/bai.png) no-repeat 4px 10px;
}
JS代码,它不以任何方式显示子菜单。
$(document).ready(function() {
$("[data-toggle]").click(function() {
var toggle_el = $(this).data("toggle");
$(toggle_el).toggleClass("open-sidebar");
});
$('parent').click(function() {
$('.submenu').toggleClass('visible');
});
});
答案 0 :(得分:1)
使用此功能,忘记放置说明符.parent
,检查以下代码,只需使用toggle
进行子菜单
$(document).ready(function() {
$("[data-toggle]").click(function() {
var toggle_el = $(this).data("toggle");
$(toggle_el).toggleClass("open-sidebar");
});
$('.parent').click(function() {
$('.submenu').toggle('visible');
});
});
答案 1 :(得分:0)
我实现了我的目标,因为我是html5 + css3的新手,我不知道它是否是最好的方法。我愿意接受任何改进建议。以下是基本代码:jsfiddle.net/s1fpz40c/4
$(document).ready(function() {
$(".main-content").click(function() {
$(".container").toggleClass("open-sidebar");
});
});