[英]What is the proper way to position a CSS hover menu?
I put together a fiddle with a very basic hover example. 我把一个小提琴与一个非常基本的悬停示例放在一起。
http://jsfiddle.net/qhj2P/ http://jsfiddle.net/qhj2P/
Here is the CSS: 这是CSS:
.button
{
width: 50px;
height: 15px;
background-color: #090;
padding: 5px;
}
.menu
{
height: 200px;
width: 50px;
background-color: #900;
display: none;
}
I'm curious what the best way to position the menus is. 我很好奇放置菜单的最佳方法是什么。 How do I make it so that the first menu drops down right below the first button and the second menu drops down right below the second button? 如何使第一个菜单位于第一个按钮的正下方,而第二个菜单位于第二个按钮的正下方? I'm not so good at CSS positioning and the like. 我不太擅长CSS定位等。
Use a styled, unordered lists, not DIVs. 使用样式化的无序列表,而不要使用DIV。 Stick most of the styling on the A-tag with display:block
. 使用display:block
将大多数样式粘贴在A标签上。
<div class="menu">
<ul>
<li><a href="...">Menu Item</a>
<ul>
<li><a href="...">Sub Item</a></li>
<li><a href="...">Sub Item</a></li>
</ul>
</li>
<li><a href="...">Menu Item</a></li>
</ul>
</div>
CSS: CSS:
.menu ul, .menu li {
padding:0;
margin:0;
list-style-type:none
}
.menu li {
display:inline-block;
position:relative;
}
.menu a {
font-family:arial;
text-decoration:none;
display:block;
background-color:#c0c0c0;
padding:3px;
color:#000000
}
/* level two */
ul ul {
position:absolute;
display:none;
}
ul ul a {
background-color:#d0d0d0;
margin-top:3px;
}
The best way would be to use <ul>
instead of your current setup. 最好的方法是使用<ul>
而不是当前的设置。
http://jsfiddle.net/elclanrs/fHmvs/1/ http://jsfiddle.net/elclanrs/fHmvs/1/
<ul class="menu">
<li>
<a href="#">One</a>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
</ul>
</li>
<li>
<a href="#">Two</a>
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
</ul>
</li>
</ul>
css: CSS:
.menu li {
float: left;
position: relative;
}
.menu ul {
position: absolute;
display: none;
background: red;
}
.menu ul li { clear: both; }
.menu a {
display: block;
padding: .5em;
margin: .1em;
}
jq: jq:
$('.menu li').hover(function() {
$(this).children('ul').stop(1, 1).slideToggle();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.