我希望导航栏固定并在向下滚动时保持在页面其余部分的上方,但是每当我添加“ position:fixed;”时,在CSS页面上的功能消失了。
这是JS和Css中导航栏的代码...
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;
这是我的app.js,我在其中导入所有页面和顶部的导航栏...
import Navbar from './components/NavBar/Navbar';
import Home from "./components/pages/HomePage/Home";
import Comecando from "./components/pages/Comecando/Comecando";
import Depoimentos from "./components/pages/Depoimentos/Depoimentos";
import Sac from "./components/pages/SAC/Sac";
import Servicos from "./components/pages/Servicos/Servicos";
import './App.css';
function App () {
return(
<div className='App'>
<div>
<Navbar/>
</div>
<Home/>
<Servicos/>
<Comecando/>
<Depoimentos/>
<Sac/>
</div>
);
}
export default App;
这是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;
}
您可能想NavBar
在NavBar.css
文件中的类中添加以下内容,以添加位置属性,如下所示:
left: 0;
position: fixed;
top: 0;
z-index: 1; // increase this value if you have other elements at varying levels of elevation
或者,您可能希望NavBar
在NavBar.css
文件中的类中添加以下内容,而不是添加position: fixed
(取决于您要查找的行为):
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句