Displaying a sub-sub-menu on hover of menu with CSS

余生长醉 提交于 2021-01-28 09:10:12

问题


I need to display sub-sub-menu on hover of my sub-menu. So far I did code to display menu -> sub-menu on menu click, but to proceed I want a functionality to display sub-sub-menu on hover of my sub-menu. Can somebody help me to achieve the same?

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>

回答1:


Try the following snippet without using jquery or javascript, you can get it done using only css. And have updated according to your question

#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>



回答2:


you can try this

#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;
}
<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>


来源:https://stackoverflow.com/questions/49191300/displaying-a-sub-sub-menu-on-hover-of-menu-with-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!