Whenever my navbar
reaches the top of the screen, the links in the dropdown menu disappear.
I followed tutorials and examples from w3schools to build my webpage. Specifically:
This code snippet exemplifies my issue:
window.onscroll = function() {
myFunction()
};
var navbar = document.getElementById("navi");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
#navi {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
.drop {
float: left;
overflow: hidden;
}
.drop .dropbutton {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 20px 25px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
#navbar a:hover,
.drop:hover .dropbutton {
background-color: #25aa25;
}
.links {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 210px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.links a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.links a:hover {
background-color: #ddd;
}
.drop:hover .links {
display: block;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.sticky+article {
padding-top: 60px;
}
<body>
<header>
<h1>
Example for StackOverflow
</h1>
</header>
<nav id="navi">
<div class="drop">
<button class="dropbutton">
Button - dropdown
</button>
<div class="links">
<a href="">Random link 1</a>
<a href="">Random link 2</a>
<a href="">Random link 3</a>
</div>
</div>
</nav>
<article>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
<p>Just for filling in the page</p>
</article>
</body>