【发布时间】:2018-12-07 15:31:07
【问题描述】:
现在导航在全屏时不会在本示例中显示,但在 iPad 模式下在 Chrome 上时它可以正确显示,所以对于我的目的,这将起作用。
我之前完全制作了一个从顶部导航中弹出的子菜单,但出于什么原因让我难住了。
首先我想得很好,呵呵!你需要改hoverableMenu{position:absolute};
和hoverable{position:relative};
和中提琴我就完了。但是,无论我做什么,当我有float:left; 时,它都会一直向左移动,即使我想,我也可以使用边距对位置进行硬编码,但这不是一个优雅的解决方案,我知道老实说它是只是在我头上飞过的东西。
感谢所有帮助。
以下是我的最小可验证示例。
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
body{
margin: 0px;
background-color: lightblue;
height: 100%;
}
header{
/*This is for the header picture background*/
background-size: 100%;
background-image: url(resources/chem3D.png);
background-repeat: no-repeat;
/* vh = viewheight. Used in place of percentages when percentages don't seem to work. This is basically 30% but not really
https://stackoverflow.com/questions/31039979/css-units-what-is-the-difference-between-vh-vw-and */
min-height: 100%;
}
/*Seperate the header from the content*/
#seperator{
height: 10px;
background-color: black;
margin:0;
padding:0;
}
/*THe main content of the site. Where the Flex magic happens.*/
#content{
display: flex;
margin: auto;
height: auto;
}
/*Navigation Stuffs*/
nav{
width: 200px;
}
.topnav{
overflow: hidden;
}
/*style of links in nav*/
/* ADDED FLOAT LEFT */
.topnav a{
float: left;
display: block;
color: black;
text-align: center;
padding: 10px 5px 10px 15px;
text-decoration: none;
font-size: 1.2em;
letter-spacing: 0.1em;
}
/* Drop down menu */
a.hoverable:focus + div.hoverableMenu{
display: block;
float: left;
}
a.hoverable:hover{
color:black;
}
div.hoverableMenu{
display: none;
width: 70%;
margin-left:auto;
margin-right:10px;
}
div.hoverableMenu>a{
letter-spacing: 0em;
}
div.hoverableMenu:focus{
display: block;
}
/*Mobile views*/
/*Tablet View*/
@media screen and (max-width: 900px){
#seperator{
display: none;
}
#content{
height: 100%;
display:flex;
flex-wrap: wrap;
}
nav{
width: 100%;
flex-basis: 100%;
}
.topnav a{
float: left;
}
main{
width: 50%;
}
header{
display:none;
}
}
<div id="seperator"></div>
<div id="content">
<nav>
<div class="topnav" id="myTopnav">
<a href="index.html">Home</a>
<a href="teaching.html">Teaching</a>
<a class="hoverable" href="#">Research <i class="fa fa-caret-down"></i></a>
<div class="hoverableMenu">
<a href="research.html">Overview</a>
<a href="publications.html">Publications</a>
</div>
<a class="hoverable" href="#">Students <i class="fa fa-caret-down"></i></a>
<div class="hoverableMenu">
<a href="studentawards.html">Awards</a>
<a href="gallery.html">Gallery</a>
</div>
<a href="contact.html">Contact</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
</nav>
</div>
我访问的资源:
https://jsfiddle.net/davidpauljunior/pdcAF/1/
How to make sub menu float over div?
Can't click on menu links in drop down menu
编辑
在我的媒体查询下,我更改了以下内容。
div.hoverable{
display: relative;
}
div.hoverableMenu{
float:right;
display: absolute;
}
div.hoverable:focus > div.hoverableMenu{
top:1em;
left: 140px;
z-index: 99;
}
执行此操作后,菜单现在相对于其父级弹出,但它们导致它们右侧的元素环绕到其余中殿项目的下方。而不是浮动在导航上的子菜单。 :(
【问题讨论】:
-
这个link可以帮助你
-
感谢您的回复。如果我将 [a.hoverable:focus + div.hoverableMenu] 更改为 [a.hoverable:focus > div.hoverableMenu] 我的菜单根本不会激活。可能是我误会了?
-
我改变了 div.hoverable{display: relative;} 和 div.hoverableMenu{display:absolute;}。在 a.hoverable:focus + div.hoverableMenu{} 中设置顶部和左侧也没有任何变化。
-
我什至添加了一条规则 div.hoverable:focus > div.hoverableMenu{top: 1em; left 140px;} 试图获得链接中描述的父元素的相对定位,但我仍然没有得到想要的结果。