简体   繁体   English

特殊下拉菜单

[英]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: 为了更好地了解我要实现的目标,我做了以下插图:

  1. Simple menu with items arranged with inline blocks. 简单的菜单,内嵌块排列的项目。 As you can see, the menu scales to 100% of the container and has all the items arranged in center. 如您所见,菜单缩放到容器的100%,并且所有项目都排列在中间。

这是鼠标不悬停任何菜单项时的菜单外观

  1. When hovering over a menu item which has a submenu. 将鼠标悬停在具有子菜单的菜单项上时。 In the illustration that's Menu 1 which has Sub Menu 1 and it needs to display it on mouse hover, like a simple <ul><li><ul></ul></li></ul> would do. 在图示中,菜单1具有子菜单1,它需要在鼠标悬停时显示它,就像简单的<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. 如您所见,子菜单必须缩放到容器的100%,并且所有项目都排列在中间。

将鼠标悬停在带有子菜单的一项上时

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.

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