Subscribe Via RSS

3910 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 Stars174 Votes, Rating: 4.29
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

413 Responses to “Sliding Jquery Menu”

  1. tarife says:

    Thank you for that post! Very useful!

  2. thanks for the such a nice post.

  3. it is the most informative post to creat sliding menu button using jquery. Very much technical and informative post.

  4. It is outstanding article. it is very useful and informative. i was searching this information. thanks for posting

  5. Idefix says:

    Thanks !!! I was just lookiung for something like that !

  6. noob says:

    thanks for sharing

  7. Hello there! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Thank you!

  8. Marco says:

    Hey man, thanks for a great tutorial.

    im wondering how i would edit the js for the sliding menu to activate on a hover. i tried replacing click with hover, and it works, but im guessing i have to bind ‘the_menu’ somehow?

    thanks

  9. Can i protect my blog content copyright without registering it to copyright departmen? please help?

  10. Aaron says:

    Yes, you can copyright your blog without registering with the copyright department.

  11. Muzaffar says:

    this ans isn’t working properly check and then applied it.

  12. rocky says:

    thanx…learn alot

  13. robert says:

    thank you for this, a great tutorial

  14. Jaco says:

    Perfect thanks! Just what I was looking for: simple, uncomplicated and elegant.

  15. masterb says:

    Thanks, works great!

  16. H.D. says:

    This is a great tutorial, so simple and easy and makes a great effect! Thanks a lot! Great menu, great work – helpful post.

  17. Victor says:

    how I can have more than one item?
    thnx

  18. Yordan says:

    I like this menu. I will use it in my site. Nice work and thanks.

  19. Mann says:

    This one is great dude.keep it up posting such nice tuts.

  20. excellent menu!

  21. Hello there, just was alert to your weblog via Google, and located that it is really informative. I?m gonna watch out for brussels. I?ll appreciate in the event you continue this in future. A lot of people will be benefited from your writing. Cheers!

  22. Your blog is impressive,i like reading it. Thank you for your information,

  23. Furtano says:

    Thanks a lot! Works well!

  24. Zafer says:

    Thanks, it’s great menu…

  25. a11D says:

    Nice menu!

    Had one problem though:

    I changed position to absolute to stop it pushing my content down, which worked, but it only shows one of my menu list items(Contact Us) instead of the other four that I have?

    Any advice would be appreciated.

  26. Thanks for the menu, I think I will be able to reuse it for one my by sites. It looks great!

  27. abhim says:

    haturnuhun pisan….rahayu

  28. Sandeep says:

    Thanks, I was needed for drop down over, but it will also work. ;)

  29. mikki says:

    so how do you add more than one button? i tried but everytime i click
    the first one, the image i use for the second one also moves…

  30. Nice post………………..

  31. jaffa says:

    Great thing.

  32. Hello, can you tell me if it’s possible to use multiple instances of this sliding menu on the same page? I’ve tried but have been unsuccessful so far.

    I hope just create new instances of .the_menu and changing the name would do it…

  33. Aaron says:

    Yes, if you change the names you can use this multiple times. Just make sure to edit the name everywhere it appears or you will get multiple menus activating at the same time.

  34. Arron, if that’s the case, can you see what I’m doing wrong with the following as only one instance of the menu is working for me?

    http://bit.ly/yhqI18

  35. I should have added the second menu is supposed to be on the service button. And apologies for spelling your name wrong Aaron. :s

  36. Aaron says:

    Richard, did you change the instance in the CSS also? You can not have even the same container or id associated with the second instance as the first without some slightly different jQuery. If you want to send us an email with your code we would be more than happy to have one of our jQuery experts take a look and see what was wrong with it.

  37. Clint says:

    Anybody had issues with the menu going behind the content… ?

  38. Figured it out! Didn’t realise I needed to create two instances within the slider.js file.

    Thanks guys. :D

Leave a Reply