因此,如果您访问https://serverfault.com/并将鼠标悬停在GCSEs
上,则会看到Chemistry
,Maths
和Biology
的下拉列表。
我现在想要它:
我该如何实现?
这是我当前的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>
干杯!
答案 0 :(得分:0)
您知道吗,您可以使用HTML
标记代替JS
来创建菜单。
我这里有一个codepen工作,但只能点击每个项目。
https://codepen.io/beljems/pen/egopbJ
我在这里没有悬停事件,因为当我同时使用悬停和点击时可能效果不佳。您也可以单击菜单外部以关闭下拉列表。
只有当该项目有下拉菜单时,才会显示下拉图标。