将Sub-Sub-Menus添加到带有子菜单的CSS菜单中

时间:2011-06-30 15:30:55

标签: html css menu

我有一个CSS菜单,我正在使用子菜单。我想知道如何添加一个子子菜单。例如,我将鼠标悬停在主菜单项上并弹出子菜单,然后将鼠标悬停在子菜单项上,弹出另一个子菜单。这是我现在使用的JS小提琴:

http://jsfiddle.net/PrinceofVegas/Dg3yQ/4/

这是我正在使用的CSS:

.menu{
    border:none;
    border:0px;
    margin:0px;
    padding:0px;
    font: 67.5% "Lucida Sans Unicode", "Bitstream Vera Sans", "Trebuchet Unicode MS", "Lucida Grande", Verdana, Helvetica, sans-serif;
    font-size:18px;
    font-weight:bold;
}
.menu ul{
    background:#006633;
    height:35px;
    list-style:none;
    margin:0;
    padding:0;
}
.menu li{
    float:left;
    padding:0px;
}
.menu li a{
    background:#006633 url("../images/seperator.gif") bottom right no-repeat;
    color:#ffffff;
    display:block;
    font-weight:normal;
    line-height:35px;
    margin:0px;
    padding:0px 25px;
    text-align:center;
    text-decoration:none;
}
.menu li a:hover, .menu ul li:hover a{
    background: #003f20 url("../images/hover.gif") bottom center no-repeat;
    color:#FFFFFF;
    text-decoration:none;
}
.menu li ul{
    background:#006633;
    display:none;
    height:auto;
    padding:0px;
    margin:0px;
    border:0px;
    position:absolute;
    width:225px;
    z-index:200;
    /*top:1em;
    /*left:0;*/
}
.menu li:hover ul{
    display:block;
}
.menu li li {
    background:url('../images/sub_sep.gif') bottom left no-repeat;
    display:block;
    float:none;
    margin:0px;
    padding:0px;
    width:225px;
}
.menu li:hover li a{
    background:none;
}
.menu li ul a{
    display:block;
    height:30px;
    font-size:12px;
    font-style:normal;
    margin:0px;
    padding:0px 10px 0px 15px;
    text-align:left;
}
.menu li ul a:hover, .menu li ul li:hover a{
    background:#003f20 url('../images/hover_sub.gif') center left no-repeat;
    border:0px;
    color:#ffffff;
    text-decoration:none;
}
.menu p{
    clear:left;
}

以下是我正在使用的HTML:

<div class="menu">
    <ul>
        <li><a href="#" target="_self" >Main Item 1</a></li>
        <li><a href="#" target="_self" >Main Item 2</a>
            <ul>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
            </ul>
        </li>
        <li><a href="#" target="_self" >Main Item 3</a></li>
        <li><a href="#" target="_self" >Main Item 4</a>
            <ul>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
                <li><a href="#" target="_self" >Test Sub Item</a></li>
            </ul>                
        </li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:11)

以下是我将如何处理您所寻找的内容:http://jsfiddle.net/Dg3yQ/26/

我对修改CSS采取了一些自由。修改后的CSS将代码减少了几百个字符,我相信它可以完成您的预期。我希望这会有所帮助。

已编辑:添加了一个简化的代码示例,其中包含有关如何完成这些子菜单的回答。

#nav {
    list-style:none inside;
    margin:0;
    padding:0;
    text-align:center;
    }

#nav li {
    display:block;
    position:relative;
    float:left;
    background: #006633; /* menu background color */
    }

#nav li a {
    display:block;
    padding:0;
    text-decoration:none;
    width:200px; /* this is the width of the menu items */
    line-height:35px; /* this is the hieght of the menu items */
    color:#ffffff; /* list item font color */
    }
        
#nav li li a {font-size:80%;} /* smaller font size for sub menu items */
    
#nav li:hover {background:#003f20;} /* highlights current hovered list item and the parent list items when hovering over sub menues */



/*--- Sublist Styles ---*/
#nav ul {
    position:absolute;
    padding:0;
    left:0;
    display:none; /* hides sublists */
    }

#nav li:hover ul ul {display:none;} /* hides sub-sublists */

#nav li:hover ul {display:block;} /* shows sublist on hover */

#nav li li:hover ul {
    display:block; /* shows sub-sublist on hover */
    margin-left:200px; /* this should be the same width as the parent list item */
    margin-top:-35px; /* aligns top of sub menu with top of list item */
    }
<ul id="nav">
  <li><a href="#">Main Item 1</a></li>
  <li><a href="#">Main Item 2</a>
    <ul>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">Sub Item</a></li>
      <li><a href="#">SUB SUB LIST &raquo;</a>
        <ul>
          <li><a href="#">Sub Sub Item 1</a>
          <li><a href="#">Sub Sub Item 2</a>
        </ul>
      </li>
    </ul>
  </li>
  <li><a href="#">Main Item 3</a></li>
</ul>

答案 1 :(得分:0)

如果要使用CSS过渡(这对于显示属性不起作用),则可以使用不透明度替换显示。为了处理元素现在占用空间即使隐藏的事实,你可以简单地将整个菜单放入一个单独的div中,该div在z顺序中绝对和最高位置(无论如何都应该是菜单)。然后什么都不会妨碍,因为菜单将是顶层中唯一的项目。

以下是我为CSS过渡修改的原始示例:

#menu {
    border:none;
    border:0px;
    margin:0px;
    padding:0px;
    font: 67.5% "Lucida Sans Unicode", "Bitstream Vera Sans", "Trebuchet Unicode MS", "Lucida Grande", Verdana, Helvetica, sans-serif;
    font-size:18px;
    font-weight:bold;
}

#nav {
    height:35px;
    list-style:none;
    margin:0;
    padding:0;
    float:left;
    text-align:center;
    

    }

#nav li {
    display:inline-block;
    position:relative;
    float:left;
    background: #006633;
    }

#nav li a {
    display:inline-block;
    width:200px;
    line-height:35px;
    padding:0;
    text-decoration:none;
    color:#ffffff;
    }
    
#nav li li {float:left; #006633;}
    
#nav li li a {
    display:block;
    font-size:12px;
    
    opacity:1;
    transition: opacity 1s;
}
    
#nav li:hover {background:#003f20;}

/*--- Sublist Styles ---*/
#nav ul {
    position:absolute;
    padding:0px;
    left:0;
    /* display:none; */
    opacity:0;
    transition: opacity 1s;
    }

/*--- Hide Sub Sublists ---*/
#nav li:hover ul ul {
    /* display:none; */
    opacity:0;
    transition: opacity 1s;
}

/*--- Sublevel UL's display and position on hover ---*/
#nav li:hover ul {
   /* display:block; */
   opacity:1;
   transition: opacity 1s;
   } 
   
/* had to make the position NOT based on hover, but permanent 
for the transition to work , thus moved it from POS_001 */
#nav li li  ul {
   margin-left:200px;
   margin-top:-35px;

}

#nav li li:hover ul {
   /* POS_001 */

   
   /* display:block; */
   opacity:1;
   transition: opacity 1s;
}
<div id="menu">
        <ul id="nav">
            <li><a href="#" target="_self" >Main Item 1</a></li>
            <li><a href="#" target="_self" >Main Item 2</a>
                <ul>
                    <li><a href="#" target="_self" >Test Sub Item</a></li>
                    <li><a href="#" target="_self" >SUB SUB LIST 1 &gt;&gt;</a>
                        <ul>
                            <li><a href="#" target="_self" >Sub Sub Test Item 1</a>
                            <li><a href="#" target="_self" >Sub Sub Test Item 2</a>
                        </ul>
                    </li>
                    <li><a href="#" target="_self" >SUB SUB LIST 2 &gt;&gt;</a>
                        <ul>
                            <li><a href="#" target="_self" >Sub Sub Test Item 3</a>
                            <li><a href="#" target="_self" >Sub Sub Test Item 4</a>
                        </ul>
                    </li>
                </ul>
            </li>
            <li><a href="#" target="_self" >Main Item 3</a></li>
        </ul>
    </div>