I’m making a simple drop down menu that I’m using JavaScript to show and hide. The menu shows, and the links work still, but when I hover past the first link to drop down, the whole drop down menu goes away, even though I set a specific height for it. I also have a separate div with content below it, and the text in that div gets pushed out of the way, though I thought z-index would fix that.
function showDrop() {
document.getElementById("dropdown").style.visibility = "visible";
}
function hideDrop() {
document.getElementById("dropdown").style.visibility = "hidden";
}
#nav {
/* margin-left: 550px;
padding-top: 110px; */
font-family: 'Averia Serif Libre', cursive;
font-size: 24px;
position: relative;
}
#nav ul li {
display: block;
float: left;
margin-right: 10px;
}
#nav ul li a:link,
#nav ul li a:visited {
text-decoration: none;
float: left;
}
#nav ul li a:hover,
#nav ul li a:active {
color: #00B2EE;
}
#nav ul li ul {
visibility: hidden;
top: 0;
left: 0;
display: block;
width: 100px;
height: 100px;
clear: both;
z-index: 2;
padding-top: 2px;
}
#nav ul li ul li {
width: 100px;
z-index: inherit;
background-color: #AAA;
font-size: 16px;
line-height: 22px;
}
<div id="nav">
<ul>
<li><a href="#" id="nAbout">About</a></li>
<li><a href="#" id="nPortfolio" onMouseOver="showDrop();">Portfolio</a>
<ul id="dropdown" onMouseOut="hideDrop();">
<li><a href="#">Print Design</a></li>
<li><a href="#">Web Design</a></li>
<li><a href="#">Illustration</a></li>
</ul>
</li>
<li><a href="#" id="nContact">Contact</a></li>
<li><a href="#" id="nBlog">Blog</a></li>
</ul>
</div>
Use the
onmouseleaveevent instead, becauseonmouseoutconsiders the mouse to be ‘out’ even if you’re hovering children of the element. Sample code:Hope that helped.