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 :
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 :
/** width of the menu */ #menu{ width: 180px; } /** the main title */ #menu h1{ line-height: 35px; color: #fff; /** simulate link */ cursor: pointer; font-weight: bold; /** background image will change when menu expands/collapses */ background: #666 url(images/block-arrow-expanded.gif) no-repeat scroll 30px 13px; } /** the links */ #menu-links li{ line-height: 35px; border-bottom:1px solid #ddd; background-color: #eee; }
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 :
<!-- js --> <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> <script type="text/javascript" src="js/tools-min.js"></script> <script type="text/javascript" src="js/effects-min.js"></script>
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 :
<script type="text/javascript"> YAHOO.util.Event.addListener('menu-title', 'click', function(){ if(YAHOO.util.Dom.getStyle('menu-links', 'display') == 'block'){ new YAHOO.widget.Effects.BlindUp('menu-links', {seconds: 0.2}); YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-collapsed.gif)'); } else{ new YAHOO.widget.Effects.BlindDown('menu-links', {seconds: 0.2}); YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-expanded.gif)'); } }); </script>
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 ;-).
new YAHOO.widget.Effects.BlindDown('menu-links', {seconds: 0.2});
We also change the background image for the menu title by using the YAHOO.util.Dom.setStyle method:
YAHOO.util.Dom.setStyle('menu-title', 'background-image', 'url(images/block-arrow-expanded.gif)');
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.
Rien compris!!!! Merci quand meme si t’as fait un truc sympa…mais vraiment…rien capté !!!!
o moins tone essaye compran
Nice article!!
thanks Dav
Dave:
Is it possible to create the same menu effect using the tag rather than the and tags?
Thanks!
Dave, Ok my last comment dis not post correctly.
I was asking if I could create the menu using a DIV tag rather than the UL and il tags.
Thanks!
Shinshuri,
In fact, it’s Asvin not Dav.
Yes, you can use DIV tags instead of the list. Makes no difference, it should work no problem, you’ll just have to modify the stylesheet. Let me know if you want a helping hand.
Hey thanks a bunch. I’ll let you know if I do need help.
Asvin:
I think I’m gonna need some help on trying to implement this expand and collapse method in my code using the DIV tag instead of the List tags. I’d like to take you up on the offer to help me.
Also, once the .js file is implemented will there be issues of having to access Yahoo’s server to run this in the client internal to my organization?
Let me know Thanks!
Hi Asvin:
Can you help me with coding the DIV tag in the expand/collapse menu? Or maybe entertain some questions, etc.
Thanks,
Shin
Shin,
Sure will help you, just give me till tomorrow, i’am overloaded at work
Don’t worry will help u
Shin,
check out this demo which doesn’t use any LI :
http://htmlblog.net/demo/yui-menu-slashdot/div.html
Concerning including the yahoo libraries, no problem, you can download them and extract them to your internal web server.
Download it here :
http://sourceforge.net/project/downloading.php?group_id=165715&filename=yui_2.5.2.zip
Let me know if it’s OK for you.