下拉菜单 - jQuery/CSS

Posted

技术标签:

【中文标题】下拉菜单 - jQuery/CSS【英文标题】:Drop Down Menu - jQuery/CSS 【发布时间】:2011-11-16 18:42:03 【问题描述】:

我正在做的下拉菜单有问题。请查看此截图:http://img215.imageshack.us/img215/8449/hovermenu.png

这是html代码:

 <ul class="topnav">
    <li><a href="#">Home</a></li>
    <li>
        <a href="#" class="subnavkey">Tutorials</a>
        <ul class="subnav">
            <li><a href="#">Sub Nav Link</a></li>
            <li><a href="#">Sub Nav Link</a></li>
        </ul>
    </li>
    <li>
        <a href="#" class="subnavkey">Resources</a>
        <ul class="subnav">
            <li><a href="#">Sub Nav Link</a></li>
            <li><a href="#">Sub Nav Link</a></li>
        </ul>
    </li>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Advertise</a></li>
    <li><a href="#">Submit</a></li>
    <li><a href="#">Contact Us</a></li>
</ul>

这里是 jQuery 代码:

$(document).ready(function()

    $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    $(".subnavkey").hover(function()  //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function() 
        , function()
            $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        );

        //Following events are applied to the trigger (Hover events for the trigger)
        ).hover(function() 
            $(this).addClass("subhover"); //On hover over, add class "subhover"
        , function()    //On Hover Out
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    );

);

这是 CSS:

ul.topnav   
    background: url(../images/topmenubg.png);
    background-repeat: repeat-x;
    border-radius:16px;
    border-color:#a5a7a8;
    list-style: none;
    float: right;    
    font-size: 1.2em;

    list-style-type:none;
    text-align: left;  
    padding:0px;
    margin-top:9px;       

ul.topnav li 
    float: left;
    margin: 0;             
    padding: 10px;
    position: relative; /*--Declare X and Y axis base for sub navigation--*/
    border-color:#D9D9D9;
    border-width:0px 1px 0px 0px;
    border-style:solid;
    display:block; 
    font-weight:500;
    color:#333;    
    height:14px;

ul.topnav li:last-child
    border-width:0px;

ul.topnav li span  /*--Drop down trigger styles--*/

    float: left;

ul.topnav li span.subhover background-position: center bottom; cursor: pointer; /*--Hover effect for trigger--*/
ul.topnav li ul.subnav 
    list-style: none;
    position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
    left: 0; top: 35px;
    background: #fff;
    margin: 0; padding: 0;
    display: none;
    float: left;
    width: 170px;
    border: 1px solid #111;

ul.topnav li ul.subnav li
    margin: 0; padding: 0;
    border-top: 1px solid #252525; /*--Create bevel effect--*/
    border-bottom: 1px solid #444; /*--Create bevel effect--*/
    clear: both;
    width: 170px;

html ul.topnav li ul.subnav li a 
    float: left;
    width: 145px;
    background: #fff;
    padding-left: 20px;

html ul.topnav li ul.subnav li a:hover  /*--Hover effect for subnav links--*/
    background: #fff;

正如您在屏幕截图中看到的,它只显示了“子菜单”/“下拉菜单”中第一个链接的一小部分。正如 HTML 代码所示,还有更多链接。

如何才能显示所有链接?

【问题讨论】:

【参考方案1】:

我不得不同意 simoncereska 的观点,你那里的花车比你真正需要的要多得多。我对您的代码进行了一些修改,以向您展示仅浮动主导航菜单可以做什么:

/* CSS RESET */
*  margin: 0; padding: 0; 

body  background: #19192b; 
.topnav 
    float: left;
    font-family: Verdana;
    font-size: 11px;
    margin: 9px;
    padding: 0;
    list-style-type: none; 
.topnav a  
    color: #333;
    text-align: center;
    text-decoration: none;
    height: 14px; /* 34 - 20 */
    padding: 10px 15px;
    cursor: pointer;
    display: block; 
/* First child */
.topnav > li 
    position: relative;
    background: #f0f0f0 url(../images/topmenubg.png) 0 0 repeat-x;
    float: left;
    border: 0 solid #d9d9d9;
    border-width: 0 0 0 1px;
    display: block; 
.topnav > li:hover  background: #d9d9d9; 
.topnav > li:first-child  
    -moz-border-radius: 16px 0 0 16px;
    -webkit-border-radius: 16px 0 0 16px;
    border-radius: 16px 0 0 16px;
    border-left: 0; 
.topnav > li:last-child  
    -moz-border-radius: 0 16px 16px 0;
    -webkit-border-radius: 0 16px 16px 0;
    border-radius: 0 16px 16px 0; 
.topnav > li:first-child a  padding-left: 20px; 
.topnav > li:last-child a  padding-right: 20px; 
.topnav li ul  
    position: absolute;
    top: 34px;
    width: 100%;
    display: none; 
.topnav li:hover ul  display: block; 
.topnav li ul li 
    background: #f0f0f0;
    font-size: 10px;
    padding: 1px 0 0;
    border-top: 1px solid #d9d9d9;
    display: block; 
.topnav li ul li:hover  background: #fff; 
.topnav li ul li:last-child 
    -moz-border-radius: 0 0 16px;
    -webkit-border-radius: 0 0 16px;
    border-radius: 0 0 16px 16px;
.topnav li ul li a 
    width: 100%;
    padding: 10px 0;
    height: auto; 
.topnav li ul li:last-child a  padding-bottom: 18px; 

这里有一个工作示例(没有 javascript):http://jsfiddle.net/MFmwJ/

【讨论】:

【参考方案2】:

使用 DROPPY 轻松制作下拉菜单。不需要 css 或额外的 javascript。

看这个例子:

<link rel="stylesheet" href="http://onehackoranother.com/projects/jquery/droppy/stylesheets/droppy.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"/></script>
<script src="http://onehackoranother.com/projects/jquery/droppy/javascripts/jquery.droppy.js"/></script>

<ul class="topnav">
<li><a href="#">Home</a></li>
<li>
    <a href="#" class="subnavkey">Tutorials</a>
    <ul class="subnav">
        <li><a href="#">Sub Nav Link</a></li>
        <li><a href="#">Sub Nav Link</a></li>
    </ul>
</li>
<li>
    <a href="#" class="subnavkey">Resources</a>
    <ul class="subnav">
        <li><a href="#">Sub Nav Link</a></li>
        <li><a href="#">Sub Nav Link</a></li>
    </ul>
</li>
<li><a href="#">About Us</a></li>
<li><a href="#">Advertise</a></li>
<li><a href="#">Submit</a></li>
<li><a href="#">Contact Us</a></li>

$(function () 
    $('.topnav').droppy();
);

http://jsfiddle.net/AtbK5/

滴滴:http://onehackoranother.com/projects/jquery/droppy/

【讨论】:

【参考方案3】:

我猜你的子菜单列表项(li)也是浮动的,清除它们。

【讨论】:

一切正常jsfiddle.net/simoncereska/FJ6Rb 在 safari 上测试。你用什么浏览器?再次检查 js 错误,其他样式。尝试删除 line-height, height ...【参考方案4】:

试试这个:

$(document).ready(function()
    $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    $(".subnavkey").hover(
        function()  //When trigger is clicked...
            //Following events are applied to the subnav itself (moving subnav up and down)
            $(this).addClass("subhover").parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
        
        , function()
            $(this).removeClass("subhover").parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        
    );
);

【讨论】:

以上是关于下拉菜单 - jQuery/CSS的主要内容,如果未能解决你的问题,请参考以下文章

下拉 Jquery/CSS 菜单在 iPad/iPhone 上没有响应

通用无限极下拉菜单

asp有两个下拉菜单,是二级联动菜单

在打开另一个下拉菜单之前完全关闭一个下拉菜单

下拉菜单中的 Bootstrap4 下拉菜单关闭父菜单

ASP+ACCESS 级联下拉菜单