简体   繁体   English

如何在响应式CSS菜单上添加子菜单

[英]how to add sub menus on a responsive css menu

I have this html for my css menu: 我的css菜单有这个html:

<nav class="clearfix">
    <ul class="clearix">
        <li><a href="http://www.domain.co.uk/">Homepage</a></li>
        <li><a href="/services">Services</a></li>
        <li><a href="/project-gallery">Project Gallery</a></li>
        <li><a href="/contact-us">Contact Us</a></li>  
    </ul>
    <a href="#" id="pull">Menu</a>  
</nav>

nav {
    height: 50px;  
    width: 100%;  
    background: #F00;  
    font-size: 14pt;  
    font-family: Arial;
    position: relative;  
    border-bottom: 5px solid #FFFFFF;  
}  
nav ul {  
    padding: 0;  
    margin: 0 auto;  
    width: 100%;  
    height: 50px;
    text-align: center;
}
nav li {  
    display: inline;
}  
.clearfix:before,  
.clearfix:after {  
    content: " ";  
    display: table;  
}  
.clearfix:after {  
    clear: both;  
}  
.clearfix {  
    *zoom: 1;  
}  
nav a {  
    color: #FFFFFF;  
    display: inline-block;  
    width: auto;

    text-align: center;  
    text-decoration: none;  
    line-height: 50px;  
}  
nav li a {
    box-sizing:border-box;  
    -moz-box-sizing:border-box;  
    -webkit-box-sizing:border-box;
    padding-left: 10px;
    padding-right: 10px;
}  
nav li:last-child a {  
    border-right: 0;  
}  
nav a:hover, nav a:active {  
    background-color: #000000;
    color:#FFFFFF;  
} 
nav a#pull {  
    display: none;  
}  
@media screen and (max-width: 600px) {  
    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 #FFFFFF;  
        border-right: 1px solid #FFFFFF;  
    }  
    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;
        color:#FFFFFF;
        background-color: #F00;  
        width: 100%;  
        position: relative;  
    }  
    nav a#pull:after {  
        content:"";  
        background: url('nav-icon.png') no-repeat;  
        width: 30px;  
        height: 30px;  
        display: inline-block;  
        position: absolute;  
        rightright: 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 #FFFFFF;  
    }  
}  

I am looking for a way to add sub menus and then second sub menus on on the first ones but still keep it as responsive as it is. 我正在寻找一种方法来添加子菜单,然后在第一个子菜单上添加第二个子菜单,但仍保持其响应性。

How can I do this? 我怎样才能做到这一点?

http://jsfiddle.net/EYjnG/ http://jsfiddle.net/EYjnG/

JSFIDDLE DEMO logic is just simple and have with this code JSFIDDLE DEMO逻辑很简单,并且具有此代码

#submenu,#submenu2,#submenu3{
    visibility:hidden;       /*turn all the submenus visibility hidden */
}
#top_menu li.first:hover #submenu,#submenu li.second:hover #submenu2,#submenu2     li.second:hover #submenu3{
    visibility:visible;      /*On hover turn on visibility visible */
}      

Complete code : 完整代码:

HTML: HTML:

<div id="top_menu">  <!--MAIN MENU -->                      
    <ul>
        <li class="first">menu1                 
            <div id="submenu">   <!--First Submenu -->
                <ul class="abc">
                    <li class="second">item1    
                        <div id="submenu2">  <!--Second Submenu -->
                            <ul class="abc">
                                <li class="second">item1_1
                                    <div id="submenu3">  <!--Third Submenu -->
                                        <ul class="abc">
                                            <li class="second">item1_1_1</li>
                                            <li class="second">item1_1_2</li>
                                            <li class="second">item1_1_3</li>
                                        </ul>
                                    </div>  <!--third Submenu Ends here-->
                                </li>
                                <li class="second">item1_2</li>
                                <li class="second">item1_3</li>
                            </ul>
                        </div>  <!--Second Submenu Ends here-->                         
                    </li>
                    <li class="second">item2
                                <div id="submenu2">
                                    <ul class="abc">
                                        <li class="second">item2_1</li>
                                        <li class="second">item2_2</li>
                                        <li class="second">item2_3</li>
                                    </ul>
                                </div>                              
                    </li>
                    <li class="second">item3
                                <div id="submenu2">
                                    <ul class="abc">
                                        <li class="second">item3_1</li>
                                        <li class="second">item3_2</li>
                                        <li class="second">item3_3</li>
                                    </ul>
                                </div>                              
                    </li>
                </ul>
            </div>
        </li>   

        <li class="first">menu2
            <div id="submenu">
                <ul class="abc">
                    <li class="second">item1</li>
                    <li class="second">item2</li>
                    <li class="second">item3</li>
                    <li class="second">item4</li>
                </ul>
            </div>
        </li>

    </ul>   
</div>

CSS: CSS:

ul{
    padding:10px;
    padding-right:0px;
}
li.first{
    display:block;
    display:inline-block;
    padding:5px;
    padding-right:25px;
    padding-left:25px;
    cursor:pointer;
}
li.second{
    list-style:none;
    margin:0px;
    padding:5px;
    padding-right:25px;
    margin-bottom:5px;
    cursor:pointer;
}
#submenu li.second:hover{
    background:red;
    border-radius:5px;
}

#submenu2 li.second:hover{
    background:green;
    border-radius:5px;
}
/*********MAIN LOGIC***************/

#submenu,#submenu2,#submenu3{
    visibility:hidden;
}
#top_menu li.first:hover #submenu,#submenu li.second:hover #submenu2,#submenu2 li.second:hover #submenu3{
    visibility:visible;
}
/**********STYLING SUBMENUS**************/
#submenu{
    padding-right:0px;
    text-align:left;
    position:absolute;
    background:white;
    box-shadow:0px 0px 5px;
    border-radius:5px;
}
#submenu2{
    text-align:left;
    position:absolute;
    left:70px;
    top:0px;
    background:red;
    box-shadow:0px 0px 5px;
    border-radius:5px;
}
#submenu3{
    text-align:left;
    position:absolute;
    left:80px;
    top:0px;
    background:green;
    box-shadow:0px 0px 5px;
    border-radius:5px;
}

just understand the logic behind this code and you can made as many submenus as you want. 只需了解此代码背后的逻辑,您就可以根据需要创建任意数量的子菜单。

There are many ways to go ahead about this. 有很多方法可以解决这个问题。

I usually hide the sub menu ul s with display: none and take them out of the content flow with position: absolute . 我通常用display: none隐藏子菜单ul并将它们从position: absolute的内容流中取出。 Give the li containing the sub menu position: relative so that the sub menus are relative to their direct parents, then position the sub menus however you please using the top , right , bottom and left properties. li包含子菜单position: relative以使子菜单相对于它们的直接父项,然后使用toprightbottomleft属性定位子菜单。 Finally, change the sub menu to display: block through a :hover or whatever. 最后,更改子菜单以display: block通过:hover或其他。

Here's a bare-bones example of this: 这是一个简单的例子:

Markup: 标记:

<nav>
  <ul>
    <li><a>Link</a>
      <ul>
        <li><a>Sub link</a></li>
        <li><a>Sub link</a></li>
        <li><a>Sub link</a></li>
      </ul>
    </li>
  </ul>
</nav>

CSS: CSS:

nav li {
  position: relative;
}

nav li > ul {
  position: absolute;
  top: 100%;
  display: none;
}

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

Here's a pen with this example . 这是一个用这个例子的笔 It looks like crap but you get the drill. 它看起来像垃圾,但你得到了演习。

You can just keep nesting more sub-menus, but you'll probably want to use different positioning for second-and-lower-levels of sub menus. 您可以继续嵌套更多子菜单,但您可能希望对二级和更低级别的子菜单使用不同的定位。

However, please note that mobile browsers don't really support :hover . 但请注意,移动浏览器并不支持:hover At least they don't treat it the same. 至少他们不会对待它。 You shouldn't make your sub menus accessible only on :hover . 您不应仅在:hover访问子菜单:hover Consider adding some sort of class name toggle on click with javascript instead. 考虑在点击时使用javascript添加某种类名称切换。

I am not 100% sure if your asking how to make the id menu have a menu functionality or just a sub menu for your main nav. 如果您询问如何使id菜单具有菜单功能或只是主导航的子菜单,我不是100%确定。

If it is pertaining to a sub menu for your main nav then this will work just fine. 如果它与主导航的子菜单有关,那么这将正常工作。 If it's for the mobile menu then let me know and I'll work something out for you. 如果是移动菜单,请告诉我,我会为您解决问题。 (SOLVED) (解决了)

This fiddle has the sub menu working while still being responsive the entire time. 这个小提琴有子菜单工作,同时仍然响应整个时间。 You can style it to your needs but it is a solid start. 您可以根据自己的需求设计样式,但这是一个坚实的开端。

nav ul li ul {
  display: none;
  position: absolute;
  width: 100%;
  top: 100%;
  background: #000;
  color: #fff;
}

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

nav ul li ul li {
  display: block;
  -webkit-transition: .6s ease;
  -moz-transition: .6s ease;
  -ms-transition: .6s ease;
  -o-transition: .6s ease;
}

nav ul li ul li:hover {
  background: #c1c1c1;
  color: #2b2b2b;
}

JSFIDDLE 的jsfiddle

JSFIDDLE with relative sized sub menu JSFIDDLE具有相对大小的子菜单

Here is the mobile navigation working and the biggest problem was you had no jQuery library selected for the fiddle to run off of. 这是移动导航工作,最大的问题是你没有选择jQuery库为小提琴运行。

Mobile Nav 移动导航

HTML HTML

<div id="pull"><span>Menu</span>

</div> 

CSS CSS

div span {
  color: #FFFFFF;  
  display: inline-block;  
  width: auto;
  text-align: center;  
  text-decoration: none;  
  line-height: 50px;
  padding-left: 10px;
  padding-right: 10px;
}

I changed the id pull to a div because when it was an anchor tag all of the navs would lose their text color. 我将id pull更改为div,因为当它是锚标记时,所有导航都会丢失其文本颜色。

I have made a drop-down with a drop-down in it while still being responsive! 我已经做了一个下拉列表,同时仍然有响应! Take a peak at this jsfiddle. 在这个jsfiddle占据一席之地。

JSFIDDLE Drop-Down with a nested Drop-Down 带嵌套下拉列表的JSFIDDLE下拉列表

use hover in css like: 在css中使用hover如:

a:hover

or if your div id is "div1": 或者如果你的div id是“div1”:

#div1:hover 

Here's my take: http://codepen.io/teodragovic/pen/rmviJ 这是我的看法: http//codepen.io/teodragovic/pen/rmviJ

HTML HTML

<nav>
  <input type="checkbox" id="nav-toggle-1" />
  <label for="nav-toggle-1" class="pull sub"><a>Menu</a></label>  
  <ul class="lvl-1">
    <li><a href="http://www.domain.co.uk/">Homepage</a></li>
    <li>
      <input type="checkbox" id="nav-toggle-2" />
      <label for="nav-toggle-2" class="sub"><a>Services</a></label>
      <ul class="lvl-2">
        <li><a href="#">Service 1</a></li>
        <li><a href="#">Service 2</a></li>
        <li>
          <input type="checkbox" id="nav-toggle-3" />
          <label for="nav-toggle-3" class="sub"><a>Service 3</a></label>
          <ul class="lvl-3">
            <li><a href="#">Service 3 a</a></li>
            <li><a href="#">Service 3 b</a></li>
          </ul>
        </li>
        <li><a href="#">Service 4</a></li>
      </ul>
    </li>
    <li><a href="/project-gallery">Project Gallery</a></li>
    <li><a href="/contact-us">Contact Us</a></li>  
  </ul> 
</nav>

CSS CSS

@import "compass";

/* globals */
* {box-sizing:border-box;}

ul {
  margin: 0;
  padding: 0;
}

input {
  position: absolute;
  top: -99999px;
  left: -99999px;
  opacity: 0;
}

nav {
  height: 50px;
  background: #F00;  
  font: 16px/1.5 Arial, sans-serif;
  position: relative;
}

a {  
  color: #FFFFFF;  
  display: inline-block;
  text-decoration: none;  
  line-height: 50px;  
  padding: 0 20px;
  &:hover,
  &:active {
    background-color: #000000;
    color:#FFFFFF;
  }
}

/* nav for +600px screen */

ul.lvl-1 { 
  text-align: center;
  @include pie-clearfix;
  li {
    display: inline;
  }
}

ul.lvl-2, 
ul.lvl-3 {
  position: absolute;
  width: 100%;
  background: lighten(red, 15%);
  display:none;
}
ul.lvl-3 {background: lighten(red, 30%);}    

#nav-toggle-2:checked ~ ul.lvl-2,
#nav-toggle-3:checked ~ ul.lvl-3 {
  display: block;
}

.pull {display: none;}

/* arrow thingy - crappy positioning, needs tinkering */
.sub {
  position: relative;
  cursor: pointer;
  &:after {  
    position: absolute;
    top: 40%;
    right: 0;
    content:"";  
    width: 0;
    height: 0;
    border-right: 6px solid transparent;
    border-left: 6px solid transparent;
    border-top: 6px solid white;
  }  
}

/* medium-ish nav */

@media screen and (max-width: 600px) {  

nav {height: auto;}

a {  
  text-align: left;  
  width: 100%;  
  text-indent: 25px;
  border-bottom: 1px solid #FFFFFF;
}

ul > li {  
  width: 50%;  
  float: left;
  &:nth-of-type(odd) {
    border-right: 1px solid white;
  }
}

li:nth-of-type(even) ul.lvl-2,
li:nth-of-type(even) ul.lvl-3 {
  position: relative;
  width: 200%;
  left: -100%;
}

li:nth-of-type(odd) ul.lvl-2,
li:nth-of-type(odd) ul.lvl-3 {
  position: relative;
  width: 200%;
  left: 1px;
}

ul.lvl-2 li {background: lighten(red, 15%);}
ul.lvl-3 li {background: lighten(red, 30%);}

}

/* small-ish nav */

@media only screen and (max-width : 480px) {

.pull {
  display: block;
  width: 100%;  
  position: relative;  
}

ul {
  height: 0;  
  > li {
    width: 100%;
    &:nth-of-type(odd) {
      border-right: none;
    }
  }
}  

#nav-toggle-1:checked ~ ul.lvl-1 {
  height: auto;
}

#nav-toggle-2:checked ~ ul.lvl-2,
#nav-toggle-3:checked ~ ul.lvl-3 {
  //reverting stuff from previous breakpoint
  left: 0;
  width: 100%;
}

}

Markup is little modified from original since I find it easier to use classes than general selectors, especially when nesting lists. 标记很少从原始修改,因为我发现使用类比一般选择器更容易,特别是在嵌套列表时。

It's CSS only (I'm using SASS+compass). 它只是CSS(我正在使用SASS +指南针)。 :checked pseudo-class are used for toggling menus on and off. :checked伪类用于打开和关闭菜单。 I removed link for services assuming that it will be used just for opening sub-menu (content-wise, you could always add something like "all services" in submenu if you want to keep that page in navigation). 我删除了服务链接,假设它只用于打开子菜单(内容方面,如果你想在导航中保留该页面,你总是可以在子菜单中添加类似“所有服务”的东西)。

Biggest challenge was styling middle breakpoint. 最大的挑战是造型中断点。 Depending on position of parent list item (odd or even), child list is stretched to 200% (because parent is 50% wide) and positioned so it floats from left side. 根据父列表项的位置(奇数或偶数),子列表被拉伸到200%(因为父级是50%宽)并且定位为从左侧浮动。 Small bug appears on level 3 regarding width of the list causing color bleed on edges. 第3级出现关于列表宽度的小错误,导致边缘出现颜色渗色。

Additionaly, display: block and display:none selectors could be replaced with max-height to add some cool css animation effects 另外, display: blockdisplay:none选择器可以用max-height替换来添加一些很酷的css动画效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM