Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create vertical Drop-Down menu
#1
Welcome and Enjoy!

Intro:
In this tutorial I decided to show you how to create drop-down menu with javascript.

Files:
  • menu.htm
  • drop.js

Writing JavaScript:
We start off by creating our menu variables.

Code:
var mainMenu = new Array();
mainMenu['first'] = new Array();
mainMenu['first']['Home'] = 'index.htm';
mainMenu['first']['Forum'] = 'forum.htm';

mainMenu['second'] = new Array();
mainMenu['second']['SubHome'] = 'sub.htm';
mainMenu['second']['Forum'] = 'forum.php';

mainMenu['third'] = new Array();
mainMenu['third']['SubThird'] = 'sub.htm';
mainMenu['third']['SubLink'] = 'forum.php';

We create mainMenu as a global variable and as array, next step is to define our links.
Code:
mainMenu['first'] = new Array();

We define array member "first" as new array to hold our link for the menu with the id "first".
And then we create new links by adding new memebrs to our array "mainMenu['first']".

Code:
mainMenu['first']['Home'] = 'index.htm';

Now "first" is used to identify our category for the link list, "Home" is used as a link text and the whole variable holds the path where the link should go,
in this case "index.htm".

Now let us define a variable to control status of the current open menu.
Code:
var menuLast = null;

Now we can start writing our function to open menu.

Code:
function dropdownMenu(menuID) {
    if(menuLast != null) {
        closeMenu(menuLast);
    if(menuLast == menuID) {
        menuLast = null;
        return 1;
    }
    }
    
    var subMenu = document.createElement('div');
    subMenu.setAttribute('id', 'subMenu');
    
    for(aIndex in mainMenu[menuID]) {
    var subLink = document.createElement('div');
    subLink.innerHTML = '<a href="' + mainMenu[menuID][aIndex] + '">' + aIndex + '</a>';
    subMenu.appendChild(subLink);
    }
    
    var parentMenu = document.getElementById(menuID);
    parentMenu.appendChild(subMenu);
    
    menuLast = menuID;
}

The first line is a conditional statement with wich we are checking if "menuLast" isn't null, if it's
not null that means that a menu is open and we call a function to close it.
The next "if" condition we use to check if the menu that was called is already open, if it's open we set "menuLast" to null and end
the function with "return 1;"

In the next step we create new "DIV" element using javscripts "DOM" function createElement, and we set the id
of the element to "subMenu".
This will hold our sub links.
Code:
var subMenu = document.createElement('div');
subMenu.setAttribute('id', 'subMenu');

With the next part of code we are doing a loop trough our menu list, and again we use the DOM function to create new div element and the populate
it's conetent with the "innerHTML" parameter.
With "subMenu.appendChild" we add our newly created element "subLink" to the subMenu.
Code:
for(aIndex in mainMenu[menuID]) {
    var subLink = document.createElement('div');
    subLink.innerHTML = '<a href="' + mainMenu[aIndex] + '">' + aIndex + '</a>';
    subMenu.appendChild(subLink);
}

Now we use "document.getElementById()" to get the parent category for our drop down box.
And then we append our subMenu to our parent.
Code:
var parentMenu = document.getElementById(menuID);
parentMenu.appendChild(subMenu);
    
menuLast = menuID;

And we lastly define "menuLast".

Now the function to close the drop down box.
Code:
function closeMenu(menuID) {
    var menu = document.getElementById(menuID);
    var subMenu = document.getElementById('subMenu');
    
    menu.removeChild(subMenu);
}

We use "getElementById()" to get our open menu parent, and then we use the same function to get our
opened menu.

With the DOM function "removeChild()" we remove it from our parent (menu).

So far so good, now the html

Code:
<div id="menu">
    <div class="menuItem" id="first">
        <div class="mainLink" onmouseup="dropdownMenu('first');">First</div>
    </div>
    <div class="menuItem" id="second">
        <div class="mainLink" onmouseup="dropdownMenu('second');">Second</div>
    </div>
    <div class="menuItem" id="third">
        <div class="mainLink" onmouseup="dropdownMenu('third');">Third</div>
    </div>
</div>




Thank you for reading!
Reply
#2
This is just a BUMP, but hell if no one likes it... well that's ok.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu crashes on website from Macs Naflod 0 540 03-23-2012, 12:35 PM
Last Post: Naflod
  A menu like yours Rik Telner 11 1,989 12-11-2011, 08:33 PM
Last Post: Hex
  Problem with labroccas tabbed menu plugin HostMyPicture 17 1,551 08-14-2011, 12:39 AM
Last Post: FISH
  Who wants to create a website? Emotion 8 1,227 08-02-2010, 11:52 PM
Last Post: Cybr
  [TUT] Drop-Shadow using CSS and HTML Gaijin 4 1,241 04-06-2010, 05:28 PM
Last Post: Navineous

Forum Jump:


Users browsing this thread: 1 Guest(s)