Home arrow JavaScript arrow Page 3 - Dynamic Drop-down Menus: Javascript Progressive Enhancement
JAVASCRIPT

Dynamic Drop-down Menus: Javascript Progressive Enhancement


In this conclusion to a nine-part series on Progressive Enhancement, we'll take the drop-down menu that we designed in the previous part and make its behavior dynamic with the addition of jQuery's "animate()" method. Users with JavaScript disabled on their browsers, however, will still be able to make complete use of the menu.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
July 28, 2010
TABLE OF CONTENTS:
  1. · Dynamic Drop-down Menus: Javascript Progressive Enhancement
  2. · Review: the sample menu in its current state
  3. · Building a dynamic version of the menu
  4. · The menu’s full source code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Dynamic Drop-down Menus: Javascript Progressive Enhancement - Building a dynamic version of the menu
(Page 3 of 4 )

As you know, there are many ways to build a drop-down menu based on the markup defined in the previous segment. In this particular case, though, my approach is to build it by using the handy “animate()” method included with jQuery, which supports a couple of easy options that are very simple to use.

With that said, here’s the JavaScript code that will expand and collapse each section of the menu: 

$(document).ready(function() {

    // set the height of all the menu sections to 0

    $("ul#navbar li.menu ul").css("height", "0");

    // expand the current menu

    $("#navbar li.menu").mouseover(function() {

        var ul = $(this).find('ul');

        var items = ul.children().length * 50;

        ul.animate({height: items}, {queue: false, duration: 300})

    });

    // shrink the current menu

    $("#navbar li.menu").mouseout(function() {

        $(this).find('ul').animate({height: 0}, {queue: false, duration: 300})

    });

});

</script>

To be frank, understanding what this short script does is a breeze. It simply triggers two different callback functions to show and hide each section of the menu. Quite possibly, the script’s trickiest part is that this process is achieved by dynamically calculating the height that will be assigned to each animated section, based on the number of items that it contains. Anyway, if you’re familiar with some of the DOM traversing methods that jQuery offers, you should grasp quickly how this task is performed by the previous snippet.

Now that the behavioral layer of the menu has been properly set, it’s time to bind it to the web page coded before. Once this step is complete, you’ll be able to test the menu in its "trim" and "enhanced" versions and see for yourself that it’s really functional in both cases.

This final assembly process will be done in the next section. Thus, click on the following link and keep reading.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials