Expanding/collapsing javascript menu

by asvin

In this post I'll show you how to create an expanding/collapsing menu like Slashdot left menu used to be, using the YUI library and Dav Glass YUI effects widget.

Preview of the script we are going to build.

The HTML markup for our menu is :

  1.  
  2. <div id="menu">
  3. <h1 id="menu-title">Menu</h1>
  4. <div id="menu-links">
  5. <li>Home</li>
  6. <li>About</li>
  7. <li>CSS</li>
  8. </ul></div>
  9. </div>
  10.  

We use 2 div tags, one for holding the menu itself and the other one which contains the links in an unordered list and 1 h1 tag for the title.

The CSS for the menu :

  1.  
  2. /** width of the menu */
  3. #menu{
  4. width: 180px;
  5. }
  6.  
  7. /** the main title */
  8. #menu h1{
  9. line-height: 35px;
  10. color: #fff;
  11. /** simulate link */
  12. cursor: pointer;
  13. font-weight: bold;
  14. /** background image will change when menu expands/collapses */
  15. background: #666 url(images/block-arrow-expanded.gif) no-repeat scroll 30px 13px;
  16. }
  17.  
  18. /** the links */
  19. #menu-links li{
  20. line-height: 35px;
  21. border-bottom:1px solid #ddd;
  22. background-color: #eee;
  23. }
  24.  

Please pay attention to the background for the #menu h1. It contains the image, which will change when the menu collapses or expands. When the menu collapses we'll be using the block-arrow-collapsed.gif image and when the menu expands we'll be using the block-arrow-expanded.gif image. You can find the images in the sample demo file in the images directory.

To do the collapsing/expanding effects, we need to include the following javascript files beforehand :

  1.  
  2. <!-- js -->
  3. <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&2.5.2/build/animation/animation-min.js"></script>
  4. <script type="text/javascript" src="js/tools-min.js"></script>
  5. <script type="text/javascript" src="js/effects-min.js"></script>
  6.  

We use Dav Glass YAHOO.Tools package and his effects widget along with yahoo-dom-event-animation aggregate file which are hosted on Yahoo servers.

Finally the javascript to do the magical stuff goes like that :

  1.  
  2. <script type="text/javascript">
  3. YAHOO.util.Event.addListener('menu-title', 'click', function(){
  4. if(YAHOO.util.Dom.getStyle('menu-links', 'display') == 'block'){
  5. new YAHOO.widget.Effects.BlindUp('menu-links', {seconds: 0.2});
  6. YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-collapsed.gif)');
  7. }
  8. else{
  9. new YAHOO.widget.Effects.BlindDown('menu-links', {seconds: 0.2});
  10. YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-expanded.gif)');
  11. }
  12. });
  13. </script>
  14.  

In line 3 of the above listing, we wait when the user clicks on the menu-title container and we check the display attribute of our links. If the links are not currently displayed then we display them by using Dav's BlindDown effect with a duration of 0.2 seconds ;-).

  1.  
  2. new YAHOO.widget.Effects.BlindDown('menu-links', {seconds: 0.2});
  3.  

We also change the background image for the menu title by using the YAHOO.util.Dom.setStyle method:

  1.  
  2.  
  3. YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-expanded.gif)');
  4.  

If the links are visible then we do the contrary, i.e we use the BlindUp effect and use the collapsed gif as background image.

Download sample demo



Read more here