Vertical Pulldown Menu
14,972 ViewsJavascript Tutorials March 7th, 2007
Create a vertical pulldown script that displays navigational headers and when you click on a header new links appear underneath it, this effect works both in Firefox and Internet Explorer.
We will be doing all of our code in just 1 file to make this easier, later on you can split it all up into different files for your applications but for the ease of this tutorial everything will be in the same file.
This is a great effect for websites that have limited room and want to clean up there navigation a bit. It also provides visitors to your website the choice to display certain links or not, if there are links they do not want to see they can just hide them.
So first we need to start off by creating our HTML document and defining our javascript function in the
of our document.-
<html>
-
<head>
-
<title>Creating javascript pulldown menus</title>
-
<script language="Javascript" type="text/javascript">
-
<!-- Hide from old browsers
-
-
function displayMenu(currentMenu) {
-
var thisMenu = document.getElementById(currentMenu).style
-
-
// The above creates a new function that will expand and contract certain menus that are specified.
-
-
if (thisMenu.display == "block") {
-
thisMenu.display = "none"
-
} else {
-
thisMenu.display = "block"
-
}
-
return false
-
}
-
// -->
-
</script>
So the above piece of code may seem a bit long, confusing and daunting and first, but fear not it is actually quite easy to understand. All we are doing is starting a <script> section in the <head> of our HTML document, then we define a new function. This is the function we will be using to actually contract and expand the menus. It just says, if the menu is already expanded, then contract it and then we have a } else { statement that says if it is already contracted, expand it.
Now we need to do a bit of CSS work for our menus to work properly.
-
<style type="text/css">
-
.menu {
-
display: none;
-
margin-left: 15px;
-
color: #000;
-
}
-
.menu a {
-
color: #000;
-
}
-
.menu a:hover {
-
color: #B22222;
-
margin-left: 20px;
-
}
-
</style>
-
</head>
The above piece of code is the CSS in our menu, Im just putting this in so that it may be a BIT more aesthetically pleasing to our eyes, obviously if you use this script in your own websites/applications then you style the menus to how you like.
Now we start the actual body of our HTML document and start to create our menus.
I thought I would cut it off there and explain that little snippet of code, what we are doing here is creating a text link and using an event handler on it. We are using the onClick event handler. What this does is ON CLICK of the link we execute an action. In the case of this piece of code we are calling our displayMenu function that we created earlier. You may also notice the piece of text in there that says 'tutMenu'. This is defining the name of this specific menu, because if we didnt name our menus then the javascript function wouldnt know which menu to expand or contract.
Now we are going to create the navigational options that you get when you click on the header.
This is our navigational menu, we are using the menu class we created in our CSS style before. And I just put # in the menu links just for filler, when you use this script in an actual website application you would change those # to the actual links you want to visit when you click on the tutorials.
Now I am just going to create another 2 menu headers and 2 more sets of menu navigation links for more filler and to give you an idea of what it looks like and how it works with more then 1.
So all I've done above is create 2 new menus as filler menus.
Now for all you people that would like to see the completed code in its entirity here you go -
-
<title>Creating javascript pulldown menus</title>
-
<script language="Javascript" type="text/javascript">
-
<!-- Hide from old browsers
-
function displayMenu(currentMenu) {
-
var thisMenu = document.getElementById(currentMenu).style
-
// The above creates a new function that will expand and contract certain menus that are specified.
-
if (thisMenu.display == "block") {
-
thisMenu.display = "none"
-
} else {
-
thisMenu.display = "block"
-
}
-
return false
-
}
-
// -->
-
</script>
-
<style type="text/css">
-
.menu {
-
display: none;
-
margin-left: 15px;
-
color: #000;
-
}
-
.menu a {
-
color: #000;
-
}
-
.menu a:hover {
-
color: #B22222;
-
margin-left: 20px;
-
}
-
</style>
-
</head>
-
<h3>Click on a menu header to expand or contract it</h3>
-
-
<div class="menu" id="tutMenu">
-
</div>
-
<div class="menu" id="linksMenu">
-
</div>
-
-
<div class="menu" id="randomMenu">
-
</div>
-
</body>
-
</html>
Notes
When you are using the <div class=" " id=" "> you MUST define the id=" " as the same name that you defined in the head in the displayMenu('') function. So for example, in one of the menus I have defined the displayMenu('') parameter as randomMenu, so for my id=" " I must define it as id="randomMenu".
You must also remember that anyone with javascript disabled in there web browsers will not be able to use this navigation, so it is always a good idea to provide alternatives to your main navigation and provide links in your footer, this is also a good idea for Search Engines, because a lot of search engine spiders do not have javascript and will not be able to crawl any of the links located in your navigation. So a fix for that is to just place your links in the footer of your website to provide maximum usability.
Well, thank you for taking the time to read my tutorial. I hope you have come away from this tutorial with some new knowledge.
Thanks
Sean


(15 votes, average: 3.8 out of 5)










March 8th, 2007 at 2:00 pm
This is great, it works perfect too. I might use it on my own website!
March 17th, 2007 at 12:28 pm
Thanks.. thanks... thanks
March 17th, 2007 at 3:52 pm
Cool! but how i chance the name when the menu is down example: show comments and after click: Close comments(sorry my english!)
March 29th, 2007 at 9:45 am
Excellent! It is the very thing I want. Thanks a lot.
April 9th, 2007 at 12:19 pm
Great code! One problem with FireFox 2.0, the submenus get shaky when you hover at the left edge of them. Is there a way to fix this?
April 9th, 2007 at 12:48 pm
Ah I see exactly what you mean, when I made this tutorial I was still using firefox 1.5 - I will see if I can find a fix for this.
Thanks for pointing it out!
May 8th, 2007 at 7:17 am
kool...thanx..
also please could you post how I could get the list of links in a different container upon clicking the link (in the list)?
May 27th, 2007 at 7:28 am
Nice Tut, I'll learn that making menus in Javascript, doesn't require some long end code, just simple like this.. Thanks dude, for an great tut
July 10th, 2007 at 10:26 am
Great! Works in both IE and FF (2.0.0.4)
Thanks!!
July 10th, 2007 at 11:03 am
Is there a way to have one pulled down menu close automatically when pulling down another one?
July 17th, 2007 at 8:53 am
Can you change the onclick to allow the menu to expand when it is hovered over?
August 23rd, 2007 at 9:38 am
Is there a way to make it multi level?
September 7th, 2008 at 8:49 am
Thanx. this is wat i was looking for.
November 12th, 2008 at 4:43 pm
lr8j3g1zke1wy1df