使用CSS在菜单上悬停时显示子子菜单

时间:2018-03-09 10:09:15

标签: html css3 menu hover

我需要在子菜单的鼠标悬停时显示子菜单。到目前为止,我做了显示菜单的代码 - >菜单上的子菜单单击,但要继续我希望功能在我的子菜单上悬停时显示子子菜单。有人可以帮我实现同样的目标吗?

var ddmenuitem = 0;

function jsddm_open() {
  jsddm_close();
  ddmenuitem = $(this).find('ul.submenu').css('display', 'block');
  //   $(this).find('div.subsubmenu').css('display','none');
}

function jsddm_close() {
  if (ddmenuitem) ddmenuitem.css('display', 'none');
}
$(document).ready(function() {
  $('#topnav > ul > li').bind('click', jsddm_open)
  $('#topnav > ul > li > a').click(function(ev) {
    if ($(this).hasClass('current')) {
      ev.preventDefault();
    }


    if ($(this).attr('class') != 'active') {
      $('#topnav ul li a').removeClass('active');
      $(this).addClass('active');
    }
  });
});
#topnav {
  float: left;
  width: 600px;
  height: 30px;
  background: black;
  margin-top: 10px;
  position: relative;
  font-size: 15px;
  margin-left: 30px
}

#topnav ul {
  list-style: none;
  padding: 0px;
  margin: 0px;
}

#topnav ul li {
  float: left;
  margin: 0;
  padding: 0;
}

#topnav ul li a {
  padding: 5px 15px;
  color: red;
  text-decoration: none;
  display: block;
  font-weight: bold;
}

#topnav ul li a:link {
  color: red;
  text-decoration: none;
}

#topnav ul li a:visited {
  color: #FFF;
  text-decoration: none;
}

#topnav ul li a:hover {
  color: red;
  text-decoration: none;
}

#topnav ul li a.active {
  text-decoration: none;
  color: black;
  background: #e0e0e0;
}

#topnav ul li ul.submenu {
  float: left;
  padding: 4px 0;
  position: absolute;
  left: 0;
  top: 30px;
  display: none;
  background: #e0e0e0;
  color: #00537F;
  width: 600px;
  height: 30px;
}

#topnav ul li ul.submenu a {
  display: inline;
  color: #00537F;
  padding: 4px 8px;
}

#topnav ul.submenu a:hover {
  background-color: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="topnav">
  <ul>sds
    <li>
      <a href="#">Admin</a>
    </li>
    <li>
      <a href="#"> MAC </a>
      <ul class="submenu">
        <li><a href="#">Master Data</a></li>
        <li>
          <a href="#">Transaction Data</a>
          <ul>
            <li><a href="#">Company Master</a></li>
            <li><a href="#">Location Master</a></li>
            <li><a href="#">Size Master</a></li>
          </ul>
        </li>
        <li>
          <a href="#">Admin Data</a>
        </li>
      </ul>

    </li>
  </ul>
</div>

2 个答案:

答案 0 :(得分:0)

在不使用jquery或javascript的情况下尝试以下代码段,您只需使用css即可完成。并根据您的问题进行了更新

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

#nav li {
  display: block;
  position: relative;
  float: left;
  background: #000000;
}

#nav li a {
  display: block;
  padding: 0;
  text-decoration: none;
  width: 200px;
  line-height: 35px;
  color: #fff;
}

#nav li li a {
  font-size: 80%;
}

#nav li:hover {
  background: #ff0000;
}

#nav ul {
  position: absolute;
  padding: 0;
  left: 0;
  display: none;
}

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

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

#nav li li:hover ul {
  display: block;
  margin-left: 200px;
  margin-top: -35px;
}
<div id="topnav">
  <ul id="nav">
    <li>
      <a href="#">Admin</a>
    </li>
    <li>
      <a href="#"> MAC </a>
      <ul class="submenu">
        <li><a href="#">Master Data</a></li>
        <li>
          <a href="#">Transaction Data &#8658; </a>
          <ul>
            <li><a href="#">Company Master</a></li>
            <li><a href="#">Location Master</a></li>
            <li><a href="#">Size Master</a></li>
          </ul>
        </li>
        <li>
          <a href="#">Admin Data</a>
        </li>
      </ul>

    </li>
  </ul>
</div>

答案 1 :(得分:-1)

你可以试试这个

&#13;
&#13;
#menu {
  background: #343434;
  color: #eee;
  height: 35px;
  border-bottom: 4px solid #eeeded
}

#menu ul,
#menu li {
  margin: 0 0;
  padding: 0 0;
  list-style: none
}

#menu ul {
  height: 35px
}

#menu li {
  float: left;
  display: inline;
  position: relative;
  font: bold 12px Arial;
  text-shadow: 0 -1px 0 #000;
  border-right: 1px solid #444;
  border-left: 1px solid #111;
  text-transform: uppercase
}

#menu li:first-child {
  border-left: none
}

#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
  color: #eee;
}

#menu li:hover > a,
#menu li a:hover {
  background: #111
}

#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 35px;
  opacity: 0;
  cursor: pointer
}

#menu label {
  font: bold 30px Arial;
  display: none;
  width: 35px;
  height: 36px;
  line-height: 36px;
  text-align: center
}

#menu label span {
  font-size: 12px;
  position: absolute;
  left: 35px
}

#menu ul.menus {
  height: auto;
  width: 180px;
  background: #111;
  position: absolute;
  z-index: 99;
  display: none;
  border: 0;
}

#menu ul.menus li {
  display: block;
  width: 100%;
  font: 12px Arial;
  text-transform: none;
}

#menu li:hover ul.menus {
  display: block
}

#menu a.home {
  background: #c00;
}

#menu a.prett {
  padding: 0 27px 0 14px
}

#menu a.prett::after {
  content: "";
  width: 0;
  height: 0;
  border-width: 6px 5px;
  border-style: solid;
  border-color: #eee transparent transparent transparent;
  position: absolute;
  top: 15px;
  right: 9px
}

#menu ul.menus a:hover {
  background: #333;
}

#menu ul.menus .submenu {
  display: none;
  position: absolute;
  left: 180px;
  background: #111;
  top: 0;
  width: 180px;
}

#menu ul.menus .submenu li {
  background: #111;
}

#menu ul.menus .has-submenu:hover .submenu {
  display: block;
}
&#13;
<nav>
  <ul id='menu'>
    <li><a class='home' href='/'>Home</a></li>
    <li><a class='prett' href='#' title='Menu'>Menu</a>
      <ul class='menus'>
        <li class='has-submenu'><a class='prett' href='Dropdown 1' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
          <ul class='submenu'>
            <li><a href="#" title="Sub Menu">Sub Menu</a></li>
            <li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
            <li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
          </ul>
        </li>
        <li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
        <li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
      </ul>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;