reactjs - 如何让我的导航栏与页面的其余部分重叠?
问题描述
当我向下滚动时,我希望我的导航栏被固定并保持在页面的其余部分之上,但是每当我添加“位置:固定;”时 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
推荐阅读
- r - 将数据框的多列从字符串转换为R中的数字
- c# - 正则表达式匹配所有字符直到到达字符,但也包括最后一个匹配
- javascript - 使用javascript验证表单中的选择时出错
- rust - 如何测试对象的其他特征函数
- html - Outlook 桌面的徽标大小调整
- powershell - [System.Web.Security.Membership]::GeneratePassword() - 找不到类型
- c# - OnItemDataBound 将列表视图中的链接按钮注册到更新面板脚本管理器,但是,它们仅在第一次单击时起作用
- java - 具有相同存储库和实体的多个数据源
- database - 如何在 GDPR 下将用户名存储在数据库中?
- c - 比较频繁输入数据和存储 MAX 和 MIN 值的快速方法