
<?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>Bryce McDonnell &#124;&#124; Freelance Web Application Developer &#187; coding</title>
	<atom:link href="http://www.brycemcdonnell.com/skills-and-expert-groups/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brycemcdonnell.com</link>
	<description>bryce mcdonnell is a freelance web application developer specializing in PHP, Ruby on Rails and MySQL</description>
	<lastBuildDate>Fri, 26 Feb 2010 18:18:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mootools content slider &#8211; javascript</title>
		<link>http://www.brycemcdonnell.com/2009/03/mootools-content-slider-javascript/</link>
		<comments>http://www.brycemcdonnell.com/2009/03/mootools-content-slider-javascript/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 00:32:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Stuff I'm Working On]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[content slider]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://blog.brycemcdonnell.com/?p=77</guid>
		<description><![CDATA[
This is an old version of an improved Mootools Content Slider

I recently implemented a javascript based slider using MooTools. I&#8217;m increasingly becoming a huge fan of Mootools. Their documentation is great and the code I write just seems to be much cleaner. I don&#8217;t feel like it&#8217;s as fragile as js I&#8217;ve written in prototype/scriptaculous.
The [...]]]></description>
			<content:encoded><![CDATA[<div style="padding:20px; margin:10px; border:3px solid #FF0; background-color: #CCC">
<h2><span style="color: #000000;">This is an old version of an improved <a title="improved mootools content slider" href="http://blog.brycemcdonnell.com/improved-content-slider-in-mootools/">Mootools Content Slider</a></span></h2>
</div>
<p>I recently implemented a javascript based slider using <a href="http://mootools.net">MooTools</a>. I&#8217;m increasingly becoming a huge fan of <a href="http://mootools.net">Mootools</a>. Their <a href="http://mootools.net/docs">documentation</a> is great and the code I write just seems to be much cleaner. I don&#8217;t feel like it&#8217;s as fragile as js I&#8217;ve written in prototype/scriptaculous.</p>
<p>The content slider, which I pretty much implemented verbatim using <a href="http://woork.blogspot.com/2009/01/elegant-animated-weekly-timeline-for.html"><strong>Antonio Lupetti&#8217;s</strong></a> great tutorial at <a href="http://woork.blogspot.com/2009/01/elegant-animated-weekly-timeline-for.html">http://woork.blogspot.com/2009/01/elegant-animated-weekly-timeline-for.html</a>.</p>
<p>Antonio&#8217;s tutorial is really great. It is missing two elements which I think improve the code base:<span id="more-77"></span></p>
<p>1) you have to set the number of elements to scroll explicitly</p>
<p>2) it does not handle some crazy user clicking like mad on either the left or right buttons</p>
<p>To solve both those pains, I&#8217;ve updated the code a bit to work with MooTools 1.2 and made a couple of enhancements seen below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> totIncrement <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> increment <span style="color: #339933;">=</span> <span style="color: #CC0000;">387</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> totalIncrements <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sliderList li&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> 1
<span style="color: #003366; font-weight: bold;">var</span> maxRightIncrement <span style="color: #339933;">=</span> increment<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span> totalIncrements<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Fx.<span style="color: #660066;">Tween</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sliderList'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   property<span style="color: #339933;">:</span> <span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">,</span>
   duration<span style="color: #339933;">:</span> 1000<span style="color: #339933;">,</span>
   transition<span style="color: #339933;">:</span> Fx.<span style="color: #660066;">Transitions</span>.<span style="color: #000066;">Back</span>.<span style="color: #660066;">easeInOut</span><span style="color: #339933;">,</span>
   onStart<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slide-left&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeEvents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slide-right&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeEvents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
   onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      appEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> appEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// Previous Button</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slide-left'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">'click'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>totIncrement<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>0<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            event.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            totIncrement <span style="color: #339933;">=</span> totIncrement <span style="color: #339933;">+</span> increment<span style="color: #339933;">;</span>
            fx.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span>totIncrement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// Next Button</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slide-right'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">'click'</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>totIncrement<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>maxRightIncrement<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            event.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            totIncrement <span style="color: #339933;">=</span> totIncrement <span style="color: #339933;">-</span> increment<span style="color: #339933;">;</span>
            fx.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span>totIncrement<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
appEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By encapsulating the right and left click events into its own function, I can easily turn on and off the events to prevent the end user from invoking the slide event in the middle of an event that may already be going on. A shorter way to say that is users can&#8217;t double click.</p>
<p>Secondly, creating the totalIncrements variable allows someone (like me) to add another list element into the markup at a later date without having to remember to update the javascript code.</p>
<p>These are just a couple of tweaks to an otherwise great tutorial. Thanks a lot Antonio and keep up the good work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brycemcdonnell.com/2009/03/mootools-content-slider-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
