繁体   English   中英

固定导航-无滚动

[英]Fixed Navigation - no scrolling

您好,目前正在做一个快速网站,已设置标题和切换导航,但是由于导航栏已固定,如果导航栏高于屏幕高度,它将不允许在导航栏上滚动,请尝试溢出:和位置相对,但似乎找不到解决方法,感谢您提供的任何帮助。

 function NavToggle() { var tn = document.getElementById("nav-bar") if(tn.style.display === "none"){ tn.style.display = "block"; } else { tn.style.display = "none"; } } 
 *{ box-sizing: border-box; font-family: sans-serif; margin: 0; padding: 0; } html, body{ overflow-y: auto; } ul{ margin: 0; padding: 0; } /* Header and Navigation */ header{ width: 100vw; height: auto; background-color: #222; position: fixed; z-index: 1; } nav{ display: none; z-index: 2; } .title-wrapper{ display: flex; align-items: center; justify-content: space-between; height: 50px; width: 100vw; padding: 0 10px; } /* menu base styles */ nav ul{ list-style-type: none; } nav li{ height: 50px; } .title-wrapper>a{ font-size: 16px; } a{ color: #999; } nav a{ text-decoration: none; display: flex; align-items: center; justify-content: flex-start; height: 100%; font-size: 14px; padding-left: 10px; } nav a:hover{ color: #fff; } /* Menu Toggle Styling */ .menu-toggle{ font-size: 26px; color: #fff; cursor: pointer; padding: 0 15px 0 10px; display: flex; align-items: center; justify-content: center; } .menu-toggle-button{ padding: inherit; } 
 <header class="nav-wrapper header-container"> <div class="header-wrapper"> <div class="title-wrapper"> <a class="a-tag header-title">Chemical Finger Print Analysis</a> <div class="menu-toggle"> <span id="menu-toggle-button" onclick="NavToggle()">&#9776;</span> </div> </div> <nav id="nav-bar"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Data</a></li> <li><a href="#">Reports</a></li> </ul> <ul> <li><a href="#">Register</a></li> <li><a href="#">Login</a></li> </ul> </nav> </div> </header> 

您已经固定了整个<header>但是需要将title-wrapper固定为固定,这是工作代码

 function NavToggle() { var tn = document.getElementById("nav-bar") if (tn.style.display === "none") { tn.style.display = "block"; } else { tn.style.display = "none"; } } 
 * { box-sizing: border-box; font-family: sans-serif; margin: 0; padding: 0; } html, body { overflow-y: auto; } ul { margin: 0; padding: 0; } /* Header and Navigation */ header { width: 100vw; height: auto; background-color: #222; position: relative; z-index: 1; } nav { display: none; z-index: 2; } .title-wrapper { display: flex; align-items: center; justify-content: space-between; height: 50px; width: 100vw; padding: 0 10px; position: fixed; background-color: #222222; } /* menu base styles */ nav ul { list-style-type: none; } nav li { height: 50px; } .title-wrapper>a { font-size: 16px; } a { color: #999; } nav a { text-decoration: none; display: flex; align-items: center; justify-content: flex-start; height: 100%; font-size: 14px; padding-left: 10px; } nav a:hover { color: #fff; } /* Menu Toggle Styling */ .menu-toggle { font-size: 26px; color: #fff; cursor: pointer; padding: 0 15px 0 10px; display: flex; align-items: center; justify-content: center; } .menu-toggle-button { padding: inherit; } 
 <header class="nav-wrapper header-container"> <div class="header-wrapper"> <div class="title-wrapper"> <a class="a-tag header-title">Chemical Finger Print Analysis</a> <div class="menu-toggle"> <span id="menu-toggle-button" onclick="NavToggle()">&#9776;</span> </div> </div> <nav id="nav-bar"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Data</a></li> <li><a href="#">Reports</a></li> </ul> <ul> <li><a href="#">Register</a></li> <li><a href="#">Login</a></li> </ul> </nav> </div> </header> 

只需添加overflow-y: auto;max-height: 100vh; 像这样.header

 function NavToggle() { var tn = document.getElementById("nav-bar") if(tn.style.display === "none"){ tn.style.display = "block"; } else{ tn.style.display = "none"; } } 
 html, body{ overflow-y: auto; } ul { margin: 0; padding: 0; } /* Header and Navigation */ header { width: 100vw; height: auto; background-color: #222; position: fixed; z-index: 1; overflow-y: auto; overflow-x: hidden; max-height: 100vh; } nav { display: none; z-index: 2; } .title-wrapper{ display: flex; align-items: center; justify-content: space-between; height: 50px; width: 100vw; padding: 0 10px; } /* menu base styles */ nav ul{ list-style-type: none; } nav li{ height: 50px; } .title-wrapper>a{ font-size: 16px; } a{ color: #999; } nav a{ text-decoration: none; display: flex; align-items: center; justify-content: flex-start; height: 100%; font-size: 14px; padding-left: 10px; } nav a:hover{ color: #fff; } /* Menu Toggle Styling */ .menu-toggle{ font-size: 26px; color: #fff; cursor: pointer; padding: 0 15px 0 10px; display: flex; align-items: center; justify-content: center; } .menu-toggle-button{ padding: inherit; } 
 <header class="nav-wrapper header-container"> <div class="header-wrapper"> <div class="title-wrapper"> <a class="a-tag header-title">Chemical Finger Print Analysis</a> <div class="menu-toggle"> <span id="menu-toggle-button" onclick="NavToggle()">&#9776;</span> </div> </div> <nav id="nav-bar"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Data</a></li> <li><a href="#">Reports</a></li> </ul> <ul> <li><a href="#">Register</a></li> <li><a href="#">Login</a></li> </ul> </nav> </div> </header> 

希望这会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM