让下拉留在悬停

时间:2016-01-21 08:16:27

标签: html css

我正在为一个学校项目建立一个网站,并且拥有以前的少量编码经验。我听说只使用CSS就可以在不使用JavaScript的情况下实现下拉菜单。

我无法将下拉菜单停留在悬停状态,当鼠标下移到li元素时,它会消失:



/* Configuration of menu width */
html body ul.sf-menu ul,
html body ul.sf-menu ul li {
  width: 200px;
}
html body ul.sf-menu ul ul {
  margin: 0 0 0 200px;
}
/* Framework for proper showing/hiding/positioning */

ul.sf-menu,
ul.sf-menu * {
  margin: 0;
  padding: 0;
}
ul.sf-menu {
  display: block;
  position: relative;
}
ul.sf-menu li {
  display: block;
  list-style: none;
  float: left;
  position: relative;
}
ul.sf-menu li:hover {
  visibility: inherit;
  /* fixes IE7 'sticky bug' */
}
ul.sf-menu a {
  display: block;
  position: relative;
}
ul.sf-menu ul {
  position: absolute;
  left: 0;
  width: 150px;
  top: auto;
  left: -999999px;
}
ul.sf-menu ul a {
  zoom: 1;
  /* IE6/7 fix */
}
ul.sf-menu ul li {
  float: left;
  /* Must always be floated otherwise there will be a rogue 1px margin-bottom in IE6/7 */
  width: 150px;
}
ul.sf-menu ul ul {
  top: 0;
  margin: 0 0 0 150px;
}
ul.sf-menu li:hover ul,
ul.sf-menu li:focus ul,
ul.sf-menu li.sf-hover ul,
ul.sf-menu ul li:hover ul,
ul.sf-menu ul li:focus ul,
ul.sf-menu ul li.sf-hover ul,
ul.sf-menu ul ul li:hover ul,
ul.sf-menu ul ul li:focus ul,
ul.sf-menu ul ul li.sf-hover ul,
ul.sf-menu ul ul ul li:hover ul,
ul.sf-menu ul ul ul li:focus ul,
ul.sf-menu ul ul ul li.sf-hover ul {
  left: auto;
}
ul.sf-menu li:hover ul ul,
ul.sf-menu li:focus ul ul,
ul.sf-menu li.sf-hover ul ul,
ul.sf-menu ul li:hover ul ul,
ul.sf-menu ul li:focus ul ul,
ul.sf-menu ul li.sf-hover ul ul,
ul.sf-menu ul ul li:hover ul ul,
ul.sf-menu ul ul li:focus ul ul,
ul.sf-menu ul ul li.sf-hover ul ul,
ul.sf-menu ul ul ul li:hover ul ul,
ul.sf-menu ul ul ul li:focus ul ul,
ul.sf-menu ul ul ul li.sf-hover ul ul {
  left: -999999px;
}
/* autoArrows CSS */

span.sf-arrow {
  width: 7px;
  height: 7px;
  position: absolute;
  top: 20px;
  right: 5px;
  display: block;
  background: url(../images/arrows-white.png) no-repeat 0 0;
  overflow: hidden;
  /* making sure IE6 doesn't overflow and expand the box */
  font-size: 1px;
}
ul ul span.sf-arrow {
  right: 5px;
  top: 20px;
  background-position: 0 100%;
}
/* Theming the menu */

ul#nav {
  float: left;
}
ul#nav ul {
  background: #555;
  margin-top: 5px;
  padding-bottom: 15px;
}
ul#nav li a {
  padding: 7px 25px 6px 25px;
  font: 170%'Yanone Kaffeesatz', arial, sans-serif;
  text-decoration: none;
  color: #000;
  margin-right: 2px;
}
ul#nav li a:hover,
ul#nav li a:focus,
ul#nav li.selected a,
ul#nav li.selected a:hover,
ul#nav li.selected a:focus {
  color: #09D4FF;
  text-shadow: none;
}
ul#nav ul li a {
  color: #FFF;
}
ul#nav ul li a:hover {
  color: #09D4FF;
  margin-right: 2px;
}
/* Image Transitions */

ul#images {
  margin: 0 15px 10px 0;
  padding: 0;
  list-style: none;
  position: relative;
}
ul#images li {
  display: block;
  overflow: hidden;
  padding: 0;
  float: left;
  width: 155px;
  height: 300px;
}

<nav>
  <ul class="sf-menu" id="nav">
    <li class="selected"><a href="index.html">Home</a>
    </li>
    <li><a href="form.html"> Form </a>
    </li>
    <li><a href="page.html">A Page</a>
    </li>
    <li><a href="another_page.html">Another Page</a>
    </li>
    <li><a href="#">Example Drop Down</a>
      <ul>
        <li><a href="#">Drop Down One</a>
        </li>
        <li><a href="#">Drop Down Two</a>
          <ul>
            <li>
              <img src="arrows-white.png" />
            </li>
            <li><a href="#">Sub Drop Down One</a>
            </li>
            <li><a href="#">Sub Drop Down Two</a>
            </li>
            <li><a href="#">Sub Drop Down Three</a>
            </li>
            <li><a href="#">Sub Drop Down Four</a>
            </li>
            <li><a href="#">Sub Drop Down Five</a>
            </li>
          </ul>
        </li>
        <li><a href="#">Drop Down Three</a>
        </li>
        <li><a href="#">Drop Down Four</a>
        </li>
        <li><a href="#">Drop Down Five</a>
        </li>
      </ul>
    </li>
    <li><a href="contact.php">Contact Us</a>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您的问题是,您只需要从margin-top移除#nav: -

ul#nav ul { 
  background: #555;
  //margin-top: 5px;
  padding-bottom: 15px;
}

这会导致您正在展示的悬停li及其子ul(下拉列表)之间留出空格。

下面的工作代码

&#13;
&#13;
html body ul.sf-menu ul,
html body ul.sf-menu ul li {
  width: 200px;
}
html body ul.sf-menu ul ul {
  margin: 0 0 0 200px;
}
/* Framework for proper showing/hiding/positioning */

ul.sf-menu,
ul.sf-menu * {
  margin: 0;
  padding: 0;
}
ul.sf-menu {
  display: block;
  position: relative;
}
ul.sf-menu li {
  display: block;
  list-style: none;
  float: left;
  position: relative;
}
ul.sf-menu li:hover {
  visibility: inherit;
  /* fixes IE7 'sticky bug' */
}
ul.sf-menu a {
  display: block;
  position: relative;
}
ul.sf-menu ul {
  position: absolute;
  left: 0;
  width: 150px;
  top: auto;
  left: -999999px;
}
ul.sf-menu ul a {
  zoom: 1;
  /* IE6/7 fix */
}
ul.sf-menu ul li {
  float: left;
  /* Must always be floated otherwise there will be a rogue 1px margin-bottom in IE6/7 */
  width: 150px;
}
ul.sf-menu ul ul {
  top: 0;
  margin: 0 0 0 150px;
}
ul.sf-menu li:hover ul,
ul.sf-menu li:focus ul,
ul.sf-menu li.sf-hover ul,
ul.sf-menu ul li:hover ul,
ul.sf-menu ul li:focus ul,
ul.sf-menu ul li.sf-hover ul,
ul.sf-menu ul ul li:hover ul,
ul.sf-menu ul ul li:focus ul,
ul.sf-menu ul ul li.sf-hover ul,
ul.sf-menu ul ul ul li:hover ul,
ul.sf-menu ul ul ul li:focus ul,
ul.sf-menu ul ul ul li.sf-hover ul {
  left: auto;
}
ul.sf-menu li:hover ul ul,
ul.sf-menu li:focus ul ul,
ul.sf-menu li.sf-hover ul ul,
ul.sf-menu ul li:hover ul ul,
ul.sf-menu ul li:focus ul ul,
ul.sf-menu ul li.sf-hover ul ul,
ul.sf-menu ul ul li:hover ul ul,
ul.sf-menu ul ul li:focus ul ul,
ul.sf-menu ul ul li.sf-hover ul ul,
ul.sf-menu ul ul ul li:hover ul ul,
ul.sf-menu ul ul ul li:focus ul ul,
ul.sf-menu ul ul ul li.sf-hover ul ul {
  left: -999999px;
}
/* autoArrows CSS */

span.sf-arrow {
  width: 7px;
  height: 7px;
  position: absolute;
  top: 20px;
  right: 5px;
  display: block;
  background: url(../images/arrows-white.png) no-repeat 0 0;
  overflow: hidden;
  /* making sure IE6 doesn't overflow and expand the box */
  font-size: 1px;
}
ul ul span.sf-arrow {
  right: 5px;
  top: 20px;
  background-position: 0 100%;
}
/* Theming the menu */

ul#nav {
  float: left;
}
ul#nav ul {
  background: #555;
  //margin-top: 5px;
  padding-bottom: 15px;
}
ul#nav li a {
  padding: 7px 25px 6px 25px;
  font: 170%'Yanone Kaffeesatz', arial, sans-serif;
  text-decoration: none;
  color: #000;
  margin-right: 2px;
}
ul#nav li a:hover,
ul#nav li a:focus,
ul#nav li.selected a,
ul#nav li.selected a:hover,
ul#nav li.selected a:focus {
  color: #09D4FF;
  text-shadow: none;
}
ul#nav ul li a {
  color: #FFF;
}
ul#nav ul li a:hover {
  color: #09D4FF;
  margin-right: 2px;
}
/* Image Transitions */

ul#images {
  margin: 0 15px 10px 0;
  padding: 0;
  list-style: none;
  position: relative;
}
ul#images li {
  display: block;
  overflow: hidden;
  padding: 0;
  float: left;
  width: 155px;
  height: 300px;
}
&#13;
<nav>
  <ul class="sf-menu" id="nav">
    <li class="selected"><a href="index.html">Home</a>
    </li>
    <li><a href="form.html"> Form </a>
    </li>
    <li><a href="page.html">A Page</a>
    </li>
    <li><a href="another_page.html">Another Page</a>
    </li>
    <li><a href="#">Example Drop Down</a>
      <ul>
        <li><a href="#">Drop Down One</a>
        </li>
        <li><a href="#">Drop Down Two</a>
          <ul>
            <li>
              <img src="arrows-white.png" />
            </li>
            <li><a href="#">Sub Drop Down One</a>
            </li>
            <li><a href="#">Sub Drop Down Two</a>
            </li>
            <li><a href="#">Sub Drop Down Three</a>
            </li>
            <li><a href="#">Sub Drop Down Four</a>
            </li>
            <li><a href="#">Sub Drop Down Five</a>
            </li>
          </ul>
        </li>
        <li><a href="#">Drop Down Three</a>
        </li>
        <li><a href="#">Drop Down Four</a>
        </li>
        <li><a href="#">Drop Down Five</a>
        </li>
      </ul>
    </li>
    <li><a href="contact.php">Contact Us</a>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以通过为包含嵌套li的每个ul元素添加类名来实现这一点,假设li的类名是.has-nested-ul,那么我们就可以控制max-height属性 - 我们可以使用height但它有缺点 - 就像这样:

JS Fiddle

&#13;
&#13;
html body ul.sf-menu ul, html body ul.sf-menu ul li {
  width: 200px;
}
html body ul.sf-menu ul {
  max-height: 0;
  overflow: hidden;
}
html body ul.sf-menu li.has-nested-ul:hover ul {
  max-height: 1000px;
}
&#13;
<nav>
  <ul class="sf-menu" id="nav">
    <li class="selected"><a href="index.html">Home</a>
    </li>
    <li><a href="form.html"> Form </a>
    </li>
    <li><a href="page.html">A Page</a>
    </li>
    <li><a href="another_page.html">Another Page</a>
    </li>
    <li class="has-nested-ul"><a href="#">Example Drop Down</a>
      <ul>
        <li><a href="#">Drop Down One</a>
        </li>
        <li><a href="#">Drop Down Two</a>
          <ul>
            <li>
              <img src="arrows-white.png" />
            </li>
            <li><a href="#">Sub Drop Down One</a>
            </li>
            <li><a href="#">Sub Drop Down Two</a>
            </li>
            <li><a href="#">Sub Drop Down Three</a>
            </li>
            <li><a href="#">Sub Drop Down Four</a>
            </li>
            <li><a href="#">Sub Drop Down Five</a>
            </li>
          </ul>
        </li>
        <li><a href="#">Drop Down Three</a>
        </li>
        <li><a href="#">Drop Down Four</a>
        </li>
        <li><a href="#">Drop Down Five</a>
        </li>
      </ul>
    </li>
    <li><a href="contact.php">Contact Us</a>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;