简体   繁体   English

将导航栏设置为“固定”时,下拉菜单在导航栏中无法正常工作。

[英]Dropdown menu not working properly in navigation bar when said navigation bar is set to position:fixed;

I have designed a navigation bar with four options, whereupon while one of these options are hovered, a dropdown menu appears. 我设计了一个带有四个选项的导航栏,当这些选项之一悬停时,出现一个下拉菜单。 The dropdown menu functions properly while the navigation bar is not set to position:fixed; 导航栏未设置为position:fixed;时,下拉菜单正常运行。 but when it is, the dropdown menu covers the option/link that activates it. 但如果是,则下拉菜单包含激活它的选项/链接。

Here I have linked a Jsfiddle: https://jsfiddle.net/rqhenq4a/ 在这里我链接了一个Jsfiddle: https ://jsfiddle.net/rqhenq4a/

I have implemented the fixed navigation bar with the following lines of code: 我使用以下代码行实现了固定的导航栏:

body {
padding-top:49px
}

(To avoid the navigation bar to overlap underlying content, the navigation bar happens to have a height of exactly 49px) (为避免导航栏与基础内容重叠,导航栏的高度恰好为49px)

ul {
position:fixed;
width:100%;
top:0;
z-index:1;
}

The first line of code to make the navigation bar fixed, the second line of code to make the navigation bar cover the whole viewport, the third line of code to let it be on top of the viewport at all times (I think), and the last line of code to avoid the navigation bar to inherit opacity from an underlying image. 第一行代码使导航栏固定,第二行代码使导航栏覆盖整个视口,第三行代码使它始终位于视口顶部(我认为),并且最后一行代码,以避免导航栏从基础图像继承不透明度。

The desired result would be a fixed navigation bar where the "Produkter" option would not be covered by the options of the dropdown menu. 所需的结果将是一个固定的导航栏,其中下拉菜单的选项不会覆盖“ Produkter”选项。

is this what you are looking for jsfiddle.net 这是您在寻找jsfiddle.net的东西吗

   ul li ul#dropdowncontent {
    min-width: 100%;
    display: none;
    position: absolute;
    z-index: 999;
    left: 0;
    width: 90px;
    top: 100%;
}

Just add top:100%; 只需添加top:100%;

you are just missing that offset within top. 您只是缺少顶部的偏移量。 By setting position to absolute, you have default top offset 0. Here is your edited code, fix is on line 46. 通过将position设置为absolute,您具有默认的顶部偏移0。这是您编辑的代码,修复位于46行。

https://jsfiddle.net/rqhenq4a/3/ https://jsfiddle.net/rqhenq4a/3/

Code example: 代码示例:

ul li ul#dropdowncontent {
min-width:100%;
display:none;
position:absolute;
z-index:999;
left:0;
top:49px;       /*This line here <---------- */
width:90px;
}

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

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