[英]Special Drop-Down Menu
I am trying to achieve a simple drop-down menu with the following HTML structure. 我正在尝试使用以下HTML结构实现一个简单的下拉菜单。 This structure is mandatory (I think) as explained in the illustrations below.
如下图所示,此结构是强制性的(我认为)。
<nav role="navigation">
<ul id="main-menu" class="nav top-nav clearfix">
<li id="menu-item-1" class="menu-item"><a href="#">Menu 1</a></li>
<li id="menu-item-2" class="menu-item"><a href="#">Menu 2</a></li>
<li id="menu-item-3" class="menu-item"><a href="#">Menu 3</a></li>
</ul>
<ul id="sub-menu-1" class="sub-menu nav clearfix">
<li class="menu-item"><a href="#">Sub Menu 1.1</a></li>
<li class="menu-item"><a href="#">Sub Menu 1.2</a></li>
<li class="menu-item"><a href="#">Sub Menu 1.3/a></li>
</ul>
<ul id="sub-menu-2" class="sub-menu nav clearfix">
<li class="menu-item"><a href="#">Sub Menu 2.1</a></li>
<li class="menu-item"><a href="#">Sub Menu 2.2</a></li>
<li class="menu-item"><a href="#">Sub Menu 2.3/a></li>
</ul>
</nav>
To get a better idea of what I am trying to achieve I've made the following illustrations: 为了更好地了解我要实现的目标,我做了以下插图:
<ul><li><ul></ul></li></ul>
would do. <ul><li><ul></ul></li></ul>
一样。 As you can see the submenu has to scale to 100% of the container and has all the items arranged in center. I think the best approach is with javascript (not sure you can do this with only CSS), but I am kind off stuck. 我认为最好的方法是使用javascript(不确定您是否只能使用CSS来做到这一点),但我有点坚持。 The sub menu appears on main menu item hover, but as soon as I hover out into the sub menu in order to navigate, the sub menu disappears.
子菜单出现在主菜单项的悬停上,但是当我将鼠标悬停到子菜单以进行导航时,该子菜单就会消失。 Anyway, this is the javascript:
无论如何,这是javascript:
$('nav #main-menu .menu-item a').hover(
function() {
var id = $(this).parent().attr('id');
id = id.substr(id.length - 1);
submenu = $('#sub-menu-' + id);
submenu.show();
},
function() {
var id = $(this).parent().attr('id');
id = id.substr(id.length - 1);
submenu = $('#sub-menu-' + id);
submenu.hide();
}
);
I am pretty sure that there is a better way to do this. 我很确定有更好的方法可以做到这一点。
I've also set up a FIDDLE for better understanding. 我还设置了FIDDLE以便更好地理解。
//show sub menu when we hover over an item
$('nav #main-menu > .menu-item')
.on('mouseenter', function() {
$('.sub-menu').hide();
var id = $(this).attr('id');
id = id.substr(id.length - 1);
$('#sub-menu-' + id).show();
});
//hide submenu when the mouse goes away
$('nav').on('mouseleave', function() { $('.sub-menu').hide(); });
Modified your fiddle here: http://jsfiddle.net/3z8MR/10/ 在这里修改了小提琴: http : //jsfiddle.net/3z8MR/10/
Edit Add this line to conform to your specs in the comments 编辑添加此行以符合您的评论中的规范
$('.sub-menu').on('mouseleave', function() { $(this).hide(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.