<?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>Dylan Butler » San Diego Freelance Web Development and Consultation &#187; menus</title>
	<atom:link href="http://www.dylanbutler.com/tag/menus/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dylanbutler.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 09 Aug 2010 16:31:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>YUI MenuCreator : generic show/hide code</title>
		<link>http://www.dylanbutler.com/2008/08/yui-generic-showhide-code/</link>
		<comments>http://www.dylanbutler.com/2008/08/yui-generic-showhide-code/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 01:03:25 +0000</pubDate>
		<dc:creator>Dylan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[menus]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.dylanbutler.com/?p=66</guid>
		<description><![CDATA[At work I frequently encounter designs that utilize a mouseover show/mouseout hide menu that usually consists of a trigger (often an A tag, or LI element) and a menu (often a DIV, or a UL). Due to the browser inconsistencies &#8230; <a href="http://www.dylanbutler.com/2008/08/yui-generic-showhide-code/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://pint.com/" target="_blank">work</a> I frequently encounter designs that utilize a mouseover show/mouseout hide menu that usually consists of a trigger (often an A tag, or LI element) and a menu (often a DIV, or a UL). Due to the browser inconsistencies between event handling and mouseout/mouseover DOM detection, it can be cumbersome to create a menu that is quick to implement (thus cost effective) and most importantly, stable. It also has support for a delay threshold, and the ability to animate show and hide events.</p>
<p><a href="/wp-content/uploads/2008/08/dropdowntest_example1.htm" target="_blank"><img class="alignnone size-full wp-image-67" title="menu" src="http://www.dylanbutler.com/wp-content/uploads/2008/08/menu.gif" alt="" width="530" height="283" /></a><br />
<span id="more-66"></span><br />
Althought I use javascript-free solutions for the majority of drop-downs I make, some menus simply warrant javascript and there&#8217;s no way around it. Perhaps the content inside the menu is interactive, or if javascript support has previously been established, or maybe <strong>the DOM structure requires two oddly placed elements to interact with each other like a normal drop-down would.</strong> Given that some advanced menus require javascript (sorry <a href="http://cssplay.co.uk" target="_blank">Stu Nichols</a>), this method uses javascript. All debates and javascript taboos aside, the code below is capable of creating a consistently stable show/hide relationship between a trigger element and a menu element.</p>
<p>My idea of a menu (it could be a &#8216;drop-down&#8217; or a &#8216;flyout&#8217; menu) usually consists of a trigger (the element that onmouseover reveals the menu) and a menu (the element to be shown). The code below is capable of creating menus out of nested &#8220;semantic&#8221; lists (UL, OL, DL) OR from an array of trigger/menu ID pairs depending on what you pass in as the constructor <code>menu</code> array.</p>
<p>This code requires YAHOO! User Interface Library <em>yahoo-dom-event.js</em>.</p>
<p>This article assumes you already have an HTML/CSS menu in mind. This article does not teach how to write CSS. This code simply provides an outlet for controlling show/hide functionality with javascript. The MenuCreator code basically creates a show/hide relationship between a trigger and a menu element. It&#8217;s purpose is to eliminate the nuances of mouse event handling when creating simple show/hide menus.</p>
<p><strong>Examples:</strong></p>
<ol>
<li><a href="/wp-content/uploads/2008/08/dropdowntest_example1.htm" target="_blank">Example 1 (div menu AND a nested list menu)</a></li>
<li><a href="/wp-content/uploads/2008/08/dropdowntest_example2.htm" target="_blank">Example 2 (complex example of a div menu)</a></li>
<li><a href="http://www.viewsonic.com/" target="_blank">Example 3 (used on a live site)</a></li>
</ol>
<p>The usage is simple:</p>
<ol>
<li>Once you have your CSS and HTML setup to look the way you want, add the MenuCreator code to your page:
<pre class="prettyprint">var MenuCreator = function(obj) {
	for (var oMenu in obj.menus) {
		(function(index) {
			var trigger = (obj.menus[index].trigger) ? YAHOO.util.Dom.get(obj.menus[index].trigger) : YAHOO.util.Dom.get(obj.menus[index]);
			var menu = (obj.menus[index].id) ? YAHOO.util.Dom.get(obj.menus[index].id) : YAHOO.util.Dom.get(obj.menus[index]).lastChild;

			if (!trigger || !menu || !obj.show || !obj.hide) return;

			function enterLink(e) {
				obj.show(trigger,menu);
				YAHOO.util.Event.stopEvent(e);
			}

			function exitLink(e) {
				if (!e) var e = window.event;
				if (e.stopPropagation) e.stopPropagation();
				else e.cancelBubble = true;
				var relTarg = e.relatedTarget || e.toElement;
				if (relTarg != menu) {
					obj.hide(trigger, menu);
					return false;
				}
				YAHOO.util.Event.stopEvent(e);
			}

			function exitField(e) {
				if (!e) var e = window.event;
				var tg = (window.event) ? e.srcElement : e.target;
				if (e.stopPropagation) e.stopPropagation();
				else e.cancelBubble = true;
				if (tg.nodeName != 'DIV' &amp;&amp; tg.nodeName != 'UL') return;
				var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
				while (reltg != tg &amp;&amp; reltg.nodeName != 'BODY' &amp;&amp; reltg.nodeName != 'HTML') {
					if (reltg == this) return;
					reltg = reltg.parentNode
				}
				if (reltg == tg) return;
				obj.hide(trigger, menu);
				YAHOO.util.Event.stopEvent(e);
			}

			YAHOO.util.Event.on(trigger, 'mouseover', enterLink);
			YAHOO.util.Event.on(trigger, 'mouseout', exitLink);
			YAHOO.util.Event.on(menu, 'mouseout', exitField);
		}(oMenu));
	}
};</pre>
</li>
<li>Create the menu by passing in an array of menu/trigger ID pairs and a show function and a hide function.
<pre class="prettyprint">var divMenu = new MenuCreator({
	menus: [{
		id: 'box1',
		trigger: 'tab1' },
	{
		id: 'box2',
		trigger: 'tab2'},
	{
		id: 'box3',
		trigger: 'tab3'
	}],
	show:function(trigger,menu) {
		YAHOO.util.Dom.addClass(trigger, 'hover');
		menu.style.display = "block";
	},
	hide:function(trigger,menu) {
		YAHOO.util.Dom.removeClass(trigger, 'hover');
		menu.style.display = "none";
	}
});</pre>
</li>
</ol>
<p><strong>Requires</strong><br />
<em>yahoo-dom-event.js</em><br />
Include this file by placing this above the MenuCreator code.</p>
<p><strong>FAQ</strong><br />
<em>Why provide a custom show/hide function?</em><br />
Often the specific things that need to happen on a successful show/hide differ from menu to menu, so abstracting them from the MenuCreator seemed more logical.</p>
<pre>&lt;script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js"&gt;&lt;/script&gt;</pre>
<p><em>Why is a bulky menu creator solution like this (after yahoo-dom-event.js) useful?</em><br />
Most programmers prefer to use one JavaScript library (if any) for a web site. This code is most useful for a web site that is already using YUI (like all <a href="http://pint.com" target="_blank">Pint</a> web sites, where YUI is included by default).</p>
<p><em>Why not use something like <a href="http://www.alistapart.com/articles/dropdowns" target="_blank">suckerfish</a> drop downs?</em><br />
Suckerfish drop downs only support nested lists with links in them. Suckerfish code is not a robust solution for complex HTML menus. MenuCreator offers a convenient way to deal with multiple javascript hover interfaces.</p>
<p><strong>Assumptions</strong></p>
<ol>
<li>Assumes the menus are hidden by default (using display:none, visibility:hidden, or however else you wish).</li>
<li>Assumes the show/hide events are mouseover/mouseout events.</li>
</ol>
<p><strong>Special Thanks</strong><br />
Peter Paul-Koch (quirksmode) for his <a href="http://www.quirksmode.org/js/events_mouse.html" target="_blank">mouse event</a> DOM solutions.</p>
<p>Diana Chan who wrote <a href="http://ev3.net" target="_blank">the prototype</a> for this entire snippet.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dylanbutler.com/2008/08/yui-generic-showhide-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
