HTML 导航栏下拉菜单在尝试使其粘滞时变得无响应
HTML nav bar dropdown become unresponsive when trying to make it sticky
我正在尝试将粘性导航栏添加到我正在创建的示例网页中。下面附上了代码的工作示例。但是,当我一直向下滚动时,我的导航栏下拉列表停止工作。再次向上滚动时它工作正常。我不确定这里出了什么问题。感谢您对这里出了什么问题的任何想法。
.header {
padding: 10px;
text-align: center;
background: black;
color: white;
}
.navbar {
overflow: hidden;
background-color: #47494f;
z-index: 5;
}
.keepItTop {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
}
/* Style the navigation bar links */
.navbar a {
float: left;
font-size: 16px;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
/* Right-aligned link */
.navbar a.right {
float: right;
}
/* Change color on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
/*Dropdown Menu*/
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.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: 6;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.homeVideo {
width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="header">
<h1>Sample WebPage</h1>
</div>
<div class="keepItTop">
<div class="navbar">
<a href="index.html">Home</a>
<a href="about.html">About Me</a>
<div class="dropdown">
<button class="dropbtn">Games
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="swimming.html">Swimming</a>
<a href="badminton.html">Badminton</a>
</div>
</div>
<a href="contact.html">Contact Us</a>
</div>
</div>
<div>
<video class="homeVideo" autoplay muted loop>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
</div>
您的视频的 z 堆叠级别高于您的菜单。将菜单置于其上方。
.header {
padding: 10px;
text-align: center;
background: black;
color: white;
}
.navbar {
overflow: hidden;
background-color: #47494f;
z-index: 5;
}
.keepItTop {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
z-index: 99; /* <----------------------- HERE */
}
/* Style the navigation bar links */
.navbar a {
float: left;
font-size: 16px;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
/* Right-aligned link */
.navbar a.right {
float: right;
}
/* Change color on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
/*Dropdown Menu*/
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.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: 6;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.homeVideo {
width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="header">
<h1>Sample WebPage</h1>
</div>
<div class="keepItTop">
<div class="navbar">
<a href="index.html">Home</a>
<a href="about.html">About Me</a>
<div class="dropdown">
<button class="dropbtn">Games
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="swimming.html">Swimming</a>
<a href="badminton.html">Badminton</a>
</div>
</div>
<a href="contact.html">Contact Us</a>
</div>
</div>
<div>
<video class="homeVideo" autoplay muted loop>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
</div>
我正在尝试将粘性导航栏添加到我正在创建的示例网页中。下面附上了代码的工作示例。但是,当我一直向下滚动时,我的导航栏下拉列表停止工作。再次向上滚动时它工作正常。我不确定这里出了什么问题。感谢您对这里出了什么问题的任何想法。
.header {
padding: 10px;
text-align: center;
background: black;
color: white;
}
.navbar {
overflow: hidden;
background-color: #47494f;
z-index: 5;
}
.keepItTop {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
}
/* Style the navigation bar links */
.navbar a {
float: left;
font-size: 16px;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
/* Right-aligned link */
.navbar a.right {
float: right;
}
/* Change color on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
/*Dropdown Menu*/
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.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: 6;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.homeVideo {
width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="header">
<h1>Sample WebPage</h1>
</div>
<div class="keepItTop">
<div class="navbar">
<a href="index.html">Home</a>
<a href="about.html">About Me</a>
<div class="dropdown">
<button class="dropbtn">Games
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="swimming.html">Swimming</a>
<a href="badminton.html">Badminton</a>
</div>
</div>
<a href="contact.html">Contact Us</a>
</div>
</div>
<div>
<video class="homeVideo" autoplay muted loop>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
</div>
您的视频的 z 堆叠级别高于您的菜单。将菜单置于其上方。
.header {
padding: 10px;
text-align: center;
background: black;
color: white;
}
.navbar {
overflow: hidden;
background-color: #47494f;
z-index: 5;
}
.keepItTop {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
z-index: 99; /* <----------------------- HERE */
}
/* Style the navigation bar links */
.navbar a {
float: left;
font-size: 16px;
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
/* Right-aligned link */
.navbar a.right {
float: right;
}
/* Change color on hover */
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
/*Dropdown Menu*/
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.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: 6;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.homeVideo {
width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="header">
<h1>Sample WebPage</h1>
</div>
<div class="keepItTop">
<div class="navbar">
<a href="index.html">Home</a>
<a href="about.html">About Me</a>
<div class="dropdown">
<button class="dropbtn">Games
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="swimming.html">Swimming</a>
<a href="badminton.html">Badminton</a>
</div>
</div>
<a href="contact.html">Contact Us</a>
</div>
</div>
<div>
<video class="homeVideo" autoplay muted loop>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
</div>