Simple Drop-down menu with Prototype

I worked on my friend Dylan Rhodes’ website called Reezle that just entered its public beta. Among other things I did the main navigation menu for the site. Since the website uses the Prototype-Scriptaculous combination it was logical to create this element with the existing libraries.

I eventually came up with a solution that was both elegant and efficient and I decided to release this script as open-source (with permission of course) as it can have many other implementations.

See an example here

The above is included in the archive, along with the Prototype library, and I suggest you use that as a basis for your application. The actual JavaScript routine for the menu is this:

var DropDownMenu = Class.create();
 
DropDownMenu.prototype = {
 
 initialize: function(menuElement) {
	menuElement.childElements().each(function(node){
		// if there is a submenu
		var submenu = $A(node.getElementsByTagName("ul")).first();
		if(submenu != null){
			// make sub-menu invisible
			Element.extend(submenu).setStyle({display: 'none'});
			// toggle the visibility of the submenu
			node.onmouseover = node.onmouseout = function(){
				Element.toggle(submenu);
			}
		}
	});
}
 
};

Download the Script

Version: 1.0
Language: JavaScript
File size: 29,6kb

Last 5 projects by Makis

3 Responses to “Simple Drop-down menu with Prototype”

  1. Jonny Says:

    This doesn’t quite work IE6, jus thought I’d let you know

  2. makis Says:

    Thanks Jonny,

    That’s just fine – I think we can all live with the fact that IE6 is becoming obsolete, as you can see from the browser statistics:
    http://www.w3schools.com/browsers/browsers_stats.asp

    Give it another year and it will be the new IE5 ;)

  3. Ashish Says:

    Thank U

Leave a Reply