JavaScript Click Menu

Jan0308Jan 03, 08

As the result of a recent email conversation, I decided to create a simple menu system using JavaScript.

After completing the original script, it became apparent that it could be modified so that users could experience familiar "click" navigation. It is quite common to use nested lists to create the "menu" effect, but when the user wishes to expand a portion of a hidden list, speed should be the primary concern. Hence, A good solution is to use a script to augment your menu system, for which I propose the following:
Code:
window.oldMenuOnLoad = window.onload ? window.onload : function(){}; window.onload = function() { var lis = document.getElementsByTagName("li"); for(var c in lis) { if(lis[c] && lis[c].className && lis[c].className.indexOf("submenu") > -1 && lis[c].getElementsByTagName('ul').length==1) { lis[c].childNodes[0].onmousedown = function(e) { this.blur(); return false; }; lis[c].childNodes[0].onmouseup = function(e) { if(!e) e=window.event; var submenu = this.parentNode.getElementsByTagName('ul')[0]; if( (e.button && e.button!=2) || (e.which && e.which!=2) ) { this.blur(); if(window.menu_currentOpenSubmenu && window.menu_currentOpenSubmenu != submenu) window.menu_document_onmouseup(); else if(window.menu_currentOpenSubmenu) return false; submenu.onmouseout = function() { window.menu_currentMouseOutTimer = setTimeout("window.menu_document_onmouseup();", 3000); }; submenu.onmousemove = function() { if(window.menu_currentMouseOutTimer) clearTimeout(window.menu_currentMouseOutTimer); }; submenu.style.visibility = "hidden"; submenu.className = submenu.className.indexOf(' open')==-1 ? (submenu.className + ' open') : submenu.className.substring(0,submenu.className.indexOf(' open')); window.menu_currentOpenSubmenu = submenu; for(var i=0; i<10; i++) setTimeout("if(window.menu_currentOpenSubmenu) window.menu_currentOpenSubmenu.style.marginTop = '"+(submenu.offsetHeight/(10-i) - submenu.offsetHeight)+"px';", i*20); setTimeout("if(window.menu_currentOpenSubmenu) window.menu_currentOpenSubmenu.style.visibility = 'visible';", 10); window.menu_onMouseDownOld = document.onmouseup ? document.onmouseup : function(e){}; setTimeout("document.onmouseup = window.menu_document_onmouseup;", 1); window.menu_document_onmouseup = function() { if(window.menu_currentOpenSubmenu) { if(window.menu_currentOpenSubmenu.className) window.menu_currentOpenSubmenu.className = window.menu_currentOpenSubmenu.className.substring(0, window.menu_currentOpenSubmenu.className.indexOf(' open')); window.menu_currentOpenSubmenu = null; } document.onmouseup = window.menu_onMouseDownOld; }; } if(e.returnValue) e.returnValue = false; if(e.preventDefault) e.preventDefault(); return false; } } } window.oldMenuOnLoad(); };

Code:
<ul class="navmenu"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li class="submenu"><a href="javascript:void(0);">Sub Menu 1</a> <ul> <li class="first"><a href="#">Sub Menu 1, Item 1</a></li> <li><a href="#">Sub Menu 1, Item 2</a></li> <li><a href="#">Sub Menu 1, Item 3</a></li> <li><a href="#">Sub Menu 1, Item 4</a></li> </ul> </li> <li><a href="#">Link3</a></li> <li class="submenu"><a href="javascript:void(0);">Sub Menu 2</a> <ul> <li class="first"><a href="#">Sub Menu 2, Item 1</a></li> <li><a href="#">Sub Menu 2, Item 2</a></li> </ul> </li> <li><a href="#">Link4</a></li> </ul>
The first block of code creates the JavaScript building blocks that are required to transform this simple bulleted list into a convenient navigational menu. The second block of code is the actual bulleted list - the foundation and structure of the menu are defined by adding and removing <li> and <ul> elements as necessary. We put it together in a simple document structure, with the bulleted list inside the body of the document and the JavaScript inside the header. The result is a really nice menu. The CSS used in that example is lengthy, so you can simply view the source of the example to access that portion of the code.

About Jason Miller:

I am a JavaScript developer from Waterloo, Ontario, Canada. When I am not typing green code onto a black screen, you might find me at the nearest coffee pub checking out the brew. I run a internet firm called developIT and maintain blogs and web apps when I can.
Comments
jason#
Author's Note: this code is not well-written, overwrites mouse events and uses eval() implicitly via String parameters passed to setTimeout. There are numerous alternate solutions available, however, the general concept implemented here could still be of value. Therefore, I cannot recommended this for "copy & paste" use in its present state.
cheap jordans#
http://www.topsneakerwholesale.com/
http://www.adidasultraboost.uk/
http://www.adidasyeezy.us.org/
http://www.cheapsnapbackhats.us.com/
http://www.cheapyeezys.fr/
http://www.yeezysboost350.fr/
http://www.adidasyeezy.uk/
http://www.soronshop.com/
http://www.cheapairmax2018.co.uk/
http://www.dearlover.us/
http://www.kickstmall.com/
http://www.adidasultraboost.fr/
http://www.adidasyeezy.fr/
http://www.adidasyeezy350boost.uk/
http://www.adidasyeezy350boost.us.org/
http://www.cheapyeezy350boost.us/
http://www.yeezy350boost.uk.com/
http://www.yeezyboost350.fr/
http://www.yeezyboost350.com.de/
http://www.yeezyboost350forsale.co.uk/

http://www.yeezyboost350forsale.us.org/
http://www.yeezyboost.me.uk/
http://www.yeezyshoes.it/
http://www.yeezyshoes.me.uk/
http://www.airjordansforsale.us.com/
http://www.cheapjordan12.us.com/
http://www.cheapjordans.org/
http://www.cheapjordansforsale.co.uk/
http://www.cheapjordanshoes.org/
http://www.cheapjordansmen.us.com/
http://www.cheapjordansonline.us.org/
http://www.cheapjordansshoes.us.org/
http://www.cheappumashoes.us.com/
http://www.cheaprealyeezys.us.com/
http://www.cheapunderarmourshoes.com/
http://www.cheapyeezyboost350v2.us.com/
http://www.cheapyeezyv2.com/
http://www.jordansforcheap.us.org/
http://www.jordanshoescheap.us.org/
http://www.cheapairjordans.us.org/

http://www.cheapadidasshoes.us.com/
http://www.cheapcurryshoes.us.com/
http://www.cheapfakeyeezys.com/
http://www.cheapjordan11.us.com/
http://www.cheapjordansforsale.us.org/
http://www.cheapjordanshoes.us.org/
http://www.cheapkobes.us.com/
http://www.cheaplebronshoes.us.com/
http://www.cheapnikeshoes.us.org/
http://www.cheapnmd.com/
http://www.cheaprealjordans.us.org/
http://www.cheapyeezys.us.org/
http://www.cheapyeezyshoes.us.com/
http://www.jordanscheap.us.org/
http://www.lebron12cheap.com/
http://www.cheapadidasnmd.us.org/
http://www.cheapairforceones.us.com/
http://www.cheapairmax90.us.org/
http://www.cheapjordanshoesformen.us.com/
http://www.cheapjordansonline.co.uk/

http://www.cheapkdshoes.us.com/
http://www.cheapkobeshoes.us.com/
http://www.cheaplebron13.us.com/
http://www.cheaplebrons.us.com/
http://www.cheapnikeairmaxshoes.us.com/
http://www.cheapnikeroshe.us.com/
http://www.cheapretrojordans.us.org/
http://www.cheapultraboost.us.com/
http://www.cheapunderarmour.us.com/
http://www.cheapyeezyboost350.com/
http://www.cheapyeezys350.com/
http://www.cheapyeezysforsale.us.com/
http://www.discountjordans.us.com/
http://www.jordansforcheap.co.uk/
http://www.nikerosheruncheap.com/
http://www.offwhitecheap.com/
http://www.wholesalenikeshoes.us.com/

http://www.cheapchristianlouboutin.co.uk/
http://www.cheapchristianlouboutin.us.com/
http://www.christianlouboutinshoessale.co.uk/
http://www.christianlouboutinshoessale.us.org/
Leave a Comment

Post Comment