简体   繁体   English

我如何在导航栏中获取“下拉”菜单以将其下拉到正确的位置?

[英]How do i get the “dropdown” menus in a nav bar to drop down in the right spot?

I'm creating a vertical navigation bar, which has sub menus pop up to the right on mouseover. 我正在创建一个垂直导航栏,在鼠标悬停时其子菜单会在右侧弹出。

The only problem is when i mouseover the categories, no matter whether they are positioned near the top or bottom of the navigation bar, their sub category bar appears up to the right of the top of the nav bar. 唯一的问题是,当我将鼠标悬停在类别上时,无论它们位于导航栏顶部还是底部附近,其子类别栏都显示在导航栏顶部的右侧。

I did some reading on how to make these, and supposedly im supposed to set positioning to absolute, and then do an offset from left. 我读了一些有关如何制作这些书的内容,据推测,我应该将定位设置为绝对,然后从左偏移。 This is my css code for the submenus: 这是我的子菜单的CSS代码:

#sidenav2 li ul
{
position:absolute;
left:200px;
Top:0px;
display:none;
}

So i figured that it might have something to do with the top setting, and got rid of that. 因此,我认为这可能与最高设置有关,因此摆脱了这一设置。 To my surprise, it almost worked. 令我惊讶的是,它几乎奏效了。 Each submenu was popping up at a different spot corresponding to the element hovered over. 每个子菜单都在与悬停元素相对应的不同位置弹出。 The only problem was that they were popping up about 10 pixels too low. 唯一的问题是它们弹出的像素太低约10像素。 Not really understanding how the css rules applied, i figured maybe i could apply a small bottom offset to make each submenu go up a little bit, but then they all started popping up near the bottom. 我不太了解css规则是如何应用的,我想也许我可以应用一个小的底部偏移量来使每个子菜单稍微升高一点,但是然后它们都开始在底部附近弹出。

I dont understand how to fix this, and to make things worse, im not sure how to keep up with this seemingly arbitrary results, or the rules that determine them. 我不知道如何解决这个问题,并使情况更糟,我不确定如何跟上这些看似武断的结果或确定这些结果的规则。

If it makes any difference, my menu items and sub menus arent written in static html, theyre the result of a mysql query which puts each of every category's children inside an unordered list within that item's list. 如果有什么不同,我的菜单项和子菜单都是用静态html编写的,它们是mysql查询的结果,该查询将每个类别的子级中的每个子级放在该项列表中的无序列表内。

Here is my other pertinent css code. 这是我其他相关的CSS代码。 The problem may very well be somewhere here. 问题很可能就在这里。

#sidenav2
{
float:left;
width:200px;
border: solid black 1px;
height:1000px;
background-color:red;
margin-top:-10px;

}

#sidenav2 ul
{
position: relative;
list-style-type:none;
border:0px;
padding:0px;
background-color:red;
}

#sidenav2 ul li
{
display:block;
padding:5px;
color:white;
cursor:pointer;

}

Any help or insight would be greatly appreciated! 任何帮助或见识将不胜感激!

您必须给出position: relative对于您的li

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

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