Drop Down Menu - jQuery/CSS

2019-06-23 17:56发布

I have a problem with a dropdown menu I am doing. Please check this screenshot out: http://img215.imageshack.us/img215/8449/hovermenu.png

This is the html code:

 <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>

And here is the jQuery code:

$(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"
    });

});

Here is the 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;
}

As you can see in the screenshot, it only shows a little part of the first link in the "sub-menu"/"dropdown-menu". And as the HTML code shows, there are more links.

How can I do so it shows all the links?

4条回答
乱世女痞
2楼-- · 2019-06-23 18:21

I guess you sub menu list items (li) also are floated, clear them.

查看更多
混吃等死
3楼-- · 2019-06-23 18:27

I'm going to have to agree with simoncereska, you have far more floats in there than you truly need. I reworked your code a little to show you what you could do only floating the primary navigation menu:

/* 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; }

Here's a fiddle with a working example (no javascript): http://jsfiddle.net/MFmwJ/

查看更多
家丑人穷心不美
4楼-- · 2019-06-23 18:30

try this:

$(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
        }
    );
});
查看更多
Animai°情兽
5楼-- · 2019-06-23 18:34

Use DROPPY to make dropdown menus easily. No css or extra javascript is required.

See this example:

<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/

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

查看更多
登录 后发表回答