我有一个粘性响应式移动汉堡菜单,如果我在页面顶部,我必须单击两次才能将其下推文本然后打开。当我滚动并停在中间页面的某处并尝试打开它时,整个顶部导航栏消失并在我开始滚动时再次出现,但它永远不会展开显示其他链接。
<script>
function responsive() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
<script>
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};
// Get the myTopnav
var myTopnav = document.getElementById("myTopnav");
// Get the offset position of the myTopnav
var sticky = myTopnav.offsetTop;
// Add the sticky class to the myTopnav when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
if (window.pageYOffset >= sticky) {
myTopnav.classList.add("sticky")
} else {
myTopnav.classList.remove("sticky");
}
}
</script>
.sticky {
position: fixed;
top: 0;
width: 100%;
z-index: 5;
}
.topnav {
background-color: #005E7B ;
overflow: hidden;
z-index: 5;
font-weight: bold;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 0.875em 1em;
text-decoration: none;
font-size: 1.0625em;
}
/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
display: none;
}
/* Dropdown container - needed to position the dropdown content */
.dropdown {
float: left;
overflow: hidden;
}
/* Style the dropdown button to fit inside the topnav */
.dropdown .dropbtn {
font-size: 1.0625em;
border: none;
outline: none;
color: white;
padding: 0.875em 1em;
background-color: inherit;
font-family: inherit;
margin: 0;
}
/* Style the dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 10em;
box-shadow: 0px 0.5em 1em 0px rgba(0,0,0,0.2);
z-index: 1;
font-weight: normal;
}
/* Style the links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 0.75em 1em;
text-decoration: none;
display: block;
text-align: left;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child), .dropdown .dropbtn {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
.topnav.responsive {
position: fixed;
display: block;
width:100%;
z-index:999;
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {float: none;}
.topnav.responsive .dropdown-content {position: relative;}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
<nav>
<div class="topnav" id="myTopnav">
<a href="#about">About me</a>
<div class="dropdown">
<div class="dropbtn">Example
</div>
<div class="dropdown-content">
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
</div>
</div>
<a href="#Example">Example</a>
<a href="#about">Example</a>
<a href="#about">Example</a>
<a href="javascript:void(0);" class="icon" onclick="responsive()">☰</a>
</div>
</nav>
最佳答案
您所有的 HTML、CSS 和 JS 都是正确的,您只是在 responsive()
函数中犯了一个小错误,您在该函数中切换了 .responsive
类你的导航不正确,我在下面修复了:
function responsive() {
var x = document.getElementById("myTopnav");
x.classList.toggle('responsive');
}
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};
// Get the myTopnav
var myTopnav = document.getElementById("myTopnav");
// Get the offset position of the myTopnav
var sticky = myTopnav.offsetTop;
// Add the sticky class to the myTopnav when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
if (window.pageYOffset >= sticky) {
myTopnav.classList.add("sticky")
} else {
myTopnav.classList.remove("sticky");
}
}
body {
/* added long height to body so that it's scrollable */
height: 800px;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
z-index: 5;
}
.topnav {
background-color: #005E7B ;
overflow: hidden;
z-index: 5;
font-weight: bold;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 0.875em 1em;
text-decoration: none;
font-size: 1.0625em;
}
/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
display: none;
}
/* Dropdown container - needed to position the dropdown content */
.dropdown {
float: left;
overflow: hidden;
}
/* Style the dropdown button to fit inside the topnav */
.dropdown .dropbtn {
font-size: 1.0625em;
border: none;
outline: none;
color: white;
padding: 0.875em 1em;
background-color: inherit;
font-family: inherit;
margin: 0;
}
/* Style the dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 10em;
box-shadow: 0px 0.5em 1em 0px rgba(0,0,0,0.2);
z-index: 1;
font-weight: normal;
}
/* Style the links inside the dropdown */
.dropdown-content a {
float: none;
color: black;
padding: 0.75em 1em;
text-decoration: none;
display: block;
text-align: left;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child),
.dropdown .dropbtn {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
.topnav.responsive {
position: fixed;
display: block;
width:100%;
z-index:999;
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .dropdown {
float: none;
}
.topnav.responsive .dropdown-content {
position: relative;
}
.topnav.responsive .dropdown .dropbtn {
display: block;
width: 100%;
text-align: left;
}
}
<nav>
<div class="topnav" id="myTopnav">
<a href="#about">About me</a>
<div class="dropdown">
<div class="dropbtn">Example
</div>
<div class="dropdown-content">
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
<a href="#Example">Example</a>
</div>
</div>
<a href="#Example">Example</a>
<a href="#about">Example</a>
<a href="#about">Example</a>
<a class="icon" onclick="responsive()">☰</a>
</div>
</nav>
关于javascript - 粘性响应移动汉堡菜单不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695652/