簡體   English   中英

如何滾動固定的雙導航欄?

[英]How to scroll a fixed double navbar?

所以我有兩個固定的響應式導航欄。 這是我的代碼:

 function myFunction() { var x = document.getElementById("myTopnav"); if (x.className === "topnav") { x.className += " responsive"; } else { x.className = "topnav"; } } 
 body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .outer { position: fixed; top: 0px; widtH: 100%; } .topnav { overflow: hidden; background-color: #333; } .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .topnav a:hover { background-color: #ddd; color: black; } .active { background-color: #4CAF50; color: white; } .topnav .icon { display: none; } @media screen and (max-width: 600px) { .topnav a:not(:first-child) {display: none;} .topnav a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .topnav.responsive {position: relative;} .topnav.responsive .icon { position: absolute; right: 0; top: 0; } .topnav.responsive a { float: none; display: block; text-align: left; } } .secondnav { background:red; width:100%; height:42px; } 
 <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <div class="outer"> <div class="topnav" id="myTopnav"> <a href="#home" class="active">Home</a> <a href="#news">News</a> <a href="#contact">Contact</a> <a href="#about">About</a> <a href="#help">Help</a> <a href="#terms">Terms</a> <a href="#privacy">Privacy</a> <a href="#cookies">Cookies</a> <a href="#activitylog">Activity Log</a> <a href="#events">Events</a> <a href="#settings">Settings</a> <a href="#notifications">Notifications</a> <a href="#friendrequest">Friend Requests</a> <a href="javascript:void(0);" class="icon" onclick="myFunction()"> <i class="fa fa-bars"></i> </a> </div> <div class="secondnav"> </div> </div> </body> </html> 

問題是,當調整窗口大小並且導航欄變得響應並且我單擊菜單按鈕時,由於導航欄中有很多項目,因此我無法滾動瀏覽菜單。 如何解決這個問題? 請不要建議其他方法。 請嘗試使用此代碼。
並預先感謝!

您可以添加以下CSS:

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
    max-height: 75vh; /* Set to the hegith you want to use */
    overflow-y: scroll; /* Enable scroll */
  }

如果您不想顯示滾動條,只需添加:

  .topnav.responsive::-webkit-scrollbar {
    width: 0px;  /* remove scrollbar space */
    background: transparent;  /* optional: just make scrollbar invisible */
  }

 function myFunction() { var x = document.getElementById("myTopnav"); if (x.className === "topnav") { x.className += " responsive"; } else { x.className = "topnav"; } } 
 body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .outer { position: fixed; top: 0px; widtH: 100%; } .topnav { overflow: hidden; background-color: #333; } .topnav a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .topnav a:hover { background-color: #ddd; color: black; } .active { background-color: #4CAF50; color: white; } .topnav .icon { display: none; } @media screen and (max-width: 600px) { .topnav a:not(:first-child) {display: none;} .topnav a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .topnav.responsive { position: relative; max-height: 75vh; /* Set to the hegith you want to use */ overflow-y: scroll; /* Enable scroll */ } .topnav.responsive .icon { position: absolute; right: 0; top: 0; } .topnav.responsive a { float: none; display: block; text-align: left; } } .secondnav { background:red; width:100%; height:42px; } 
 <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <div class="outer"> <div class="topnav" id="myTopnav"> <a href="#home" class="active">Home</a> <a href="#news">News</a> <a href="#contact">Contact</a> <a href="#about">About</a> <a href="#help">Help</a> <a href="#terms">Terms</a> <a href="#privacy">Privacy</a> <a href="#cookies">Cookies</a> <a href="#activitylog">Activity Log</a> <a href="#events">Events</a> <a href="#settings">Settings</a> <a href="#notifications">Notifications</a> <a href="#friendrequest">Friend Requests</a> <a href="javascript:void(0);" class="icon" onclick="myFunction()"> <i class="fa fa-bars"></i> </a> </div> <div class="secondnav"> </div> </div> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM