Reactjs 如何使导航栏与页面的其余部分重叠?
当我向下滚动时,我希望我的导航栏是固定的,并保持在页面其余部分的上方,但每当我添加“position:fixed;”函数在css页面上消失 下面是JS和Css中导航栏的代码Reactjs 如何使导航栏与页面的其余部分重叠?,reactjs,Reactjs,当我向下滚动时,我希望我的导航栏是固定的,并保持在页面其余部分的上方,但每当我添加“position:fixed;”函数在css页面上消失 下面是JS和Css中导航栏的代码 import Logo from '../../Images/mmt-white.png'; import './Navbar.css'; const Navbar = props => ( <header className="Navbar"> <na
import Logo from '../../Images/mmt-white.png';
import './Navbar.css';
const Navbar = props => (
<header className="Navbar">
<nav className="Navbar__navigation">
<div></div>
<div className='Navbar__logo'><img src={Logo}/></div>
<div className='spacer '/>
<div className="Navbar_navigation-items">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/">Servicos</a></li>
<li><a href="/">Depoimentos</a></li>
<li><a href="/">Começando</a></li>
<li><a href="/">Contacte-nos</a></li>
</ul>
</div>
<div className="Navbar_navigation_items_acessar">
<ul>
<li><a href="/">Acessar</a></li>
</ul>
</div>
</nav>
</header>
);
export default Navbar;
您可能希望将以下内容添加到
NavBar.css
文件中的NavBar
类中,以添加如下位置属性:
left: 0;
position: fixed;
top: 0;
z-index: 1; // increase this value if you have other elements at varying levels of elevation
或者,您可能希望在NavBar.css
文件中的NavBar
类中添加以下内容,而不是position:fixed
(这取决于您正在寻找的行为):
也请发布你的CSS。我尝试了两种方法,但仍然不起作用;(我会上传我的CSS
className
你正在使用的是NavBar
,但我看不出这个类是你的CSS文件。你是说sticky
?别担心这是我的错误,我必须在我的CSS中定位…这就是它不起作用的原因!
.NavbarItems {
background: linear-gradient(90deg,rgb(49,56,64)0%,rgba(49, 56, 64, 1) 100%);
height: 10vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 1,2rem;
left: 0;
position: fixed;
top: 0;
z-index: 1;
position: relative;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.navbar-logo{
position: absolute;
top: 0;
left: 0;
transform: translate(15%, 10%);
}
.fa-react{
margin-left: 0.5rem;
font-size: 1,6rem;
}
.nav-menu {
display: grid;
grid-template-columns: repeat(6, auto);
grid-gap: 10px;
transform: translate(8%, 0%);
list-style: none;
margin-left: 10vh;
text-align: center;
width: 80vw;
justify-content: end;
padding-left: 10vh;
margin-right: 1rem;
font-size: 10px;
}
.nav-links {
color:#edf0f1;
margin-top: 2rem;
padding-right: 2rem;
padding: 0;
text-decoration: none;
font-size: 22px;
font-family: monospace;
text-transform: uppercase;
}
.nav-links:hover{
border-radius: 4px;
transition: all 0.2s ease0;
color: #0088a9;
}
.fa-bars{
color: #ffffff;
}
button{
margin-left: 10vh;
right: 0;
padding: 9px 25px;
background-color: rgba(0, 136, 169, 1);
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease 0s;
font-family: "Montserrat", sans-serif;
font-weight: 500;
color: #edf0f1;
font-size: 17px;
text-decoration: none;
}
.menu-icon{
display: none;
}
.nav-links-mobile{
color: transparent;
}
left: 0;
position: fixed;
top: 0;
z-index: 1; // increase this value if you have other elements at varying levels of elevation
position: sticky;
top: 0; // vertical position you'd like the element to stick to
z-index: 1; // increase this value if you have other elements at varying levels of elevation