我遵循了有关使用纯CSS而不使用JavaScript的粘性导航栏的教程。问题是,当我向下滚动时,粘性导航栏未固定在页面顶部。在这个粘性导航栏下,我还有其他3个部分。每次我向下滚动页面时,导航栏都会进入各节的下方,并且不再起作用。在标题中,我有一个全角图片,上面有一些文字和一个按钮。
这是我的HTML导航栏代码:
header {
margin: auto;
position: relative;
width: 100%;
height: 100vh;
background: linear-gradient(black, transparent, black), url(images/architecture2.jpg);
background-size: cover;
background-position: center;
display: table;
top: 0;
}
nav {
position: absolute;
width: 100%;
height: 50px;
background: rgba(0, 0, 0, .1);
position: sticky;
top: 0;
}
nav ul {
display: flex;
margin: 0;
padding: 0 100px;
float: right;
}
nav ul li {
list-style: none;
}
nav ul li a {
display: block;
color: #fff;
padding: 0 15px;
text-decoration: none;
text-transform: capitalize;
font-weight: bold;
line-height: 90px;
}
.intro .inner {
margin-top: 200px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 10;
color: #fff;
}
.content h1 {
font-weight: 800;
color: #fff;
text-transform: uppercase;
font-size: 3.5rem;
}
.content p {
padding: 0;
margin-top: -35px;
margin-left: -1px;
color: #fff;
font-size: 2.2rem;
padding-bottom: 35px;
}
.btnD1 {
border: 2px solid #fff;
color: #000;
background: #fff;
border-radius: 50px;
padding: 16.5px 50px;
font-size: 1.15rem;
font-weight: 500;
text-decoration: none;
}
<html>
<head>
</head>
<body>
<div class="wrapper">
<header>
<nav>
<div class="menu">
<ul>
<li><a href="#">Acasa</a></li>
<li><a href="#despre">Despre Noi</a></li>
<li><a href="#calatorii">Calatorii</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#">SignIn</a></li>
</ul>
</div>
</nav>
<div class="intro">
<div class="inner">
<div class="content">
<h1>Călătoreşte cu noi în jurul lumii</h1>
<p>Destinaţia visată este la un click distanţă!</p>
<a class="btnD1" href="#">Rezervă acum</a>
</div>
</div>
</div>
</header>
</div>
</body>
</html>
对不起,我的英语不好。
谢谢!
答案 0 :(得分:2)
其行为的原因是在z-index
类中使用了.intro .inner
。您需要将z-index: 11;
类上的.nav
设置为使其出现在所有内容的顶部。
header {
margin: auto;
position: relative;
width: 100%;
height: 100vh;
background: linear-gradient(black, transparent, black), url(images/architecture2.jpg);
background-size: cover;
background-position: center;
display: table;
top: 0;
}
nav {
position: absolute;
width: 100%;
height: 50px;
background: rgba(0, 0, 0, .8);
position: sticky;
top: 0;
z-index: 11;
}
nav ul {
display: flex;
margin: 0;
padding: 0 100px;
float: right;
}
nav ul li {
list-style: none;
}
nav ul li a {
display: block;
color: #fff;
padding: 0 15px;
text-decoration: none;
text-transform: capitalize;
font-weight: bold;
line-height: 90px;
}
.intro .inner {
margin-top: 200px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 10;
color: #fff;
}
.content h1 {
font-weight: 800;
color: #fff;
text-transform: uppercase;
font-size: 3.5rem;
}
.content p {
padding: 0;
margin-top: -35px;
margin-left: -1px;
color: #fff;
font-size: 2.2rem;
padding-bottom: 35px;
}
.btnD1 {
border: 2px solid #fff;
color: #000;
background: #fff;
border-radius: 50px;
padding: 16.5px 50px;
font-size: 1.15rem;
font-weight: 500;
text-decoration: none;
}
<html>
<head>
</head>
<body>
<div class="wrapper">
<header>
<nav>
<div class="menu">
<ul>
<li><a href="#">Acasa</a></li>
<li><a href="#despre">Despre Noi</a></li>
<li><a href="#calatorii">Calatorii</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#">SignIn</a></li>
</ul>
</div>
</nav>
<div class="intro">
<div class="inner">
<div class="content">
<h1>Călătoreşte cu noi în jurul lumii</h1>
<p>Destinaţia visată este la un click distanţă!</p>
<a class="btnD1" href="#">Rezervă acum</a>
</div>
</div>
</div>
</header>
</div>
</body>
</html>
如果您不确定还要使用其他
z-index
个值。大多数人都将其设置为999
,以确保应该始终出现在所有内容之上的内容。
答案 1 :(得分:0)
nav
是粘性的,但仅在其父项(即.header
)内。要解决此问题,只需使.header
处于粘滞状态-这样,只要其父级(即.wrapper
)正在滚动,它就会保持粘滞状态。
并为其提供比其兄弟姐妹更高的z索引(.intro inner
具有z-index: 10
),否则滚动通过时它将位于intro inner
下方:
header {
position: sticky;
top: 0;
z-index: 11;
}
答案 2 :(得分:0)
问题在于粘性。我从导航中删除了它,还添加了一个过渡和z-index。
nav{
position: fixed;
width: 100%;
height: 50px;
background: rgba(0,0,0,.2);
top: 0;
z-index:999;
transition: 0.5s;
}
nav ul{
display: flex;
margin: 0;
padding: 0 100px;
float:right;
}
nav ul li{
list-style: none;
}
nav ul li a{
display: block;
color: #fff;
padding: 0 15px;
text-decoration: none;
text-transform: capitalize;
font-weight: bold;
line-height: 90px;
}
谢谢大家!