jquery - 如何做侧滑子菜单?

标签 jquery html css web-deployment

我管理了一个带有菜单按钮的垂直滑动侧边菜单。 当我点击它时,一个菜单从左边出现/消失。 当我单击具有子菜单的菜单项时,我想达到相同的效果。我做了很多尝试,但还没有运气。 我感谢有关如何修改或添加到我的代码的任何建议。 (我知道我必须添加一些过渡......)

很明显我是新手。提前致谢,最好!

HTML代码:

<!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');
     });

  });

最佳答案

使用这个你忘了放标识符.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');
     });

  });

Check Demo Here

关于jquery - 如何做侧滑子菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893575/

相关文章:

javascript - jquery 将 javascript 内容加载到 div 中

jquery - 使用 jQuery 将函数惰性绑定(bind)到多个事件

html - 使用 html/css,我想在我的网站底部自动生成一个引用书目,类似于 latex 的\bibliography 命令

jquery - CSS overflow-x 隐藏和 overflow-y 可见

html - 滚动父 div 时位置固定不起作用

jquery - 尝试使用 Google map 预加载图标时出错 - INVALID_STATE_ERR : DOM Exception 11

javascript - 悬停时显示和隐藏内容延迟

jquery - 滚动时突出显示菜单(如果到达 div)

javascript - 将 .CSV 转换为 HTML 表

css - 如何强制所有浏览器为媒体查询一致地测量视口(viewport)宽度