<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The html blog &#187; menu</title>
	<atom:link href="http://htmlblog.net/tag/menu/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Wed, 07 Oct 2009 17:20:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bubble menu javascript or playing with YUI&#8217;s event delegation</title>
		<link>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/</link>
		<comments>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 15:10:41 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=63</guid>
		<description><![CDATA[
Image courtesy of jobee59's photostream
Event delegation refers to the use of a single event listener on a parent object to listen for events happening on its children (or deeper descendants). Event delegation allows developers to be sparse in their application of event listeners while still reacting to events as they happen on highly specific targets. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm1.static.flickr.com/199/469216645_020fc74cce.jpg?v=0" alt="bubble" /><br />
Image courtesy of <a href="http://flickr.com/photos/jobee59/469216645/" target="_blank">jobee59's photostream</a></p>
<p>Event delegation refers to the use of a single event listener on a parent object to listen for events happening on its children (or deeper descendants). Event delegation allows developers to be sparse in their application of event listeners while still reacting to events as they happen on highly specific targets. This proves to be a key strategy for maintaining high performance in event-rich web projects, where the creation of hundreds of event listeners can quickly degrade performance. <a href="http://developer.yahoo.com/yui/examples/event/event-delegation.html" target="_blank">More on event delegation including examples</a>.</p>
<p>This post illustrates the use of event delegation to create a "bubble" menu, inspired by <a href="http://nettuts.com/html-css-techniques/how-to-create-a-mootools-homepage-inspired-navigation-effect-using-jquery/" target="_blank">Bedrich Rios' post on Nettuts</a>.<br />
<span id="more-63"></span><br />
<a href="/demo/bubble/" target="_blank">Demo</a><br />
<a href="/demo/bubble/bubble.zip">Download sample file</a><br />
<br/><br />
<br/></p>
<p class="headline"><strong>The HTML</strong></p>
<p>We'll need the following components from the <a href="http://developer.yahoo.com/yui" target="_blank">YUI library</a>:</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/dom/" target="_blank">yahoo-dom-event</a> - for DOM/Event handling</li>
<li><a href="http://developer.yahoo.com/yui/animation/" target="_blank">animation</a> - for sliding effects </li>
</ul>
<p>So our basic HTML page is like this:</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&gt;</span></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/build/animation/animation-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p>Next we'll add some structural markup, a simple list, with "menu" as class name and with 5 items.</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;</pre>
<p>Updated HTML page:</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&gt;</span></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/build/animation/animation-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p class="headline"><strong>The Javascript</strong></p>
<p>Then we just have to add the <a href="/demo/bubble/bubble.zip" target="_blank">bubble.js</a> javascript file to our page and it's done, we'll have a nice "bubble" menu.</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&gt;</span></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/build/animation/animation-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- our bubble file --&gt;</span></span>
		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;bubble.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p class="headline"><strong>bubble.js</strong></p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">/**
*	Bubble Menu
*	@author Asvin Balloo (http://htmlblog.net)
*/</span>
bubbleMenu = <span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// private variables</span>
	<span style="color: #009900; font-style: italic;">// class name of our menu</span>
	menuClassName: <span style="color: #003366; font-weight: bold;">null</span>,
	<span style="color: #009900; font-style: italic;">// by how much we are sliding</span>
	moveToLength : <span style="color: #CC0000;">10</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Init the whole process here
	*	@method init
	*	@params menuClassName {string} class name of our menus
	*/</span>
	init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>menuClassName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// set our class name to be used later one</span>
		bubbleMenu.<span style="color: #006600;">menuClassName</span> = menuClassName;
&nbsp;
		<span style="color: #009900; font-style: italic;">// get all the lists with the class name</span>
		<span style="color: #003366; font-weight: bold;">var</span> list = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span>menuClassName<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// loop through them</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i
&lt;list.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// add the onmouseover event to it, call the mouseOverHandler function</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;mouseover&quot;</span>, bubbleMenu.<span style="color: #006600;">mouseOverHandler</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #009900; font-style: italic;">// get the first child and its padding left for future reference. Useful when using onmouseout to </span>
&nbsp;
restore the <span style="color: #000066; font-weight: bold;">item</span> to original state
			<span style="color: #003366; font-weight: bold;">var</span> listItems = list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #003366; font-weight: bold;">var</span> startingPadding = parseInt<span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span>listItems<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">'paddingLeft'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #009900; font-style: italic;">// add the onmouseout event, call the mouseOutHandler function</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;mouseout&quot;</span>, bubbleMenu.<span style="color: #006600;">mouseOutHandler</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'list'</span>:list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;padding&quot;</span> : 
&nbsp;
startingPadding<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Mouse over handler
	*	@params e {Event} the event
	*/</span>
	mouseOverHandler: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// get the target</span>
		<span style="color: #003366; font-weight: bold;">var</span> elTarget = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">getTarget</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// calculate the current padding left</span>
		<span style="color: #003366; font-weight: bold;">var</span> paddingLeft = parseInt<span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span>elTarget, <span style="color: #3366CC;">'paddingLeft'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// call the delegate method, with the target and the padding left value</span>
		bubbleMenu.<span style="color: #006600;">delegate</span><span style="color: #66cc66;">&#40;</span>elTarget, paddingLeft<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Mouse out handler
	*	@params e {Event} the event
	*	@params o {JSON} JSON data containing the list + padding value, eg. {'list': mylist, 'padding': 10}
	*/</span>
	mouseOutHandler: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e, o<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// get the target</span>
		<span style="color: #003366; font-weight: bold;">var</span> elTarget = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">getTarget</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// calculate the left padding, the original padding value - by how much we have padded</span>
		<span style="color: #003366; font-weight: bold;">var</span> paddingLeft = o.<span style="color: #006600;">padding</span> - bubbleMenu.<span style="color: #006600;">moveToLength</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// call the delegate method with the target and padding value</span>
		bubbleMenu.<span style="color: #006600;">delegate</span><span style="color: #66cc66;">&#40;</span>elTarget, paddingLeft<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Cool things go here, like animation
	*	@params elTarget {Target} our target
	*	@params padding {Int} by how much we are going to padd
	*/</span>
	delegate: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>elTarget, padding<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//walk up the DOM tree looking for an &lt;li&gt; in the target's ancestry; desist when you reach the container with our class name</span>
		<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">className</span> != bubbleMenu.<span style="color: #006600;">menuClassName</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// are you an LI?</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">nodeName</span>.<span style="color: #006600;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">&quot;LI&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// if so then animate, set the attributes, in our case the left padding</span>
				<span style="color: #003366; font-weight: bold;">var</span> attributes = <span style="color: #66cc66;">&#123;</span> paddingLeft: <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;to&quot;</span>: <span style="color: #66cc66;">&#91;</span>padding + bubbleMenu.<span style="color: #006600;">moveToLength</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// create our animation</span>
				<span style="color: #003366; font-weight: bold;">var</span> anim = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Motion</span><span style="color: #66cc66;">&#40;</span>elTarget, attributes, <span style="color: #CC0000;">0.6</span>, YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Easing</span>.<span style="color: #006600;">bounceOut</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// then animate</span>
				anim.<span style="color: #006600;">animate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #000066; font-weight: bold;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// it's not an li, so we keep looking and looking</span>
	            elTarget = elTarget.<span style="color: #006600;">parentNode</span>;
	        <span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// init the whole thing here</span>
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">&quot;load&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	bubbleMenu.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;menu&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>You can have unlimited (don't abuse) number of bubble menus by just adding the "menu" class to each one of them and including the bubble.js file.<br />
If you're not happy with the menu class or you found another cool class name, then you'll have to update the bubble.js file towards the end:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// init the whole thing here</span>
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">&quot;load&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	bubbleMenu.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;mycoolclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Also you can modify by how much an item slides by modifying the moveToLength variable (default 10):</p>
<pre class="javascript">&nbsp;
&nbsp;
<span style="color: #009900; font-style: italic;">// by how much we are sliding</span>
moveToLength : <span style="color: #CC0000;">30</span>,
&nbsp;</pre>
<p>Note that the demo is using reset-fonts-grids CSS and the page has been styled with the following:</p>
<pre class="css">&nbsp;
body <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span>;
	<span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">0</span>;
	<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#1d1d1d</span>;
	<span style="color: #000000; font-weight: bold;">font-family</span>: <span style="color: #ff0000;">&quot;Lucida Grande&quot;</span>, Verdana, <span style="color: #993333;">sans-serif</span>;
	<span style="color: #000000; font-weight: bold;">font-size</span>: <span style="color: #933;"><span style="color: #933;">100</span>%</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.menu</span> li<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding-left</span>: <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">2em</span>;
	<span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">150px</span>;
	<span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#<span style="color: #933;">222</span></span>;
	<span style="color: #000000; font-weight: bold;">border</span>: <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#1a1a1a</span>;
	-moz-border-radius: <span style="color: #933;">10px</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Expanding/collapsing javascript menu</title>
		<link>http://htmlblog.net/expanding-collapsing-javascript-menu/</link>
		<comments>http://htmlblog.net/expanding-collapsing-javascript-menu/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 18:07:13 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[collapse]]></category>
		<category><![CDATA[dav glass]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[expand]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[newbie]]></category>
		<category><![CDATA[slashdot]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=22</guid>
		<description><![CDATA[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 :
&#160;&#60;div id=&#34;menu&#34;&#62;&#60;h1 id=&#34;menu-title&#34;&#62;Menu&#60;/h1&#62;&#60;div id=&#34;menu-links&#34;&#62;&#60;ul&#62;&#60;li&#62;Home&#60;/li&#62;&#60;li&#62;About&#60;/li&#62;&#60;li&#62;CSS&#60;/li&#62;&#60;/ul&#62;&#60;/div&#62;&#60;/div&#62;&#160;
We use 2 div tags, one for holding the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I'll show you how to create an expanding/collapsing menu like <a href="http://slashdot.org">Slashdot</a> left menu used to be, using the <a href="http://developer.yahoo.com/yui">YUI library</a> and <a href="http://blog.davglass.com/">Dav Glass</a> <a href="http://blog.davglass.com/files/yui/effects/">YUI effects widget</a>.</p>
<p><strong><a href="http://htmlblog.net/demo/yui-menu-slashdot/">Preview of the script we are going to build.</a></strong><br />
<span id="more-22"></span><br />
The HTML markup for our menu is :</p>
<pre class="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">&lt;h1</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu-title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu-links&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Home<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>About<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>CSS<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We use 2 <strong>div</strong> tags, one for holding the menu itself and the other one which contains the links in an <strong>unordered list</strong> and 1 <strong>h1</strong> tag for the title.</p>
<p>The CSS for the menu :</p>
<pre class="css"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** width of the menu */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #cc00cc;">#menu</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">180px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** the main title */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #cc00cc;">#menu</span> h1<span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">35px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#fff</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #808080; font-style: italic;">/** simulate link */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">cursor</span>: <span style="color: #993333;">pointer</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">font-weight</span>: <span style="color: #993333;">bold</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #808080; font-style: italic;">/** background image will change when menu expands/collapses */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span> <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #933;">images/block-arrow-expanded<span style="color: #6666ff;">.gif</span></span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #993333;">scroll</span> <span style="color: #933;">30px</span> <span style="color: #933;">13px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** the links */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #cc00cc;">#menu-links</span> li<span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">35px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				border-bottom<span style="color: #3333ff;">:<span style="color: #933;">1px</span></span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ddd</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#eee</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>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 <a href="http://htmlblog.net/demo/yui-menu-slashdot/yui-menu.zip">sample demo file</a> in the images directory.</p>
<p><a href='http://htmlblog.net/demo/yui-menu-slashdot/'><img src="http://htmlblog.net/wp-content/uploads/2008/07/menu.png" alt="" title="menu" width="362" height="260" class="alignleft size-full wp-image-23" /></a></p>
<div style="clear:both"></div>
<p>To do the collapsing/expanding effects, we need to include the following javascript files beforehand :</p>
<pre class="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>  <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.5.2/build/animation/animation-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/tools-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/effects-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We use Dav Glass <a href="http://blog.davglass.com/files/yui/tools/">YAHOO.Tools package</a> and his <a href="http://blog.davglass.com/files/yui/effects/">effects widget</a> along with yahoo-dom-event-animation <a href="http://yuiblog.com/blog/2008/07/16/combohandler/">aggregate file</a> which are hosted on Yahoo servers.</p>
<p>Finally the javascript to do the magical stuff goes like that :</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #3366CC;">'display'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">'block'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					<span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-collapsed.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					<span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-expanded.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		&lt;/script&gt;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>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 <a href="http://blog.davglass.com/files/yui/docs/?type=effects">BlindDown effect</a> with a duration of 0.2 seconds ;-).</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We also change the background image for the menu title by using the <a href="http://developer.yahoo.com/yui/dom">YAHOO.util.Dom.setStyle</a> method:</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-expanded.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>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.</p>
<p><strong><a href="http://htmlblog.net/demo/yui-menu-slashdot/yui-menu.zip">Download sample demo</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/expanding-collapsing-javascript-menu/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>
