Subscribe Via RSS

3865 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Sliding Jquery Menu

February 17th, 2009 in JQuery Tutorials by Richard Carpenter

Sliding Jquery Menu

1 Star2 Stars3 Stars4 Stars5 Stars175 Votes, Rating: 4.30
Loading ... Loading ...

Hi there welcome to another tutorial, in this tutorial il show you how to create a sliding menu button using jquery. You can see the effect in action over on the PSDtuts webpage in the top right hand corner.

Oh yes and before i forget you can download the source files for free using the button above.

When the button is clicked it rolls out a box full of links, when the button is clicked again it rolls back in. This can be done using jquery and in this tutorial il show you how to do it. Right lets get started, firstly lets get our button done out the way so we can then focus on the code. Open up photoshop and create a new document with your desired size of button, your button can be any size your wish. Im using a size of 182 x 32 pixels, double click your background layer and add a simple gradient overlay.

On the right hand side of your button add a little white arrow and a vertical divider. The divide consists of two colors #302f2f & #252525.

On the left side add your little icon and some text, i dont think i need to go into too much detail on the button design, as you’ll make your own to suit your own site. Heres how mine looks. (save your button image into your images folder)

Now for the exciting bit open up notepad and save a blank notepad document as styles.css, save the file into a folder called sliding menu on your desktop. Once saved close notepad, open up your folder “sliding menu” create two new folders one called js and another called images. Goto the main jquery webpage and download the jquery libary “jquery-1.3.1.min.js” rename the file to just “jquery” and stick it into the folder js. Open up dreamweaver and create a new HTML file save the file straight away inside your sliding menu folder. (filename for the HTML file doesnt matter call it what you like, sliding_menu.html might be a good idea.) Now your in dreamweaver click the code view tab.

The most import thing we need to do first is reference our javascript and css files we do this by typing this chunk of code within the “HEAD TAGS”.

<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/slider.js"></script>

You will notice there are 3 files we have referenced styles.css, jquery.js and a third slider.js (but i havent got a slider.js file i hear you cry.) We need to create that now, open up notepad and save a blank notepad document as slider.js, save it inside your js folder. Open up your slider.js file inside dreamweaver and type out the code below.

$(document).ready(function () {
    $('img.menu_class').click(function () {
	$('ul.the_menu').slideToggle('medium');
    });
});

Let me explain the code above the 1st line means when the document is loaded run the function in our case the sliding menu. The next line means once an image with a class of menu_class is clicked the menu will slide which brings me down to line 3. The menu will toggle down at a medium speed. You can change the speed if you wish from slow, medium or fast. The second and third line are important as they hold key elements refering to our CSS file which is img.menu_class and ul.the_menu. We havent wrote these yet but will do when we begin to write out our menu. Head over to your HTML in the code view so we can begin to write out our menu.

<img src="images/button.png" width="184" height="32" class="menu_class" />
<ul class="the_menu">
<li><a href="#">A Website #1</a></li>
<li><a href="#">A Website #2</a></li>
<li><a href="#">A Link #1</a></li>
<li><a href="#">A Link #2</a></li>
<li><a href="#">A Website #3</a></li>
<li><a href="#">A Website #4</a></li>
<li><a href="#">A Link #3</a></li>
<li><a href="#">A Link #4</a></li>
</ul>

The first bit of code you see is a simple image which is our button, we specify the width and height of our button we also give it a class. The class will be the images unique anchor point for the js file which we have already wrote. The class also lets us apply any styles via css using the class .menu_class. After that we have a simple unordered list. If we take alook at our menu in our browser this is how it looks.

Open up your CSS file in dreamweaver. Lets set a few style for the main part of our document.

body {
	font-family:Arial, Helvetica, sans-serif;
	font-size:12px;
	background-color: #333333;
}

Just some simple text and background styling is needed, set your font family and desired font size, ive also changed the background from white to a darkish grey color. The next bit of styling were going to add is for the ordered and unordered lists, were also going to apply a border to our button image, you may do your border in photoshop on the actual image but i find it best to add it using CSS as changing abit of code is easier then opening up photoshop to change it.

ul, li {
	margin:0;
	padding:0;
	list-style:none;
}

.menu_class {
	border:1px solid #1c1c1c;
}

The next bit of css styling refers to the menu that drops down once the button is clicked.

.the_menu {
	display:none;
	width:300px;
	border: 1px solid #1c1c1c;
}

In these styles you can change the width of the open menu, mine has a width of 300px but this can be what ever you like. Ive also given it a 1px border the same as our button. The next bit of styling refers to the background color of our rolled out navigation and also the text colors, sizes and hovers.

.the_menu li {
	background-color: #302f2f;
}

.the_menu li a {
	color:#FFFFFF;
	text-decoration:none;
	padding:10px;
	display:block;
}

.the_menu li a:hover {
	padding:10px;
	font-weight:bold;
	color: #F00880;
}

The_menu li is the color of the background when the navigation is rolled out, the text in the navigation links wont have any line underneath them as we have used text-decoration:none, we have also spaced out our links by adding 10px paddinig all the way around our links we also want to display the links in a block. The hover styles are pretty simple, padding the same as the last style, font weight bold = bold text and the color changed to a pinkish color. Thats it for the styles you menu should be ready to test. Heres mine.

Just a quick point on positioning, dont use div align to center or right align your button as the rollout will not be aligned up properly, if you want to align your menu button properly wrap it in its own div and position div how you see fit.

See what you can come up with, maybe add some icons to your list objects. Thanks for reading, dont forget to subscribe VIA rss and twitter.

About The Author

About The Author: Richard Carpenter

Hi im Richard Carpenter and im a freelance web and graphics designer from England. I am also a regular Blogger, Tutorial Writer, and owner of Photoshop Plus. You can follow me on twitter HERE. You may also view my portfolio HERE.

 

Richard Carpenter has written 364 posts.

Be Part Of The Community!

Become part of the hv-designs community.

Subscribe Via RSS or Follow Us On Twitter.

Subscribe Via RSS Follow Us On Twitter

416 Responses to “Sliding Jquery Menu”

  1. Faizal says:

    hye all..
    anyone knows how to integrate this thing in oracle apex??
    awesome menu,thanx..

  2. Chris says:

    Is there an easy way to have the slide-out section lay over the current content instead of pushing it down?

  3. jomjania says:

    Hi verybody just want to share my thoughts about the mouse exit..

    FROM THE CONTENT: I just add class “x” on the link see below

    A Website #1

    Here is the script:

    $(document).ready(function() {
    $(‘img.menu_class’).mouseover(function() {
    $(‘.the_menu’).slideDown(‘medium’);
    });
    $(‘.x’).click(function() {
    $(‘.the_menu’).hide(‘medium’);
    });
    });

  4. Corinne says:

    Hi there,

    Thank you for the very simple tutorial. I was able to get this to work nicely. Just one little tweak…

    The menu closes (toggles) toward the middle of my image. It isn’t sliding from top to bottom as your tutorial listed. Is there something that I need to do with my CSS?

    XHTML:

    Welcome to Harry’s Cigars

    HOME
    ABOUT
    CONTACT
    CIGARS

    ACCESSORIES
    LOUNGE

    CSS:

    #menuBox {
    width:725px;
    height:auto;
    position:relative;
    margin-left:auto;
    margin-right:auto;
    }

    ul#topnav {
    list-style:none;
    width:300px;
    height:auto;
    position:relative;
    top:1px;
    left:65px;
    margin-left:auto;
    margin-right:auto;
    color: #000;
    }

    ul#topnav li{
    display:inline;
    padding-left:20px;
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    color:#FFF;}

    a {text-decoration:none}

    #menu {
    width:717px;
    height:56px;
    background-image:url(images/brown_top.png);
    background-repeat:no-repeat;
    padding:0px;
    position:absolute;
    margin-left:auto;
    margin-right:auto;
    z-index:100;
    top:8px;
    }

    Jquery:

    $(document).ready(function () {
    $(‘#dropdown’).hide();
    });

    $(document).ready(function () {
    $(‘.cigars’).click(function () {
    $(‘#dropdown’).slideToggle(’1000′);
    });
    });

    Any help would be Much appreciated!

  5. Nedagarmo says:

    Muy buena información, gracias

  6. Web Design says:

    This is awesome
    nice jquery tutorial

  7. very nice!

    thanx.

  8. Antonia says:

    Is it also possible to expand and collaps more than one panel?
    Like:

    Cats
    cat1
    cat2
    Dogs
    dog1
    dog2

  9. KazikWeb says:

    Very good tutorial thanks!

  10. Jim says:

    Very nice work indeed. This is an easy to manage upgrade for any site! Thanks!!!

  11. prem says:

    thanks HV Designs

  12. KRW24 says:

    great tutorial, I’m fairly new to website making and this was really well explained- my menu looks great- thanks!

  13. Gian Miller says:

    Hi there, I am trying to get this code to work with multiple drop downs.

    My aim is to create a horizontal list of buttons across the top of a page, and to have a 100% width drop line bar appear when you click each of them.

    At the moment when i click either of them they both open up, even if i call the second one the_menu2 and add another function the the slider.js

    Anybody know what i can do to get this working?

  14. W3bdev says:

    Hey guys,

    I was able to get multiple menu’s working. Here’s how you do it: first, create a new set of class values for the menu’s you want. Just copy and paste the_menu classes in style, and append a digit, or whatever you want (I did menu2). once you have a new set, copy the original menu in the html file, and change the names of the class references… ie: & .

    Once you have this done, you must create a new function in slider.js. This is very simple, just copy and paste the first, and modify it to fit your class change… ie:
    $(‘img.menu2_class’).click(function() {
    $(‘ul.the_menu2′).slideToggle(‘medium’);
    });

    Enjoy :)
    -W3bdev

  15. max says:

    very nice work indeed. This is an easy to manage upgrade for any site!

  16. max glover says:

    thank you soooooo much finally a bit of code that everyone can understand… AMAZING!!!!

  17. Thank u for this code. very nice tutorial.

  18. parisa says:

    hi,i used your code for my web site.
    i was searching for a sliding ,vertical, multi level menu.if u have a source or usefull links i could use please let me know:)tnx dude

  19. michel says:

    necessary that instead of the menu to open in the vertical line it opens in the horizontal line. somebody has some idea?

  20. Eddie says:

    Great man, thnks a lot!…..

  21. apple says:

    looks very great and fine thanks

  22. apple says:

    very creative tutorial thanks

  23. Thank you very nice Tutorial. I will iclude this in my hp.

    Greeds

  24. nurkan says:

    thanks for that great tutorial. but i need automatic open version. dont need click for menu.

  25. Senthil Kumar says:

    Nice one. I liked it. But the menu doesn’t close itself when clicked outside the menu. I can add it myself. No probs. It would be nice if you do add that.

  26. Great job. Thanks a lot.

  27. Thanx. Really nice work.

  28. Thank you for sharing your work.

  29. NEED HELP!!

    This is a great menu but I am trying to get it to drop down on mouse over instead of having to click.

    Can someone help?

    Thanks

  30. Jquery has some interesting features and catchy things to implement in a website . PS: DREAMWORKS ROCKS :D i am using it too and for sure helped me and made my job easier.

  31. G says:

    Very nice menu, thank you author, but I’m having a problem.

    I’ve tried everyone’s “roll-over” mouse code that was posted here and with all of them it is the same. I move the mouse anywhere outside of the main button image, the link list goes away and I cannot choose anything. It is a shame because the way I am using the buttons it is really dumb to have to click on them to get the list to appear.

  32. Vernon says:

    Thank you for the tutorial! It was relatively easy to understand, this coming from someone far from an expert.

    The menu acts differently between IE and Chrome. In IE, the menu scrolls the rest of the page and DIV downwards with the menu. In Chrome, when the menu scrolls down, the background color of the menu takes on the color of the DIV.

    Here is the page and the problem. Any help would be greatly appreciated: http://www.vernonmullen.com/third.html

  33. jestin says:

    thankssssssss

  34. Puneet says:

    Thanks for sharing this. Very helpful

  35. Harry says:

    Uncompatible with jquery.1.4.2

  36. Really nice menu!! Thanks!

    Michael K.
    Dev. @ Unihost Brasil

  37. thanks thanks thanks!!!!!

  38. Andy says:

    hi, great tutorial, can you tell me how, i would incorporate more than 1 button please? when i add more than one, because i haven’t changed the setting, all the drop down appear/disappear with a click….. really hope you can help, pleas email me if you can!!

  39. Andy says:

    ah ok i done it…but how do i add the java scipt so when i click on button the other one closes!

    heres the slider.js..

    $(document).ready(function () {
    $(‘img.menu_class1′).click(function () {
    $(‘ul.the_menu1′).slideToggle(‘medium’);
    });
    });

    $(document).ready(function () {
    $(‘img.menu_class2′).click(function () {
    $(‘ul.the_menu2′).slideToggle(‘medium’);
    });
    });

    $(document).ready(function () {
    $(‘img.menu_class3′).click(function () {
    $(‘ul.the_menu3′).slideToggle(‘medium’);
    });
    });

    $(document).ready(function () {
    $(‘img.menu_class4′).click(function () {
    $(‘ul.the_menu4′).slideToggle(‘medium’);
    });
    });

    $(document).ready(function () {
    $(‘img.menu_class5′).click(function () {
    $(‘ul.the_menu5′).slideToggle(‘medium’);
    });
    });

  40. Andy says:

    hey, great tutorial! how would i get the drop down to work for multiple buttons? im not sure i can create the javascript to close one when another is open….really hope anyone can help!

  41. Justin says:

    Anyone having troubles with making your menu open/close automatically just simply replace your code with this: $(document).ready(function () {
    $(‘img.menu_class’).hover(function () {
    $(‘ul.the_menu’).slideToggle(‘fast’);
    });
    }); What I need is to get it to slide up and not down. using slideUp instead of slideToggle isn’t working for some reason. Any help on that?

  42. Bob says:

    I hate to be the only one to say this but this is NOT a sliding menu. It is a reveal! A sliding menu would SLIDE the entire content and not just reveal it. An example of a sliding menu would be: http://www.timhortons.com/ca/en/ which actually reveals the menu on the first hover and THEN slides the menus up and down. Also this site has SLIDING menus: http://www.componentart.com/products/aspnetajax/ for example.

    I hope you all can see the difference between a true sliding menu and a reveal.

  43. ausweisapp says:

    Thanks a lot! Great menu, great work – helpful post.
    Best regards from Germany, Alex

  44. Jan says:

    What is the events and the proprieties?

  45. Thank you. That’s great tutorial.

  46. Mark says:

    Code is alright for a single instance of a menu, although is of little use if you want more than one.

  47. Dean Karasinski says:

    This is a great tutorial, so simple and easy and makes a great effect! Thanks for posting this and giving such clear instructions!

  48. Thanks. great tut. :D

  49. Bob says:

    Thank you very much! Great this menu looks’s very very nice. I will try do it!
    Cheers!

  50. Bill says:

    Could not find a demo of this functionality. Tried searching for the PSDtuts website but no luck.

  51. Hunny says:

    OMG it worked!! thanks so much. I literally went step by step and finally got it to work, so now I’m working on other buttons. This is a class assignment and so grateful for this site.

  52. berlin says:

    Nice one. I liked it. But the menu doesn’t close itself when clicked outside the menu. I can add it myself. No probs. It would be nice if you do add that.

  53. Aaron says:

    You can find a demo of this on the upper right hand side of the Tuts+ Network.

  54. gamezat says:

    it’s rally nice menu i love it so much
    i wonder if you can make it support mulit sub menus
    thank you

  55. Tim says:

    Hello,

    This was working perfectly in IE. Now that IE9 has been released it has stopped working in that version. Does anyone know of a fix?

    Thanks,

    Tim

  56. Fixed it!!! says:

    Hey guysss I just finished fixing the Z-index problem many people have been having!!!!
    What you need to do is put the content in two div tags, the outer div tag is positiond where you want the content to be then the inner div tag has
    .contenti /*(inner div tag)*/
    {
    margin-top:5px;
    width: 1000px;
    height: 100%;
    position: absolute;
    z-index: -5;
    }

  57. Yogesh says:

    “Thank you”

  58. Plakate says:

    Thanks a lot for this tutorial.
    Nice work, it works great.

  59. Roberto says:

    Richard good job,

    Let me add a bit of code but that the menu closes when you remove the pointer, this would go into slider.js

    $(‘ul, .the_menu’).mouseleave(function () {
    $(‘ul, .the_menu’).hide(‘medium’);

  60. thank you very nice work indeed. this is an easy to manage upgrade for any site!

  61. NEED HELP!!

    This is a great menu but I am trying to get it to drop down on mouse over instead of having to click.

    Can someone help?

    Thanks

  62. Aaron says:

    You just need to change the javascript file to:

    $(document).ready(function () {
    $(‘img.menu_class’).mouseover(function () {
    $(‘ul.the_menu’).slideToggle(‘medium’);
    });
    });

    You are removing the “click” instance and replacing it with the “mouseover” instance. Hope that helps

  63. Great tutorial thanks!
    That should make it possible for google to see the links and STILL allow us to use a drop-down money :)

    Thanks guys!

  64. Ben 10 says:

    Thank you very much is a large menu using a great technology.

  65. Blanc says:

    This tut is great :) I’m new to Jqeury, does anyone have a suggestion on how to get multiple buttons for a horizontal nav???

  66. Caleb says:

    To overlay everything else, on .the_menu change the z-index to 100.

    this was my final jQuery mash up:

    $(document).ready(function () {
    $(‘img.menu_class’).mouseenter(function () {

    $(‘ul.the_menu’).slideToggle(‘slow’);

    });

    $(‘ul.the_menu’).mouseleave(function () {

    $(‘ul.the_menu’).hide(‘slow’);
    });

    });

  67. Sehr viele Infos, habe vieles gelernt… Weiter so, freue mich auf neue Nachrichten.

  68. Schungit says:

    Very good informations. I learn for my work. Thank you

  69. Irina says:

    Endlich habe ich das gefunden,, wonach ich gesucht habe….

    Vielen Dank

  70. Dave says:

    As others have said this is not a hover dropdown menu.

    any chance of doing a tut on creating an onhover dropdown menu (as it is the standard and basically NOBODY wants an onclick dropdown menu seriously)?

  71. Aaron says:

    We have a hover menu tutorial coming out Monday of next week! Follow us on twitter because we will release a preview this weekend!

  72. Tom says:

    Does anyone know how to make this work with buttons like these? http://ballsoutmotors.com/test/index.php
    A friend and I have tried to figure it out and can’t. Yes I know the way I did the page is outdated as my friend pointed out, and will hopefully catch up in time. Was hoping to use this dropdown for the Vehicles, parts, and gear buttons.

  73. tuba says:

    Nice work mate, perfect to dropdown countries list, thanks man!

  74. This is a great menu but I am trying to get it to drop down on mouse over instead of having to click.

  75. sniperspy says:

    Excellent simple and easy to understand, this tutorial helped me so much. Thanks a lot!

Leave a Reply