Preface
Waaaaasssuuuuupppp? Kidding. Drop-down menus are always a pain for me because you have to remember to do all kinds of CSS and it has to work in 45 different browsers. Rubbish, I say! I recently decided to add an animated drop-down menu to for the user section. The HTML/CSS was pretty straight forward but I hit a snag with the JavaScript so I sent an email to the Dojo Toolkit Mailing List and got a quick response from Peter Higgins (those Dojo guys are awesome). I promised a few blogs (and more to come in the future) to showcase some of Dojo’s quick & easy wow features. This post assumes that you already know the basics about Dojo and have included Dojo somewhere in your project. If not, please check out Dojo Campus and the Dojo Toolkit homepage to get started.
Live Demonstration
Take a peak at the live demonstration to see where we’re headed. If you’re interested in how I got there keep on reading!
The HTML
The HTML is pretty straight forward and utilizes an unordered list for the layout. Note: all of the code here is going to be a straight copy/paste from the Blitzaroo source.
The only required attributes are class=”tabs” on the root
- and class=”dropdown” on each
- that is going to be a drop-down menu. The above menu has four regular links (Dashboard, Events, Profiles, and Teams.) and one drop-down menu (Add). I’ve spruced it up a bit by adding a few images but that is completely optional. I use the id tag on the root
- for styling the menu. No surprise here but the HTML renders a menu that looks like:
The CSS
.downArrow { font-size: 85%; vertical-align: text-bottom; } #userMenu { -webkit-border-radius: 8px; background: #f5faf5; border: 1px solid #bbcfbb; display: table; font-size: 13px; font-weight: bold; list-style-type: none; margin: 8px 0; padding: 0 12px; position: relative; } #userMenu li { cursor: pointer; font-size: 12px; list-style: none; display: table-cell; float: left; position: relative; text-shadow: 0px 0px 1px #fff; } #userMenu li a,#userMenu li div { color: #154f14; font-size: 11px; padding: 5px 12px; } #userMenu li a:hover,#userMenu li div:hover { background-color: #dae8da; text-decoration: none; } #userMenu img { margin-right: 3px; vertical-align: top; } #userMenu a { position: relative; display: block; } #userMenu .dropdown { -webkit-border-bottom-left-radius: 8px; -webkit-border-bottom-right-radius: 8px; background: #f5faf5; border: 1px solid #BBCFBB; margin: 0; padding: 0; position: absolute; z-index: 999; top: 24px; left: -999em; height: 1px; display: block; } #userMenu .dropdown li { margin: 0; padding: 0; list-style: none; width: 145px; } #userMenu .dropdown li:hover { opacity: 1; } #userMenu .dropdown li a { display: block; padding: 6px 14px; text-shadow: 0px 0px 1px #fff; } #userMenu .dropdown li a:hover { text-decoration: none; }
I’m a huge fan of CSS3 so I use border-radius quite liberally. Blitzaroo isn’t released yet so I haven’t added the Mozilla border-radius (-moz-border-radius) equivalent so you can either add it or remove the radius all together. The CSS itself is nothing special – I use display: block with a touch of padding on tags and float
The JavaScript
I found the JavaScript on a tutorial to Create The Fanciest Dropdown Menu You Ever Saw which uses jQuery rather than Dojo. Personally, I think jQuery is over-hyped and I love Dojo (/me wipes nose). I took the JavaScript listed there, combined it with the mailing list love from Peter Higgins, and came up with:
dojo.addOnLoad(function() { // user menu special effects var userMenu = dojo.byId("userMenu"); if (userMenu) { dojo.query(".dropdown").forEach(function(n) { var l = dojo.query(n); l.parent().at(0) .onmouseenter(function(){ dojo.style(l[0], "left", "0"); dojo.fx.wipeIn({node: l[0], duration: 250}).play(); }) .onmouseleave(function(){ dojo.style(l[0], "left", "-999em"); dojo.style(l[0], "display", "none"); }); }); } });
This little snippet recursively checks for .dropdown and when the mouse enters sets left: 0; followed by an animated Dojo wipeIn. On mouse out it sets left: -999em; and then sets the display to none. I tried a number of CSS/FX combinations to come up with a solution that worked and this is about all I could come up with. You could use any combination of Dojo FX to stylize your own menu. My favorites are wipeIn, wipeOut, fadeIn, and fadeOut combined with easing. Try out a few combinations and see what you like best. Enjoy!
Tags: Cascading Style Sheets, CSS, Data Formats, Dojo Toolkit, HTML, HTML element, JavaScript, Style Sheets
[...] Creating fancy drop-down menus with HTML, CSS, and Dojo. [...]
Nicely done sir!
What’s this line of code here Spliffy?
l.parent().at(0)
l is the variable. Is parent() the Dojo equivalent of parentNode? And what does at(0) mean?
Peter Higgins helped me out with that one. The n is the query result (
). The l (lowercase L) is the parentNode which would be the
, and I believe the .at(0) would be the first element. I suppose that’s not really needed in this example and the code seems to function fine without it.
Stupid HTML markup:
1.
2.
Those should be in place of the “…” from above.
Great work, thank you very much!
merci, this is very2 help me
Great stuff here!
Not sure if it’s just me, but i found that dojo.NodeList-traverse needed to be included to call the parent() function.
Rob
Great method! I wanted to use this in my current project but apparently it only works in 1.4 and forware due to the Nodelist-Traverse. Does anyone know how to accomplish this functionality in 1.3.2? It’s the one I’m stuck with unfortunately. Thanks in advance.
Hey man, you’ve saved my life.
For my current ZF project I need a fancy menu, but I don’t want mixing different JS libraries. Great work!
Nice menu!
Thank you!
Just one problem with it… It doesn’t work correctly in IE. Firefox doesn’t produce any problems
Great post! You must a expert on creating a menu:)
Hey great post!
Have an issue though…
It looks great on Google Chrome but doesn’t seem to appear the same way in IE7; the background colour and curved edges are missing and the menu extends right across the page?
For 1.3 dojo i got it working with
dojo.addOnLoad(function() {
// user menu special effects
var userMenu = dojo.byId(“userMenu”);
if (userMenu) {
dojo.query(“.dropdown”).forEach(function(n) {
var l = dojo.query(n);
var topdiv = dojo.query(l[0].parentNode.firstElementChild);
topdiv.onmouseenter(function(){
dojo.style(l[0], “left”, “0″);
dojo.fx.wipeIn({node: l[0], duration: 250}).play();
})
.onmouseleave(function(){
dojo.style(l[0], “left”, “-999em”);
dojo.style(l[0], “display”, “none”);
});
});
}
});
Uups. Went one level too low inside
It should be:
var topdiv = dojo.query(l[0].parentNode);
Otherwise you can not access the submenus