如何隐藏此css下拉嵌套列表中的第三个嵌套列表?

时间:2016-08-26 05:07:28

标签: javascript jquery html css

我成功创建了这个不那么糟糕的css-only下拉列表。我试图隐藏.submenutwo,以便仅在.submenu悬停时才能看到它。如果我们能用css做到这一点那就不错了。但是,如果javascript或jQuery可以帮助它没问题。



/* define a fixed width for the entire menu */

.navigation {
  min-width: 300px;
}
/* reset our lists to remove bullet points and padding */

.mainmenu,
.submenu {
  list-style: none;
  padding: 0;
  margin: 0;
}
/* make ALL links (main and submenu) have padding and background color */

.mainmenu a {
  display: block;
  background-color: #CCC;
  text-decoration: none;
  padding: 10px;
  color: #000;
}
/* add hover behaviour */

.mainmenu a:hover {
  background-color: #C5C5C5;
}
/* when hovering over a .mainmenu item,
      display the submenu inside it.
      we're changing the submenu's max-height from 0 to 200px;
    */

.mainmenu li:hover .submenu {
  display: block;
  min-height: 200px;
  height: auto;
}
/*
      we now overwrite the background-color for .submenu links only.
      CSS reads down the page, so code at the bottom will overwrite the code at the top.
    */

.submenu a {
  background-color: #999;
}
/* hover behaviour for links inside .submenu */

.submenu a:hover {
  background-color: #666;
}
/* this is the initial state of all submenus.
      we set it to max-height: 0, and hide the overflowed content.
    */

.submenu {
  overflow: hidden;
  max-height: 0;
  -webkit-transition: all 0.5s ease-out;
}

<nav class="navigation">
  <ul class="mainmenu">
    <li><a href="">Apples</a>


      <ul class="submenu">
        <li><a href="">Green Apples</a>

          <ul class="submenutwo">
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Oranges</a>
      <ul class="submenu">
        <li><a href="">Option 2</a>
        </li>
        <li><a href="">Option 2</a>
        </li>
        <li><a href="">Option 2</a>
        </li>
      </ul>
    </li>
    <li><a href="">Grapes</a>
      <ul class="submenu">
        <li><a href="">Purple Grapes</a>

          <ul class="submenutwo">
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
            <li class='listOptionLvlThree'><a href="#">	Option 3</a>
            </li>
          </ul>
        </li>
        <li><a href="">Green Grapes</a>
        </li>
        <li><a href="">Red Grapes</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

.submenutwo {
    display: none }

.submenu:hover .submenutwo {
    display: initial }

以下是如何在css中完成的。基本上,这里发生的是什么。首先,您只需隐藏.submenutwo即可。触发悬停后,您只需将显示屏恢复为默认值,甚至其他显示值即可。

答案 1 :(得分:1)

  

可能会帮助你

&#13;
&#13;
/* define a fixed width for the entire menu */
.navigation {
  min-width: 300px;
}
.submenutwo {
    display: none }

.submenu:hover .submenutwo {
    display: initial }
/* reset our lists to remove bullet points and padding */
.mainmenu, .submenu {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* make ALL links (main and submenu) have padding and background color */
.mainmenu a {
  display: block;
  background-color: #CCC;
  text-decoration: none;
  padding: 10px;
  color: #000;
}

/* add hover behaviour */
.mainmenu a:hover {
    background-color: #C5C5C5;
}


/* when hovering over a .mainmenu item,
  display the submenu inside it.
  we're changing the submenu's max-height from 0 to 200px;
*/

.mainmenu li:hover .submenu {
  display: block;
  min-height: 200px;
  height:auto;
}

/*
  we now overwrite the background-color for .submenu links only.
  CSS reads down the page, so code at the bottom will overwrite the code at the top.
*/

.submenu a {
  background-color: #999;
}

/* hover behaviour for links inside .submenu */
.submenu a:hover {
  background-color: #666;
}

/* this is the initial state of all submenus.
  we set it to max-height: 0, and hide the overflowed content.
*/
.submenu {
 
  overflow: hidden;
  max-height: 0;
  -webkit-transition: all 0.5s ease-out;
}
&#13;
<nav class="navigation">
  <ul class="mainmenu">
  <li><a href="">Apples</a>


    <ul class="submenu">
        <li><a href="">Green Apples</a>

        <ul class="submenutwo">
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Oranges</a>
      <ul class="submenu">
        <li><a href="">Option 2</a></li>
        <li><a href="">Option 2</a></li>
        <li><a href="">Option 2</a></li>
      </ul>
    </li>
    <li><a href="">Grapes</a>
      <ul class="submenu">
          <li><a href="">Purple Grapes</a>

          <ul class="submenutwo">
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
            <li class='listOptionLvlThree'><a href="#"> Option 3</a></li>
          </ul>
        </li>
        <li><a href="">Green Grapes</a></li>
        <li><a href="">Red Grapes</a></li>
      </ul>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;