Dropdown menu - jQuery / CSS

I have a problem with the drop down menu that I am doing. Check out this screenshot: 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 displays only a small part of the first link in the "submenu" / "drop-down menu". And, as the HTML code shows, there are more links.

How can I do this, it shows all the links?

+4
source share
4 answers

I need to agree with simoncereska, you have much more floats there than you really need. I reworked your code a bit to show you what you can do, only the floating main 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 (without javascript): http://jsfiddle.net/MFmwJ/

+2
source

Use DROPPY to easily create dropdown menus. No css or additional javascript 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/

+2
source

I assume that the items on the submenu list (li) also move, clear them.

0
source

try the following:

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

All Articles