﻿<?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>Programming By A Tool &#187; Menu</title>
	<atom:link href="http://byatool.com/tag/menu/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>Whelps! Left Side! Left Side! Minus 50 DKP!</description>
	<lastBuildDate>Tue, 07 Sep 2010 14:23:28 +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>jQuery Slide Menu&#8230;  Another Cause I Can Experiment</title>
		<link>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/</link>
		<comments>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 17:00:41 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Menu]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=827</guid>
		<description><![CDATA[So for no real reason at all I had it in my mind that I wanted to make a horizontal menu with jQuery that would work like that weird scrolling menu thing that Macs have. No idea what it&#8217;s called. So basically I don&#8217;t need it, have no reason for it, but damnit I&#8217;m going [...]]]></description>
			<content:encoded><![CDATA[<p>So for no real reason at all I had it in my mind that I wanted to make a horizontal menu with jQuery that would work like that weird scrolling menu thing that Macs have.  No idea what it&#8217;s called.  So basically I don&#8217;t need it, have no reason for it, but damnit I&#8217;m going to make it happen and I did it with only 3 things from <a href="http://www.jquery.com">jquery.com</a>; 1.3.2, ui 1.7.2, and jquery.timer . Now this is still rough in the sense it has no real styling but it works tried and true functionality wise.</p>
<p>The main idea is that there are two scroll arrows, one on each side, and X amount of divs. Now at the start, a certain amount of divs are hidden (global variable). When hovering over the left pager, for example, it causes one on the right to hide while one on the left appears giving the feeling of the items sliding.</p>
<p><a href="http://www.byatool.com/Hosted/LiveDemos/SlideMenu/SlideMenu.htm">EXAMPLE HERE!!11</a></p>
<p>The markup is simple, a holder with x number of elements that are &#8220;menuitems&#8221; and two pager divs.</p>
<pre>&lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="mainHolder"</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="leftPager green floatLeft"</span>&gt;&lt;&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;1&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft red"</span>&gt;2&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft yellow"</span>&gt;3&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;4&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft red"</span>&gt;5&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft yellow"</span>&gt;6&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="menuItem floatLeft blue"</span>&gt;7&lt;/<span style="color: #800000;">div</span>&gt;
  &lt;<span style="color: #800000;">div</span> <span style="color: #ff0000;">class</span><span style="color: #0000ff;">="rightPager green floatLeft"</span>&gt;&gt;&lt;/<span style="color: #800000;">div</span>&gt;
&lt;/<span style="color: #800000;">div</span>&gt;</pre>
<p>I think from the classes you can tell what you need to know about them.</p>
<p>First thing we need from jQuery is methods to find various elements in the container when paging.</p>
<pre><span style="color: #008000;">//When using the left pager, it's important to find the first visible element
</span><span style="color: #008000;">//then find the item before it so that it can be shown.</span>
<span style="color: #0000ff;">function</span> getNextInLineBack(menuItems)
{
  <span style="color: #0000ff;">var</span> oneBefore = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>) &amp;&amp; loopCounter &gt; 0)
    {
      oneBefore = jQuery(menuItems[loopCounter - 1 ]);
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> oneBefore;
}

<span style="color: #008000;">//Find the first visible element from the beginning.
</span><span style="color: #008000;">//This will be needed when paging right since it will have to be hidden</span>
<span style="color: #0000ff;">function</span> getFirstVisible(menuItems)
{
  <span style="color: #0000ff;">var</span> firstVisible = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>) &amp;&amp; loopCounter &lt; menuItems.length - maximumToShow)
    {
        firstVisible = menuItems[loopCounter];
        <span style="color: #0000ff;">break</span>;
    }
  }
  <span style="color: #0000ff;">return</span> firstVisible;
} 

<span style="color: #008000;">//Get the last possible visible item
</span><span style="color: #008000;">/</span><span style="color: #008000;">/If the item is in an index less than the maximum number to show, then null is returned since there has to be no more or less than the maximumToShow.
</span><span style="color: #0000ff;">function</span> getLastVisible(menuItems)
{
  <span style="color: #0000ff;">var</span> lastVisible = <span style="color: #0000ff;">null</span>;
  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = menuItems.length - 1; loopCounter &gt; maximumToShow - 1; loopCounter--)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>))
    {
      lastVisible = menuItems[loopCounter];
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> lastVisible;
}

<span style="color: #008000;">//Find the first visible from the end
</span><span style="color: #008000;">//Pretty simple, this will be important when paging left since this</span>
<span style="color: #008000;">//will be the next item to be hidden</span>
<span style="color: #0000ff;">function</span> getNextInLineFront(menuItems)
{
  <span style="color: #0000ff;">var</span> lastOne = <span style="color: #0000ff;">null</span>;

  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = menuItems.length-1; loopCounter &gt; -1; loopCounter--)
  {
    <span style="color: #0000ff;">if</span>(jQuery(menuItems[loopCounter]).is(<span style="color: #ff0000;">":visible"</span>))
    {
      lastOne = menuItems[loopCounter + 1];
      <span style="color: #0000ff;">break</span>;
    }
  }

  <span style="color: #0000ff;">return</span> lastOne;
}</pre>
<p>Next is a method that is just used to stop having to repeat the same thing over and over when needing a list of all the menu items.</p>
<pre><span style="color: #0000ff;">function</span> getMenuItems(mainHolder)
{
  <span style="color: #0000ff;">return</span> jQuery(mainHolder).children(<span style="color: #ff0000;">".menuItem"</span>);
}</pre>
<p>Next is the method to handle what item to show and what item to hide when the pager has the mouse over it. Instead of having methods for the right and left pager, I just ended up having the methods for finding the item to hide and show sent through as parameters.</p>
<pre><span style="color: #0000ff;">function</span> showHideOnHover(pager, timer, getHideMethod, getShowMethod)
{
  <span style="color: #008000;">//This is just candy for changing the color of the pager when the mouse</span>
  <span style="color: #008000;">//is over it</span>
  jQuery(pager).removeClass(<span style="color: #ff0000;">"green"</span>);
  jQuery(pager).addClass(<span style="color: #ff0000;">"orange"</span>);

  <span style="color: #008000;">//Remember those methods I passed through, well here they</span>
  <span style="color: #008000;">//are in use.  I'm using them to get the item to hide and the item</span>
  <span style="color: #008000;">//to show along with the list of items.</span>
  <span style="color: #0000ff;">var</span> menuItems = getMenuItems(jQuery(pager).parent());
  <span style="color: #0000ff;">var</span> hide = getHideMethod(menuItems);
  <span style="color: #0000ff;">var</span> show = getShowMethod(menuItems);

  <span style="color: #008000;">//If neither is null, then go ahead and show/hide</span>
  <span style="color: #008000;">//If either one is null, something isn't right and the timer</span>
  <span style="color: #008000;">//needs to be stopped.... timer??  Well I'll get to that</span>
  <span style="color: #008000;">//next.</span>
  <span style="color: #0000ff;">if</span>(show != <span style="color: #0000ff;">null</span> &amp;&amp; hide != <span style="color: #0000ff;">null</span>)
  {
    jQuery(hide).hide( <span style="color: #ff0000;">"slide"</span>, { direction: <span style="color: #ff0000;">"right"</span> } , 0);
    jQuery(show).show( <span style="color: #ff0000;">"slide"</span>, { direction: <span style="color: #ff0000;">"left"</span> } , 100);
  }
  else
  {
    timer.stop();
  }
}</pre>
<p>Now for the method above the last one, this one involves the timer passed in the last method. This method actually sets the mouseover/mouseout events (aka hover). When mouseover, the timer is created and the showHideOnHover method is called every 500 units, that&#8217;s we&#8217;ll call tools, (Not sure how much that is, seems like a half second) after the first time it&#8217;s called. On mouseout, the timer is stopped, nulled out, and the pager changes it&#8217;s color.</p>
<pre><span style="color: #0000ff;">function</span> setPager(pager, hideMethod, showMethod)
{
  <span style="color: #008000;">//Making the timer variable "global" to the events so that I know
</span>  <span style="color: #008000;">//I have the same timer for both mouseover and mouseout.</span>
  <span style="color: #0000ff;">var</span> newTimer;
  pager.hover
  (
    <span style="color: #008000;">//Mouseover method
</span>    function()
    {
      var first = true;
      <span style="color: #008000;">//This sets the timer, consequently starting the method for the first time.
</span>      <span style="color: #008000;">//Why timer doesn't have a start method I don't know.  Ask jquery.com.
</span>      <span style="color: #008000;">//The first thing is just so that the first time around it runs right away,
</span>      <span style="color: #008000;">//then each call afterwards comes every 500 tools.
</span>      newTimer = jQuery.timer
                      (
                         0, <span style="color: #008000;">//First time through, runs after 0 tools.</span>
                         function(timer)
                         {
                           showHideOnHover(pager, timer, hideMethod, showMethod);
                           <span style="color: #008000;">//If this is the first time through, reset</span>
                           <span style="color: #008000;">//timer to run every 500 tools.</span>
                           <span style="color: #0000ff;">if</span>(first)
                           {
                              timer.reset(500);
                              first = <span style="color: #0000ff;">false</span>;
                           }
                         }
                       );
    },

    <span style="color: #008000;">//Mouseout method
</span>    <span style="color: #0000ff;">function</span>()
    {
      <span style="color: #008000;">//mouse is done, stop the timer</span>
      newTimer.stop();
      newTimer = <span style="color: #0000ff;">null</span>;
      jQuery(pager).addClass(<span style="color: #ff0000;">"green"</span>);
      jQuery(pager).removeClass(<span style="color: #ff0000;">"orange"</span>)
    }
  );
}</pre>
<p>Now for the method above the one&#8230; above. This is used to set the children of the passed in holder.</p>
<pre><span style="color: #0000ff;">function</span> setChildrenDivs(mainHolder)
{
  <span style="color: #008000;">//Get the items for the holder
</span>  <span style="color: #0000ff;">var</span> menuItems = getMenuItems(mainHolder);

  <span style="color: #008000;">//Hide all the items after the first X items (maximumToShow)</span>
  <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; menuItems.length; loopCounter++)
  {
    if(loopCounter &gt; maximumToShow - 1)
    {
      jQuery(menuItems[loopCounter]).hide();
    }
  }

  <span style="color: #008000;">//set the pagers.</span>
  setPager(jQuery(mainHolder).children(<span style="color: #ff0000;">".leftPager"</span>), getLastVisible, getNextInLineBack);
  setPager(jQuery(mainHolder).children(<span style="color: #ff0000;">".rightPager"</span>),getFirstVisible, getNextInLineFront);
}</pre>
<p>FINALLY THE END! This is the document.ready method used to set this whole joke in motion. maximumToShow is just how many items to show at a time and is global.</p>
<pre><span style="color: #0000ff;">var</span> maximumToShow = 5;

jQuery(document).ready
(
  <span style="color: #0000ff;">function</span>()
  {
    <span style="color: #008000;">//Find every holder on the page and set everything in motion.
</span>    <span style="color: #0000ff;">var</span> mainHolders = jQuery(<span style="color: #ff0000;">".mainHolder"</span>);
    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> loopCounter = 0; loopCounter &lt; mainHolders.length; loopCounter++)
    {
      setChildrenDivs(mainHolders[loopCounter]);
    }
  }
);</pre>
<p>Why the timer? If you haven&#8217;t figured that out yet, well it&#8217;s because I had issues with how to get the menu to keep doing it&#8217;s thing as long as the user had his/her mouse over a pager. I didn&#8217;t want this to be a click menu because, let&#8217;s be honest, that would be much easier. So as is, the timer is started the moment the mouse is over a pager and hides/shows an item. Then every 500 tools the mouse is over the pager, it continues the hide/show until it runs out of items to show/hide. (End of the list)</p>
<p>Uhg that&#8217;s annoying to type out even with cut and paste so <a href="http://www.iamwebproject.com/BAT/SlideMenu.zip">I will host it here</a>.</p>
<p>I suppose the next part of this would have the items blow up or something when hovering over them but that should be much easier than this was.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/ui/jquery-slide-and-pairing-a-divs-hover-with-a-divs-slide/" title="jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide">jQuery Slide and Pairing a Div&#8217;s Hover With a Div&#8217;s Slide</a></li><li><a href="http://byatool.com/ui/aspnet-mvc-jquery-json-and-paging-hows-that-for-a-soe-title/" title="ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?">ASP.Net MVC, jQuery, JSon, and paging&#8230;  how&#8217;s that for a SOE title?</a></li><li><a href="http://byatool.com/ui/556/" title="jQuery: Add a Pop Up Div to a Link Dynamically">jQuery: Add a Pop Up Div to a Link Dynamically</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
