简体   繁体   English

如何使用粘性导航栏格式化下拉菜单(无 jQuery)

[英]How to format drop down menu with a sticky nav bar (no jQuery)

I'm trying to build a personal website with a sticky nav bar that also has a drop down menu under some of the items.我正在尝试建立一个带有粘性导航栏的个人网站,该网站在某些项目下还有一个下拉菜单。 The nav bar is "sticking" but the drop down menu won't appear.导航栏“粘住”,但不会出现下拉菜单。 I have found that the issues revolve around setting the position of drop down content to absolute and the overflow of the the ul that creates the "nav bar" to hidden.我发现问题围绕将下拉内容的位置设置为绝对位置以及将“导航栏”创建为隐藏的 ul 溢出。 (PS I'm relatively new to CSS, so I apologize ahead of time). (PS 我对 CSS 比较陌生,所以我提前道歉)。

So far, I have tried changing the position of the drop down content to "fixed," but this only works properly until you scroll down.到目前为止,我已经尝试将下拉内容的位置更改为“固定”,但这只能在您向下滚动之前正常工作。 Once you scroll down (as aspected) it remains the same distance down and does not adjust to the nav bar's new location.一旦你向下滚动(按方面),它会保持相同的向下距离,并且不会调整到导航栏的新位置。 In addition, I have changed the nav bar's overflow to visible.此外,我已将导航栏的溢出更改为可见。 The drop down works correctly, but the formatting of the nav bar is completely messed up.下拉菜单正常工作,但导航栏的格式完全混乱。 Essentially, all of these changes have made the problem "better," but only creates new issues with them.从本质上讲,所有这些变化都使问题“更好”,但只会产生新的问题。

 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; position: -webkit-sticky; /* Safari */ position: sticky; width: 100%; top: 0px; } li { float: left; } li a, .dropbtn { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover, .dropdown:hover .dropbtn { background-color: rgb(228, 40, 40); } li.dropdown { display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; }
 <header> <h1><a id="myNameTop" href="/">JK</a></h1> </header> <ul> <li class="nav-item"><a class="nav-link" href="/resume">Resumé</a></li> <li class="dropdown nav-item"> <a class="dropbtn" href="/code">Code</a> <div class="dropdown-content"> <a href="/gitHub">GitHub</a> <a href="/javaScriptProjects">Embeded JavaScript Projects</a> </div> </li> <li class="nav-item"><a class="nav-link" href="/graphicDesign">Graphic Design</a></li> <li class="nav-item"><a class="nav-link" href="/contact">Contact</a></li> </ul>

Currently, I hover over the nav bar item and the drop down item does not come down at all (no matter if I scrolled or not).目前,我将鼠标悬停在导航栏项目上,并且下拉项目根本没有下降(无论我是否滚动)。

I want to see the drop down menu when I hover.我想在悬停时看到下拉菜单。

The problem is you're hiding the overflow of ul .问题是你隐藏了uloverflow

You added overflow: hidden to the <ul> so that its background extended under all it's <li> s, because the <li> s have float:left , for no particular reason.你添加了overflow: hidden<ul>以便它的背景扩展到所有它的<li> s下,因为<li> s有float:left ,没有特别的原因。

I suggest "floating" the <li> s using either display:inline-block on them or display:flex on their parent.我建议在<li>使用display:inline-block或在其父级上使用display:flex来“浮动”。 And, obviously, removing the offending overflow:hidden .而且,显然,删除有问题的overflow:hidden

 ul { list-style-type: none; margin: 0; padding: 0; /* overflow: hidden; */ background-color: #333; position: -webkit-sticky; /* Safari */ position: sticky; width: 100%; top: 0px; } li { /* float: left; */ display: inline-block; } li a, .dropbtn { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover, .dropdown:hover .dropbtn { background-color: rgb(228, 40, 40); } li.dropdown { display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #f1f1f1 } .dropdown:hover .dropdown-content { display: block; } body { margin: 0; min-height: 200vh; }
 <header> <h1><a id="myNameTop" href="/">JK</a></h1> </header> <ul> <li class="nav-item"><a class="nav-link" href="/resume">Resumé</a></li> <li class="dropdown nav-item"> <a class="dropbtn" href="/code">Code</a> <div class="dropdown-content"> <a href="/gitHub">GitHub</a> <a href="/javaScriptProjects">Embeded JavaScript Projects</a> </div> </li> <li class="nav-item"><a class="nav-link" href="/graphicDesign">Graphic Design</a></li> <li class="nav-item"><a class="nav-link" href="/contact">Contact</a></li> </ul>

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

相关问题 如何在HTML / PHP中为导航栏创建下拉菜单? - How to create a drop down menu for a nav bar in HTML/PHP? 如何隐藏` <li> `桌面视图的导航栏中的下拉菜单? - How to hide `<li>` drop down menu in Nav bar in Desktop view? 下拉菜单与我的导航栏重叠 - Drop Down Menu is Overlapping my Nav Bar 我的下拉菜单没有下拉(它出现在侧面)以及如何粘在栏上 - My drop down menu isn't dropping down (its appearing sideways) & how to sticky the bar 如何使下拉菜单具有粘性 - How to make drop down menu sticky 如何在导航栏旁边添加向下箭头图标并使页面下拉菜单? - How can I add an arrow down icon next to nav bar and make pages drop down menu? jQuery UI菜单:是否可以使用多个作为CSS顶栏导航的下拉和弹出实例? - jquery ui menu: is it possible to use several as drop down and flyout instances of a css top bar nav? 如何为导航栏文本的下拉菜单文本和中心下拉菜单文本重复下划线CSS? - How can I repeat the underline CSS for my nav bar text for my drop down menu text and centre drop down menu text? 如何更改下拉菜单文本和导航栏下拉菜单背景颜色不透明度的文本位置? - How can I change the text position of drop down menu text & nav bar drop down menu background colour opacity? 如何在CSS中对齐导航下拉菜单? - How to align nav drop down menu in css?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM