jquery - 尝试在现有导航栏上添加下拉项

标签 jquery html css

我正在尝试向我的“元素”菜单项添加下拉功能,如 [第 1/2/3 页] 所示。有没有一种有效的方法来做到这一点? (如果适用,欢迎使用 jQuery 解决方案)

DEMO

(出于某种原因,Projects 元素在 JSFiddle 中被拉伸(stretch)了,但无论如何它仍然有效)

<html>
 <header id="header">
  <div class="container">
   <img src="<!--logo-->"/>
   <nav id="nav">
    <ul>
     <li>
      <a href="portfolio.html">Portfolio</a>
     </li>
     <li><a href="#">Projects</a>
      <ul>
       <li><a href="page1.html">Page 1</a></li>
       <li><a href="page2.html">Page 2</a></li>
       <li><a href="page3.html">Page 3</a></li>
      </ul>
     </li>
     <li>
      <a href="contact.html">Contact</a>
     </li>
    </ul>
   </nav>
  </div>
 </header>
</html>

1 :

最佳答案

使用 ul ul { display: none; 隐藏嵌套菜单} 并触发隐藏/嵌套的 ulli:hover > ul { display: block;

.container {
  position: relative;
  margin: 0 auto;
  width: 94%;
  max-width: 1100px;
  font-family: helvetica, sans-serif;
}

#nav {
  position: absolute;
  right: 0;
  top: -15px;
  height: 60px;
  text-transform: uppercase;
  font-weight: bold;
}

#nav ul li {
  float: left;
  list-style: none;
}

#nav ul li a {
  display: block;
  color: white;
  text-decoration: none;
  padding: 0 10px;
}

#header {
  z-index: 2;
  position: fixed;
  width: 100%;
  height: 60px;
  line-height: 60px;
  background: #222;
  color: white;
}

#header img {
  width: 180px;
  height: 60px;
}

#header h1 {
  top: 0px;
  margin: 0px;
  font-size: 1.75em;
}


/* unvisited link */

a:link {
  color: blue;
}


/* visited link */

a:visited {
  color: purple;
}


/* mouse over link - Nav*/

#nav a:hover {
  color: black;
  background-color: gold;
}


/* mouse over link - regular*/

.back a:hover {
  color: blue;
}


/* selected link */

a:active {
  color: blue;
}

ul ul {
  display: none;
  position: absolute;
  background: #222;
  padding: 0;
}

#nav ul ul li {
  float: none;
}

li:hover > ul {
  display: block;
}
<header id="header">
  <div class="container">
    <img src="<!--logo-->" alt="logo placeholder" />
    <nav id="nav">
      <ul>
        <li>
          <a href="portfolio.html">Portfolio</a>
        </li>
        <li><a href="#">Projects</a>
          <ul>
            <li><a href="page1.html">Page 1</a>
              <li><a href="page2.html">Page 2</a></li>
              <li><a href="page3.html">Page 3</a></li>
          </ul>
          </li>
          <li>
            <a href="contact.html">Contact</a>
          </li>
      </ul>
    </nav>
  </div>
</header>

关于jquery - 尝试在现有导航栏上添加下拉项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708602/

相关文章:

html - 如何在CSS中为几何图形添加边框?

jquery - Bootstrap 3 : Full Slider not working. Uncaught TypeError : $(. ..).carousel 不是函数

javascript - 如何将 jQuery 添加到 VuePress 文件中

html - 如何对齐 3 个圆形图像?

javascript - 尝试了解 Angular 路由概念,但是当尝试重新加载页面时,没有显示任何内容。

html - 如何将 div 放在父 div 的相对两侧,在缩放时父宽度相对于它们?

javascript - 试图让 jQuery 删除按钮。它不工作

javascript - 落后的 JavaScript - 优化或更便宜的功能的机会

html - 媒体查询不工作(水平网站)

css - 动画边框不必要的延迟