创建垂直下拉菜单,同时保持其他项目到位

时间:2014-07-15 22:10:51

标签: html css html5

我在这个导航栏上寻求帮助,我需要它能够在保持导航栏的其他项目的同时下拉。我查看了所有其他提交内容,但仍未找到任何有用的内容。这是代码和示例。 http://jsfiddle.net/doctor_turkey/G2xfz/

<html>
    <style>
    #div5 {
        background-color: black;
        -webkit-box-shadow: 0px 0px #000000;
        box-shadow:0px 5px 5px #5B5454;
        width: 100%;
        clear: both;
        margin-left: -1%;
        margin-right:-1%;
        padding-left:1%;
        padding-right:1%;
    }
    #div5 ul{
        margin: 0 0 0 60px;
        padding: 0px;
        list-style: none;
    }


    #div5 ul li{
        display: inline;
        -webkit-box-shadow: 0px 0px #000000;
        box-shadow: 0px 0px #000000;
        position:relative;
    }

    #div5 ul li a{
    float: left;
    width: 15%;
    height: 20px;
    margin-top: -5px;
    margin-bottom: 10px;
    padding: 0;
    font-size: 15px;
    font-weight: none;
    text-align: center;
    text-decoration: none;
    color: black;
    border-left: 1px solid #CCCCCC;
    border-right: 1px solid #CCCCCC;
    font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", Verdana, sans-serif;
    }






    #div5 ul li .firstmenu{
        border-left: 2px solid #CCCCCC;
        border-right: 1px solid #CCCCCC;
        -webkit-box-shadow: 0px 0px;
        box-shadow: 0px 0px;
    }

    #div5 ul li .smallmenuend{
       border-left: 1px solid #CCCCCC;
       border-right: 2px solid #CCCCCC; 
    } 

    /* HOVER MENU TEST*/
    #div5 li ul {
       float: left;
       left: 0;
       opacity: 0;
       position:relative;
       display:none;
       z-index: 1;
      -webkit-transition: all .25s ease;
      -moz-transition: all .25s ease;
      -ms-transition: all .25s ease;
      -o-transition: all .25s ease;
         transition: all .25s ease;
     }
     #div5 li:hover ul {
        opacity: 1;
     /* top: 50px;*/
        display:block;
        float: left;
        color:black;
        clear:left;
        display:block;
        margin-left:20%;
     }
     #div5 li ul li {
         clear:left;
         color:black;
         display:block;
         width:100%;
     } 

     #div5 li ul li a{
         width:100%;   
     }

     #div5 li ul a:hover {
         background: #bbb;
     }

    </style>


    <div id="div5" class="fluid">
      <p align="center">
          <div align="center">
              <ul>
                  <li><a href="#" class="firstmenu"> One</a></li>
                  <li><a href="#" class="smallmenuend"> Two </a> 
                      <ul>
                          <li><a href="#"> Test 1 </a></li>
                          <li><a href="#"> Test 2 </a></li>
                      </ul>  
                  </li>
                  <li><a href="#" >Three</a></li>
                  <li><a href="#" class="smallmenuend"> Four </a></li>
                  <li><a href="#" class="firstrow">Five</a></li>
                  <li ><a href="#" class="smallmenuend">Six</a></li>
              </ul>
           </div>
      </p>
   </div>

1 个答案:

答案 0 :(得分:1)

您的代码需要大量工作 - 这是一种不必要的标记和样式。

这是此导航栏的清理版本。欢迎您研究此代码并优化您的代码。

HTML:

<ul id = "nav">
    <li><a href="#">Home</a></li>
    <li><a href="#">Energy </a> 
        <ul>
            <li><a href="#">Test 1</a></li>
            <li><a href="#">Test 2</a></li>
        </ul>  
    </li>
    <li><a href="#">Account</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="#">Services</a></li>
    <li ><a href="#">Solutions</a></li>
</ul>

CSS:

* {
    margin: 0;
    padding: 0;
}

body {
    background-color: #eee;
}

#nav {
    list-style-type: none;
    display: table;
    min-width: 610px;
    margin: 0 auto;
    background-color: #fff;
    text-align: center;
}

#nav > li {
    display: inline-block;
    position: relative;
}

#nav ul {
    list-style-type: none;
    background-color: #5f6975;
    position: absolute;
    min-width: 100%;
}

#nav li a {
    font: normal 15px/1 Sans-Serif;
    text-decoration: none;
    color: #4b545f;
    display: block;
    padding: 10px 20px;
    text-align: left;
    white-space: nowrap;
}

#nav ul a {
    color: #fff;
}

#nav ul > li + li {
    border-top: 1px solid #6b727c;
}

#nav li:hover {
    background-color: #4b545f;
}

#nav > li:hover > a {
    color: #fff;
}

#nav ul {
    display: none;
}

#nav li:hover > ul {
    display: block;
}

而且,小提琴:http://jsfiddle.net/bE2D2/