<?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>Darren Krape &#187; jQuery</title>
	<atom:link href="http://www.darrenkrape.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darrenkrape.com</link>
	<description>- web design and life stuff</description>
	<lastBuildDate>Mon, 16 Aug 2010 01:34:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS drop-down menus: keeping the top level selected when hovering over a sub-menu, now in jQuery</title>
		<link>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/</link>
		<comments>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 15:17:38 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[Design and Development]]></category>
		<category><![CDATA[Journal]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.darrenkrape.com/?p=233</guid>
		<description><![CDATA[Awhile back I wrote a JavaScript tutorial on how to keep the top level menu item selected when hovering over a drop down menu. Since then I&#8217;ve become a strong user and proponent of the JavaScript library jQuery. Not only is it easy to use, it also can help simplify your code-base very significantly. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile back I wrote a <a href="http://www.darrenkrape.com/fun/journal/drop-down-menus-persistent-top-level-menu-styling/">JavaScript tutorial</a> on how to keep the top level menu item selected when hovering over a drop down menu. Since then I&#8217;ve become a strong user and proponent of the JavaScript library <a href="http://www.jquery.com">jQuery</a>. Not only is it easy to use, it also can help simplify your code-base very significantly. I&#8217;ve explained the updated code below. As you can see, it is much simpler.<br />
<span id="more-233"></span></p>
<ul>
<li><a class="code_example" href="/examples/dropdown2/">View this code in action</a></li>
<li><a class="jump" href="#complete_code">View complete code</a></li>
</ul>
<h3>How this works</h3>
<p>There are two main components of the JavaScript, first the code for all browsers to keep the top level selected and a second code block to add the hover state for IE6 (since it only supports <code>:hover</code> pseudo-classes on anchors, not list elements as we use here).</p>
<p>First, you&#8217;ll need to add the jQuery equivalent of &#8220;onLoad&#8221;: <code>$(function() {</code>. Fortunately, the multiple onLoad and caching issues we addressed in the earlier tutorial are now handled in jQuery, so no need to use the &#8220;onEvent&#8221; function from the earlier tutorial. You&#8217;ll also sometimes see this as <code>$(document).ready(function() {</code>. The functionality is the same, so feel free to use either syntax.</p>
<h4>Code for persistent top-level menu formatting</h4>
<p>Next, the general code for maintaining the hover formatting on the top level. Basically, this function iterates through each of the drop-down <code>&lt;ul&gt;</code>s and adds a <code><a href="http://docs.jquery.com/Events/hover">hover</a></code> function to each. So, when a user hovers over a drop down menu, jQuery finds the parent of that drop down, finds all anchor tags, slices out all the anchors but the first and, finally, toggles the &#8220;active&#8221; class.</p>
<pre><code>$("#menu ul").each(function(i){
  $(this).hover(function(){
    $(this).parent().find("a").slice(0,1).addClass("active");
  },function(){
    $(this).parent().find("a").slice(0,1).removeClass("active");
  });
});
</code></pre>
<h4>Internet Explorer 6 functionality</h4>
<p>To make this work in IE6, we need to add some additional code. First, we use jQuery&#8217;s built in <a href="http://docs.jquery.com/Utilities/jQuery.browser">browser sniffer</a> to only target <abbr title="Microsoft Internet Explorer">MSIE</abbr> versions below 7. It then works fundimentally like the code above and the Suckerfish drop downs from the <a href="http://www.darrenkrape.com/fun/journal/drop-down-menus-persistent-top-level-menu-styling/">earlier tutorial</a>. JavaScript cycles through each drop-down <code>&lt;ul&gt;</code> and adds a function that tells the its parent anchor tag to take on a particular class when the browser detects a mouseover on the menu.</p>
<pre><code>if($.browser.msie &amp;&amp; ($.browser.version &lt; 7)) {
  $("#menu").each(function(i){
    $(this).find("li").hover(function(){
      $(this).addClass("sfhover");
    },function(){
      $(this).removeClass("sfhover");
    });
  });
}
</code></pre>
<h4>In closing</h4>
<p>Be sure to add the closing parenthesize and bracket for the jQuery on ready state: <code>});</code>.</p>
<h3 id="complete_code">Complete Code</h3>
<pre class="complete"><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;

  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
  &lt;title&gt;Menu Example&lt;/title&gt;

  &lt;style type="text/css" media="screen"&gt;

  body {
    margin: 20px;
    background: #fff;
    font-family: Verdana, Helvetica, sans-serif;
    font-size: 9pt;
  }

  * { margin: 0; padding: 0; }
  li { list-style: none; }
  a { text-decoration: none; }

  #menu li {
    float: left;
    display: block;
    font-size: 8pt;
  }

  #menu li a {
    position: relative;
    padding: 6px 10px;
    display: block;
    font-weight: bold;
    color: #333;
  }

  #menu li a:hover, #menu li a.active {
    background: #999;
    color: white;
  }

  #menu li ul {
    left : -999em;
    position : absolute;
  }

  #menu li ul li { float : none; }

  #menu li ul a {
    width : 136px;
    padding : 8px 10px;
    background : #f4f4f4;
    border-bottom : 1px solid white;
    font-weight : normal;
  } 

  #menu li ul a:hover {
    background: #FFCC99;
    color: #333;
  }

  #menu li:hover ul, #menu li.sfhover ul {
    left: auto;
    border-top: 1px solid #999;
  }

  &lt;/style&gt;

  &lt;script type="text/javascript" src="jquery.js"&gt;  &lt;/script&gt;

  &lt;script type="text/javascript" src="jquery.js"&gt;
  &lt;!--

  $(function() {

    //Preserves the mouse-over on top-level menu elements when hovering over children
    $("#menu ul").each(function(i){
      $(this).hover(function(){
        $(this).parent().find("a").slice(0,1).addClass("active");
      },function(){
        $(this).parent().find("a").slice(0,1).removeClass("active");
      });
    });

    // IE6 Fix: Drop-down fix due to lack of support for :hover on list elements
    if($.browser.msie &amp;&amp; ($.browser.version &lt; 7)) {
      $("#menu").each(function(i){
        $(this).find("li").hover(function(){
          $(this).addClass("sfhover");
        },function(){
          $(this).removeClass("sfhover");
        });
      });
    }

  });

  // --&gt;
  &lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

  &lt;ul id="menu"&gt;
    &lt;li&gt;&lt;a href="#"&gt;Styles&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Red/White&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ros?©/Blush&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sparkling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Dessert&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fortified&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Fruit&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Ice Wine&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Whites&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Albari?±o&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chardonnay&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chenin blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Muscat&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot blanc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot gris&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Riesling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sauvignon blanc &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;S?©millon&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Reds&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Cabernet Sauvignon &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Malbec &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Merlot &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Pinot noir &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Syrah/Shiraz &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Zinfandel&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Noted Regionals&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;Amarone&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Beaujolais&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Burgundy &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Chianti &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Madeira &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Port &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Sancerre &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Tokaji &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Vinho Verde &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
    &lt;li&gt;&lt;a href="#"&gt;Key Countries&lt;/a&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;France&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Italy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Spain&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;United States&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Argentina&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;Australia&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;South Africa&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;/li&gt;
  &lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.darrenkrape.com/categories/design-and-development/css-drop-down-menus-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
