创建子菜单html

时间:2018-04-24 06:37:52

标签: html css drop-down-menu submenu

嘿,我的下拉菜单中有很多设计,我在添加子菜单时遇到问题,让我们先说一下dropdown-cotent div上的第一个元素。

我只是希望子菜单从定义了submeneu的悬停开始,然后向右下拉,并按照与当前子菜单相同的方式下拉。 这是我的HTML:

    <nav>
          <div class="main-nav">
            <ul class="left">
              <li class="dropdown"><a href="#">Home</a>
                <div class="dropdown-content">
                  <a href="#">Third</a>
                  <a href="#">Third Link</a>
                  <a href="#">Third Link 3</a>
                </div>
              </li>
              <li><a href="#">Gods</a></li>
              <li><a href="#">Goddesses</a></li>
            </ul>
            <div class="play-now"></div>
            <ul class="right">
              <li><a href="#">Heroes</a></li>
              <li><a href="#">Myths</a></li>
              <li><a href="#">Beasts</a></li>
            </ul>
          </div>
        </nav>

这是所有人的css:

nav {
    padding-left: 5px
}

nav .main-nav {
    height: 80px;
    width: 100%;
    margin-top: 64px;
    background: url(../images/navHeader.png) no-repeat top;
    position: relative
}

nav .main-nav ul {
    width: 360px;
    height: 80px;
    margin-top: 2px;
    padding: 0;
    list-style-type: none
}

nav .main-nav ul a,
nav .main-nav ul li {
    display: inline-block;
    width: 105px;
    line-height: 80px;
    height: 80px

}

nav .main-nav .dropdown-content a {
  height: auto;
  line-height: initial;
  padding: 3px 0;
}

nav .main-nav ul a {
    text-align: center;
    font-weight: 550;
    font-size: 12px;
    color: #84827d;
    text-shadow: 1px 1px 1px 1px #000;
    text-transform: uppercase;
    -webkit-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out
}

nav .main-nav ul a:hover {
    text-decoration: none;
    color: #7289da
}
nav .main-nav li .dropdown {

}
nav .main-nav .dropdown-content {
    position: absolute;
    display: none;
    float: left;
    z-index: 10;
    -webkit-box-shadow: 0 3px 5px 0 #999;
    -moz-box-shadow: 0 3px 5px 0 #999;
    box-shadow: 0 3px 5px 0 #999;
    border: 1px solid #CCC;
    background: #3A4FC5;
    color: #656161;
    opacity: .8;
    min-width: 30px;
    top: 60px;
}

nav .main-nav .dropdown-content a {
    color: black;
    text-decoration: none;
    display: block;
    text-align: center;
}
nav .main-nav .dropdown-content a:hover {
    background-color: #3A4FC5
}

nav .main-nav .dropdown:hover .dropdown-content {
    display: inline-block;
}

nav .main-nav ul.left {
    float: left;
    margin-left: 5px;
}

nav .main-nav ul.right {
    float: right;
    margin-left: 5px
}

这是片段,

&#13;
&#13;
nav {
    padding-left: 5px
}

nav .main-nav {
    height: 80px;
    width: 100%;
    margin-top: 64px;
    background: url(../images/navHeader.png) no-repeat top;
    position: relative
}

nav .main-nav ul {
    width: 360px;
    height: 80px;
    margin-top: 2px;
    padding: 0;
    list-style-type: none
}

nav .main-nav ul a,
nav .main-nav ul li {
    display: inline-block;
    width: 105px;
    line-height: 80px;
    height: 80px

}

nav .main-nav .dropdown-content a {
  height: auto;
  line-height: initial;
  padding: 3px 0;
}

nav .main-nav ul a {
    text-align: center;
    font-weight: 550;
    font-size: 12px;
    color: #84827d;
    text-shadow: 1px 1px 1px 1px #000;
    text-transform: uppercase;
    -webkit-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out
}

nav .main-nav ul a:hover {
    text-decoration: none;
    color: #7289da
}
nav .main-nav li .dropdown {

}
nav .main-nav .dropdown-content {
    position: absolute;
    display: none;
    float: left;
    z-index: 10;
    -webkit-box-shadow: 0 3px 5px 0 #999;
    -moz-box-shadow: 0 3px 5px 0 #999;
    box-shadow: 0 3px 5px 0 #999;
    border: 1px solid #CCC;
    background: #3A4FC5;
    color: #656161;
    opacity: .8;
    min-width: 30px;
    top: 60px;
}

nav .main-nav .dropdown-content a {
    color: black;
    text-decoration: none;
    display: block;
    text-align: center;
}
nav .main-nav .dropdown-content a:hover {
    background-color: #3A4FC5
}

nav .main-nav .dropdown:hover .dropdown-content {
    display: inline-block;
}

nav .main-nav ul.left {
    float: left;
    margin-left: 5px;
}

nav .main-nav ul.right {
    float: right;
    margin-left: 5px
}
&#13;
<nav>
  <div class="main-nav">
    <ul class="left">
      <li class="dropdown"><a href="#">Home</a>
        <div class="dropdown-content">
          <a href="#">Third</a>
          <a href="#">Third Link</a>
          <a href="#">Third Link 3</a>
        </div>
      </li>
      <li><a href="#">Gods</a></li>
      <li><a href="#">Goddesses</a></li>
    </ul>
    <div class="play-now"></div>
    <ul class="right">
      <li><a href="#">Heroes</a></li>
      <li><a href="#">Myths</a></li>
      <li><a href="#">Beasts</a></li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
  $('.dropdown-submenu a.test').on("click", function(e){
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
<style>
.dropdown-submenu {
    position: relative;
}

.dropdown-submenu .dropdown-menu {
    top: 0;
    left: 100%;
    margin-top: -1px;
}
</style>
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
   
<div class="container">
  <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Dropdown
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li><a tabindex="-1" href="#">HTML</a></li>
      <li><a tabindex="-1" href="#">CSS</a></li>
      <li class="dropdown-submenu">
        <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li><a tabindex="-1" href="#">2nd level dropdown</a></li>
          <li class="dropdown-submenu">
            <a class="test" href="#">Another dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">3rd level dropdown</a></li>
              <li><a href="#">3rd level dropdown</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>
</body>
</html>

答案 1 :(得分:1)

你的意思是这么说吗?

nav {
    padding-left: 5px
}

nav .main-nav {
    height: 80px;
    width: 100%;
    margin-top: 64px;
    background: url(../images/navHeader.png) no-repeat top;
    position: relative
}

nav .main-nav > ul {
    width: 360px;
    height: 80px;
    margin-top: 2px;
    padding: 0;
    list-style-type: none
}

nav .main-nav ul a,
nav .main-nav ul li {
    display: inline-block;
    width: 105px;
    line-height: 80px;
    height: 80px

}

nav .main-nav .dropdown-content a {
  height: auto;
  line-height: initial;
  padding: 3px 0;
}

nav .main-nav ul a {
    text-align: center;
    font-weight: 550;
    font-size: 12px;
    color: #84827d;
    text-shadow: 1px 1px 1px 1px #000;
    text-transform: uppercase;
    -webkit-transition: all .2s ease-in-out;
    transition: all .2s ease-in-out
}

nav .main-nav ul a:hover {
    text-decoration: none;
    color: #7289da
}
nav .main-nav li .dropdown {

}
nav .main-nav .dropdown-content {
    position: absolute;
    display: none;
    float: left;
    z-index: 10;
    -webkit-box-shadow: 0 3px 5px 0 #999;
    -moz-box-shadow: 0 3px 5px 0 #999;
    box-shadow: 0 3px 5px 0 #999;
    border: 1px solid #CCC;
    background: #3A4FC5;
    color: #656161;
    opacity: .8;
    min-width: 30px;
    top: 60px;
}

nav .main-nav .dropdown-content a {
    color: black;
    text-decoration: none;
    display: block;
    text-align: center;
}
nav .main-nav .dropdown-content a:hover {
    background-color: #3A4FC5
}

nav .main-nav .dropdown:hover .dropdown-content {
    display: inline-block;
}

nav .main-nav ul.left {
    float: left;
    margin-left: 5px;
}

.dropdown-content ul {
    padding: 0;
    width: auto !important;
    display: flex;
    flex-direction: column;
}

.dropdown-content ul li {
    height: auto !important;
    position: relative;
}

.dropdown-content ul li:hover > .dropdown-content-second {
    display: block;
}

.dropdown-content-second {
    position: absolute;
    display: none;
    float: left;
    z-index: 10;
    -webkit-box-shadow: 0 3px 5px 0 #999;
    -moz-box-shadow: 0 3px 5px 0 #999;
    box-shadow: 0 3px 5px 0 #999;
    border: 1px solid #CCC;
    background: #3A4FC5;
    color: #656161;
    opacity: .8;
    min-width: 30px;
    top: 0;
    left: 100%;
}

.dropdown-content-second ul li  {
    position: relative;
}

nav .main-nav ul.right {
    float: right;
    margin-left: 5px
}
<nav>
    <div class="main-nav">
        <ul class="left">
            <li class="dropdown"><a href="#">Home</a>
                <div class="dropdown-content">
                    <ul>
                        <li>
                            <a href="#">Third</a>
                            <div class="dropdown-content-second">
                                <ul>
                                    <li>
                                        <a href="javascript:;">Navi 1</a>
                                    </li>
                                    <li>
                                        <a href="javascript:;">Navi 2</a>
                                    </li>
                                    <li>
                                        <a href="javascript:;">Navi 3</a>
                                        <div class="dropdown-content-second">
                                            <ul>
                                                <li>
                                                    <a href="javascript:;">Navi 1</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;">Navi 2</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;">Navi 3</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;">Navi 4</a>
                                                </li>
                                                <li>
                                                    <a href="javascript:;">Navi 5</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </li>
                        <li>
                            <a href="#">Third Link</a>
                        </li>
                        <li>
                            <a href="#">Third Link 3</a>
                        </li>
                    </ul>
                </div>
            </li>
            <li><a href="#">Gods</a></li>
            <li><a href="#">Goddesses</a></li>
        </ul>
        <div class="play-now"></div>
        <ul class="right">
            <li><a href="#">Heroes</a></li>
            <li><a href="#">Myths</a></li>
            <li><a href="#">Beasts</a></li>
        </ul>
    </div>
</nav>

答案 2 :(得分:0)

看看我的菜单

@import url(https://fonts.googleapis.com/css?family=Oxygen+Mono);
    /* Starter CSS for Menu */
    #cssmenu {
      padding: 0;
      margin: 0;
      border: 0;
      width: auto;
    }
    #cssmenu ul,
    #cssmenu li {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    #cssmenu ul {
      position: relative;
      z-index: 597;
    }
    #cssmenu ul li {
      float: left;
      min-height: 1px;
      vertical-align: middle;
    }
    #cssmenu ul li.hover,
    #cssmenu ul li:hover {
      position: relative;
      z-index: 599;
      cursor: default;
    }
    #cssmenu ul ul {
      visibility: hidden;
      position: absolute;
      top: 100%;
      left: 0;
      z-index: 598;
      width: 100%;
    }
    #cssmenu ul ul li {
      float: none;
    }
    #cssmenu ul ul ul {
      top: 0;
      left: 190px;
      width: 190px;
    }
    #cssmenu ul li:hover > ul {
      visibility: visible;
    }
    #cssmenu ul ul {
      bottom: 0;
      left: 0;
    }
    #cssmenu ul ul {
      margin-top: 0;
    }
    #cssmenu ul ul li {
      font-weight: normal;
    }
    #cssmenu a {
      display: block;
      line-height: 1em;
      text-decoration: none;
    }
    /* Custom CSS Styles */
    #cssmenu {
      background: #333;
      border-bottom: 4px solid #9c9797;
      font-family: 'Oxygen Mono', Tahoma, Arial, sans-serif;
      font-size: 12px;
    }
    #cssmenu > ul {
      *display: inline-block;
    }
    #cssmenu:after,
    #cssmenu ul:after {
      content: '';
      display: block;
      clear: both;
    }
    #cssmenu ul {
      text-transform: uppercase;
    }
    #cssmenu ul ul {
      border-top: 4px solid #9c9797;
      text-transform: none;
      min-width: 190px;
    }
    #cssmenu ul ul a {
      background: #9c9797;
      color: #ffffff;
      border: 1px solid #837d7d;
      border-top: 0 none;
      line-height: 150%;
      padding: 16px 20px;
      font-size: 12px;
    }
    #cssmenu ul ul ul {
      border-top: 0 none;
    }
    #cssmenu ul ul li {
      position: relative;
    }
    #cssmenu ul ul li:first-child > a {
      border-top: 1px solid #837d7d;
    }
    #cssmenu ul ul li:hover > a {
      background: #b5b1b1;
      color: #ffffff;
    }
    #cssmenu ul ul li:last-child > a {
      -moz-border-radius: 0 0 3px 3px;
      -webkit-border-radius: 0 0 3px 3px;
      border-radius: 0 0 3px 3px;
      -moz-background-clip: padding;
      -webkit-background-clip: padding-box;
      background-clip: padding-box;
      -moz-box-shadow: 0 1px 0 #9c9797;
      -webkit-box-shadow: 0 1px 0 #9c9797;
      box-shadow: 0 1px 0 #9c9797;
    }
    #cssmenu ul ul li:last-child:hover > a {
      -moz-border-radius: 0 0 0 3px;
      -webkit-border-radius: 0 0 0 3px;
      border-radius: 0 0 0 3px;
      -moz-background-clip: padding;
      -webkit-background-clip: padding-box;
      background-clip: padding-box;
    }
    #cssmenu ul ul li.has-sub > a:after {
      content: '+';
      position: absolute;
      top: 50%;
      right: 15px;
      margin-top: -8px;
    }
    #cssmenu ul li:hover > a,
    #cssmenu ul li.active > a {
      background: #9c9797;
      color: #ffffff;
    }
    #cssmenu ul li.has-sub > a:after {
      content: '+';
      margin-left: 5px;
    }
    #cssmenu ul li.last ul {
      left: auto;
      right: 0;
    }
    #cssmenu ul li.last ul ul {
      left: auto;
      right: 99.5%;
    }
    #cssmenu a {
      background: #333;
      color: #CBCBCB;
      padding: 0 20px;
    }
    #cssmenu > ul > li > a {
      line-height: 48px;
      font-size: 12px;
    }
    #cssmenu.align-center > ul {
      text-align: center;
      font-size: 0;
    }
    #cssmenu.align-center > ul > li {
      display: inline-block;
      float: none;
    }
    #cssmenu.align-center ul ul {
      text-align: left;
    }
    #cssmenu.align-right > ul > li {
      float: right;
    }
    #cssmenu.align-right ul ul {
      left: auto;
      right: 0;
      text-align: right;
    }
    #cssmenu.align-right ul ul ul {
      left: auto;
      right: 190px;
    }
    #cssmenu.align-right ul ul li.has-sub > a:after {
      left: 15px;
      right: auto;
    }
    <html lang="ru" dir="ltr">
    <head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Icon Library -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    </head>
    <body>
    <div id="cssmenu">
      <ul>
         <li class="active"><a href="#" target="_blank"><i class="fa fa-fw fa-home"></i> Home</a></li>
         <li class="has-sub"><a href="#"><i class="fa fa-fw fa-bars"></i> Menus</a>
            <ul>
               <li class="has-sub"><a href="#">Menu 1</a>
                  <ul>
                     <li><a href="#">Menu 1.1</a></li>
                     <li><a href="#">Menu 1.2</a></li>
                     <li><a href="#">Menu 1.3</a></li>
                     <li class="has-sub"><a href="#">Menu 1.4</a>
                        <ul>
                           <li><a href="#">Menu 1.4.1</a></li>
                           <li><a href="#">Menu 1.4.2</a></li>
                           <li><a href="#">Menu 1.4.3</a></li>
                           <li><a href="#">Menu 1.4.4</a></li>
                        </ul>
                     </li>
                  </ul>
               </li>
               <li class="has-sub"><a href="#">Menu 2</a>
                  <ul>
                     <li><a href="#">Menu 2.1</a></li>
                     <li><a href="#">Menu 2.2</a></li>
                     <li><a href="#">Menu 2.3</a></li>
                  </ul>
               </li>
               <li class="has-sub"><a href="#">Menu 3</a>
                  <ul>
                     <li><a href="#">Menu 3.1</a></li>
                     <li><a href="#">Menu 3.2</a></li>
                     <li class="has-sub"><a href="#">Menu 3.3</a>
                        <ul>
                           <li><a href="#">Menu 3.3.1</a></li>
                           <li><a href="#">Menu 3.3.2</a></li>
                        </ul>
                     </li>
                  </ul>
               </li>
            </ul>
         </li>
         <li><a href="#"><i class="fa fa-fw fa-cog"></i> About us</a></li>
         <li><a href="#"><i class="fa fa-fw fa-phone"></i> Contact</a></li>
      </ul>
    </div>
    </body>
    </html>