将简单的下拉菜单添加到我的简单响应菜单中

时间:2013-11-05 03:23:04

标签: jquery css css3 drop-down-menu

我在网上关于创建响应式菜单的教程。 我能够做到这一点,我想通过在其上添加一个下拉菜单再往前走一步。

这是整个代码。或jsfiddle

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
body {
    background-color: #ece8e5;
}
.wrapper {
    width: 1020px;
    margin: 0 auto;
}
.clearfix:before, .clearfix:after {
    content: " ";
    display: table;
}
.clearfix:after {
    clear: both;
}
.clearfix {
 *zoom: 1;
}
nav {
    height: 40px;
    width: 100%;
    background: #455868;
    font-size: 11pt;
    font-family: 'PT Sans', Arial, sans-serif;
    font-weight: bold;
    position: relative;
    border-bottom: 2px solid #283744;
}
nav ul {
    padding: 0;
    margin: 0 auto;
    width: 600px;
    height: 40px;
}
nav li {
    display: inline;
    float: left;
}
nav a {
    color: #fff;
    display: inline-block;
    width: 100px;
    text-align: center;
    text-decoration: none;
    line-height: 40px;
    text-shadow: 1px 1px 0px #283744;
}
nav li a {
    border-right: 1px solid #576979;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
nav li:last-child a {
    border-right: 0;
}
nav a:hover, nav a:active {
    background-color: #8c99a4;
}
nav a#pull {
    display: none;
}
/** Drop **/

nav li ul {
    display: none;
}
 @media screen and (max-width: 768px) {
.wrapper {
    width: auto;
}
nav {
    height: auto;
}
nav ul {
    width: 100%;
    display: block;
    height: auto;
}
nav li {
    width: 50%;
    float: left;
    position: relative;
}
nav li a {
    border-bottom: 1px solid #576979;
    border-right: 1px solid #576979;
}
nav a {
    text-align: left;
    width: 100%;
    text-indent: 25px;
}
}
 @media only screen and (max-width : 480px) {
nav {
    border-bottom: 0;
}
nav ul {
    display: none;
    height: auto;
}
nav a#pull {
    display: block;
    background-color: #283744;
    width: 100%;
    position: relative;
}
nav a#pull:after {
    content: "";
    background: url('nav.png') no-repeat;
    width: 30px;
    height: 30px;
    display: inline-block;
    position: absolute;
    right: 15px;
    top: 10px;
}
}
 @media only screen and (max-width : 320px) {
nav li {
    display: block;
    float: none;
    width: 100%;
}
nav li a {
    border-bottom: 1px solid #576979;
}
}
</style>
</head>
<body>
<div class="wrapper">
  <nav class="clearfix">
    <ul class="clearfix">
      <li><a href="#">Home</a></li>
      <li><a href="#">How-to</a></li>
      <li><a href="#">Icons</a></li>
      <li><a href="#">Design</a></li>
      <li><a href="#">Web 2.0</a></li>
      <li><a href="#">Tools</a></li>
    </ul>
    <a href="#" id="pull">Menu</a> </nav>
</div>
<script>
$(function() {  
    var pull        = $('#pull');  
        menu        = $('nav ul');  
        menuHeight  = menu.height();  

    $(pull).on('click', function(e) {  
        e.preventDefault();  
        menu.slideToggle();  
    });  
});  

$(window).resize(function(){  
    var w = $(window).width();  
    if(w > 320 && menu.is(':hidden')) {  
        menu.removeAttr('style');  
    }  
});   
</script>
</body>
</html>

我试过的是在ul

中添加额外的li
<nav class="clearfix">
  <ul class="clearfix">
    <li><a href="#">Home</a>
    <ul >
     <li><a href="#">Submenu</a>
    </ul>
    </li>
    <li><a href="#">How-to</a></li>
    <li><a href="#">Icons</a></li>
    <li><a href="#">Design</a></li>
    <li><a href="#">Web 2.0</a></li>
    <li><a href="#">Tools</a></li>
  </ul>
  <a href="#" id="pull">Menu</a> </nav>

然后将css设置为:

nav ul li ul { display:none; }
nav ul li:hover ul { display:block; }

这就是我试过的......

1 个答案:

答案 0 :(得分:0)

以下代码切换子'ul'元素的显示。这应该是一个很好的起点。

(显示悬停时的子菜单 - 如果要在点击事件中显示,请将“.hover”更改为“.click”。)

// toggle display of child ul elements
$('a').hover(function(){
    $(this).parent().children('ul').toggle()
})

不要忘记将子元素添加到列表中。 e.g。

<li><a href="#">Home</a>
  <ul><li>one</li><li>two</li></ul>  
</li>

JSFiddle:http://jsfiddle.net/5D9Xa/1/