如何在悬停时扩展我的导航栏菜单

时间:2017-08-22 20:38:52

标签: javascript jquery html css css3

因此,如果您访问https://serverfault.com/并将鼠标悬停在GCSEs上,则会看到ChemistryMathsBiology的下拉列表。

我现在想要它:

  • 当您将鼠标悬停在其上方时,另一个菜单会从右侧延伸出来,与正在盘旋的内容一致 - 我可以为所选主题添加不同的页面

我该如何实现?

这是我当前的nav.js菜单:

var navigation = '<div class="container">'
navigation += '<a href="index.html"><img src="Images/homeicon.jpg" width="50" alt="Home"></a>'
navigation += '<div class="redhover">'
navigation += '<div class="dropdown">'
navigation += '<button class="dropbtn">GCSEs</button>'
navigation += '<div class="dropdown-content">'
navigation += '<a href="gcsechemistry.html">Chemistry</a>'
navigation += '<a href="gcsebiology.html">Biology</a>'
navigation += '<a href="gcsemaths.html">Maths</a>'
navigation += '</div>'
navigation += '</div>'
navigation += '<div class="dropdown">'
navigation += '<button class="dropbtn">A-Levels</button>'
navigation += '<div class="dropdown-content">'
navigation += '<a href="alevelchemistry.html">Chemistry</a>'
navigation += '<a href="alevelbiology.html">Biology</a>'
navigation += '</div>'
navigation += '</div>'
navigation += '<div class="dropdown">'
navigation += '<button class="dropbtn">University</button>'
navigation += '<div class="dropdown-content">'
navigation += '<a href="telecommunications.html">Telecommunications</a>'
navigation += '<a href="electronicengineering.html">Electronic Engineering</a>'
navigation += '<a href="engineeringmathematics.html">Engineering Mathemathics</a>'
navigation += '</div>'
navigation += '</div>'
navigation += '<div class="dropdown">'
navigation += '<button class="dropbtn">More</button>'
navigation += '<div class="dropdown-content">'
navigation += '<a href="aboutme.html">About me</a>'
navigation += '<a href="https://www.youtube.com/channel/UC5GPvJSUO4599z_ugFuoJwg">Youtube</a>'
navigation += '<a href="blog.html">Blog</a>'
navigation += '</div>'
navigation += '</div>'
navigation += '</div>'
navigation += '</div>'

document.getElementById("navigation").innerHTML = navigation;
.container {
  width: 100%;
  background-color: rgb(0, 0, 0);
  font-family: 'Montserrat';
  vertical-align: middle;
  height: 60px;
  position: fixed;
  top: 0;
  z-index: 999;
  opacity: 0.9;
}

.container a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 10px 10px;
  text-decoration: none;
  height: 20px;
  vertical-align: middle;
}

.dropdown {
  float: left;
  overflow: hidden;
}

span {
  vertical-align: middle;
}

.dropdown .dropbtn {
  font-size: 20px;
  border: none;
  outline: none;
  color: white;
  padding: 20px 20px;
  height: 60px;
  box-sizing: border-box;
  background-color: inherit;
}

.redhover a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
  font-family: 'Montserrat';
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div id="navigation"></div>

干杯!

1 个答案:

答案 0 :(得分:0)

您知道吗,您可以使用HTML标记代替JS来创建菜单。

我这里有一个codepen工作,但只能点击每个项目。
https://codepen.io/beljems/pen/egopbJ

我在这里没有悬停事件,因为当我同时使用悬停和点击时可能效果不佳。您也可以单击菜单外部以关闭下拉列表。

只有当该项目有下拉菜单时,才会显示下拉图标。