<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>decodeURI &#187; carousel</title>
	<atom:link href="http://decodeuri.com/tag/carousel/feed/" rel="self" type="application/rss+xml" />
	<link>http://decodeuri.com</link>
	<description>blog of Luciano Germán Panaro</description>
	<lastBuildDate>Sat, 24 Apr 2010 12:41:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='decodeuri.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/f9eef72031f2217eea8619b16256eea0?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>decodeURI &#187; carousel</title>
		<link>http://decodeuri.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://decodeuri.com/osd.xml" title="decodeURI" />
	<atom:link rel='hub' href='http://decodeuri.com/?pushpress=hub'/>
		<item>
		<title>Update: Creating a news carousel with jQuery, now with time based switching</title>
		<link>http://decodeuri.com/2009/01/11/update-creating-a-news-carousel-with-jquery-now-with-time-based-switching/</link>
		<comments>http://decodeuri.com/2009/01/11/update-creating-a-news-carousel-with-jquery-now-with-time-based-switching/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 16:48:23 +0000</pubDate>
		<dc:creator>lucianopanaro</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[carousel]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.decodeuri.com/?p=72</guid>
		<description><![CDATA[This is just a simple and quick update on the Creating a news carousel with jQuery post. After reading this comment, and going through the code, I decided to implement the time-based switching functionality and also clean up the code a little bit (check it out here). The additions made (along with some code cleaning) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=decodeuri.com&amp;blog=2195581&amp;post=72&amp;subd=lucianopanaro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is just a simple and quick update on the <a title="Creating a news carousel with jQuery" href="http://www.decodeuri.com/2008/10/05/creating-a-news-carousel-with-jquery" target="_blank">Creating a news carousel with jQuery</a> post.</p>
<p>After reading <a href="http://www.decodeuri.com/2008/10/05/creating-a-news-carousel-with-jquery/comment-page-1/#comment-71" target="_blank">this comment</a>, and going through the code, I decided to implement the time-based switching functionality and also clean up the code a little bit (<a href="http://www.decodeuri.com/samples/carousel_with_time_based_switching.html" target="_blank">check it out here</a>).</p>
<p>The additions made (along with some code cleaning) were:</p>
<ul>
<li>Append a simple div that will shrink while the picture is shown and reinitialized when the picture is switched.</li>
<li>Add a <strong>setInterval</strong> call that will do the picture switching (and the new div&#8217;s animation).</li>
</ul>
<p><span style="text-decoration:underline;"><strong>Update 01/12:</strong></span> I added some fixes to the code</p>
<ul>
<li>Use the image&#8217;s load event to calculate each individual width. When all images are loaded, then the carousel is initiated.</li>
<li>The <strong>animate_timer</strong> function now stops all animations on the timer div before reinitializing the animation</li>
</ul>
<p><span style="text-decoration:underline;"><strong>Update 01/27:</strong></span> Even more fixes <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li>Work with cases were images are already in cache and load event is fired before we attach to it.</li>
<li>Fixed the way the news animation was calculated.</li>
<li>Added 2 more news to help test it better.</li>
</ul>
<p>So here&#8217;s the new javascript that will do this:</p>
<pre class="brush: jscript;">
$(function() {
    var carousel   = $('#news_carousel');
    var news       = carousel.find('ul.news');
    var controls   = null; // Will hold the ul with the controls
    var timer      = null; // Will hold the timer div
    var wait       = 5000; // Milliseconds to wait for auto-switching
    var widths     = [];   // Will hold the widths of each image
    var items_size = news.find('li').length;
    var initialized = false;

    if (!items_size) { return; }

    // Controls html to append
    var controls_str = '&lt;ul class=&quot;controls&quot;&gt;';
    for ( var i = 1; i &amp;lt;= items_size; i++) {
       controls_str += &#39;&lt;li&gt;&lt;a href=&quot;#&quot;&gt;' + i + '&lt;/a&gt;&lt;/li&gt;';
    }
    controls_str += '&lt;/ul&gt;';

    // Cache the controls list
    controls = carousel.append(controls_str).find('ul.controls');

    // Make the first button in controls active
    controls.find('li:first a').addClass('active');

    // Hook to the controls' click events
    controls.find('li a').click(function(event) {
      move_news( $(this) );
      return false;
    });

    // Append the timer and cache it
    timer = carousel.append('&lt;div class=&quot;timer&quot;&gt;&lt;/div&gt;').find('div.timer');

    // Store each item's width and calculate the total width
    news.find('li img')
        .each(function(i, e) {
            widths[i] = $(e).width();
            if ( all_images_loaded() ) { init_carousel(); }
        })
        .load(function(e) {
            var i = news.find('li img').index(this);
            widths[i] = $(this).width();
            if ( all_images_loaded() ) { init_carousel(); }
        });

    function all_images_loaded() {
      return (items_size == widths.length) &amp;amp;&amp;amp; (jQuery.inArray(0, widths)  1 ) {
        return false;
      }

      var current_active = controls.find('li a.active');

      if (new_active == 'next') {
        var next = current_active.parent().next().find('a');

        if ( !next.length ) { next = controls.find('li:first a'); }

        new_active = next;
      }

      var current_index = parseInt(current_active.text(), 10) - 1;
      var new_index     = parseInt(new_active.text(), 10) - 1;
      var move_to       = new_index - current_index;

      if (!move_to) { return false; }

      var direction = (move_to &amp;gt; 0)? '-=': '+=';

      var move   = 0;
      var bottom = Math.min(current_index, new_index);
      var top    = Math.max(current_index, new_index);

      while (bottom &amp;lt; top) {
        move += widths[bottom];
        bottom++;
      }

      news.animate({marginLeft: direction + move }, 500);
      new_active.addClass(&#39;active&#39;);
      current_active.removeClass(&#39;active&#39;);
    }

    function animate_timer() {
      timer.stop().css({width: &#39;100px&#39;}).animate({width: &#39;1px&#39;}, wait);
    }

    // Initializer, called when all images are loaded
    function init_carousel() {
      if (initialized) { return false; }

      // Set the news ul total width
      var width = 0;
      for( var i = 0; i &amp;lt; widths.length; i++) { width += widths[i]; }
      news.width(width);

      // Make the news change every X seconds
      setInterval(function() { move_news(&#39;next&#39;); animate_timer(); }, wait);
      animate_timer();

      initialized = true;
    }
});
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucianopanaro.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucianopanaro.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucianopanaro.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=decodeuri.com&amp;blog=2195581&amp;post=72&amp;subd=lucianopanaro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://decodeuri.com/2009/01/11/update-creating-a-news-carousel-with-jquery-now-with-time-based-switching/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d4558687335fd7de57eb9a2786fb90?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucianopanaro</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a news carousel with jQuery</title>
		<link>http://decodeuri.com/2008/10/05/creating-a-news-carousel-with-jquery/</link>
		<comments>http://decodeuri.com/2008/10/05/creating-a-news-carousel-with-jquery/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 19:54:30 +0000</pubDate>
		<dc:creator>lucianopanaro</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[carousel]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.decodeuri.com/?p=18</guid>
		<description><![CDATA[Last week I had to do a news carousel for a project I&#8217;m developing. It had been a while since I had the chance to do something interesting with jQuery, so I wanted to share the experience of how easily you can build similar widgets for your site. So first let&#8217;s take a look at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=decodeuri.com&amp;blog=2195581&amp;post=18&amp;subd=lucianopanaro&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week I had to do a news carousel for a project I&#8217;m developing. It had been a while since I had the chance to do something interesting with jQuery, so I wanted to share the experience of how easily you can build similar widgets for your site.</p>
<p>So first let&#8217;s take a look at <a href="http://www.decodeuri.com/samples/carousel.html" target="_blank">what we want to build</a>.</p>
<p>Now, I know that there are a few plugins out there for jQuery that probably can do this, but the point of this post is to show how simple it is to create something like this with a few lines of jQuery and CSS.</p>
<p>Let&#8217;s begin by defining how we will organize the content. Being a list of news, we can either use an ordered or an unordered list.</p>
<pre class="brush: xml;">
&lt;div id=&quot;news_carousel&quot;&gt;
  &lt;ul class=&quot;news&quot;&gt;
    &lt;li&gt;
      &lt;img src=&quot;&quot; alt=&quot;&quot; /&gt;
      &lt;strong&gt;&lt;a href=&quot;#&quot;&gt;Title&lt;/a&gt;&lt;/strong&gt;
      &lt;span&gt;Description&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>Now that we have our content, we have to style it. The keys here are to:</p>
<ul>
<li>Align the list elements one next to the other.</li>
<li>Make <em>#news_carousel</em> just show one list element at a time</li>
<li>Use relative and absolute positioning to show the titles and descriptions over each image</li>
</ul>
<p>Here&#8217;s the CSS used in the sample with some comments:</p>
<pre class="brush: css;">
 #news_carousel {
     width: 444px;
     height: 333px;
     margin: 0;
     padding: 0;
     overflow: hidden;  /* this will make only show 1 li */
     position: relative;
  }
  #news_carousel ul.news {
    list-style-type: none;
    margin: 0;
    padding: 0;
    position: relative;
  }
  #news_carousel ul li {
    margin: 0;
    padding: 0;
    position: relative; /* so that we can do absolute positioning of the paragraph inside of it */
    float: left; /* align one next to the other */
  }
  #news_carousel ul.news li p {
    position: absolute;
    bottom: 10px;
    left: 0;
    margin: 5px;
  }
  #news_carousel ul.news li p strong {
    display: block;
    padding: 5px;
    margin: 0;
    font-size: 20px;
    background: #444;
  }
  #news_carousel ul.news li p span {
    padding: 2px 5px;
    color: #000;
    background: #fff;
  }
  #news_carousel ul.controls {
    position: absolute;
    top: 0px; right: 20px;
    list-style-type: none;
  }
  #news_carousel ul.controls li a {
    float: left;
    font-size: 15px;
    margin: 5px;
    padding: 2px 7px;
    background: #000;
    text-decoration: none;
    outline: none;
  }
  #news_carousel ul.controls li a.active {
    border: 2px solid #ccc;
  }
</pre>
<p>The Javascript code is pretty self-explanatory:</p>
<pre class="brush: jscript;">
var news_carousel = function() {
    var items_size = $('#news_carousel ul li').length;

    if (items_size == 0) return;

    // Calculate the total width and set that value to the ul.news width
    // Store each item width
    var width = 0;
    var widths = [];
    $('#news_carousel ul.news li img').each(function(i, e) {
      widths[i] = $(e).width();
      width += widths[i];
    });

    $(&quot;#news_carousel ul.news&quot;).width(width);

    // Append the controls
    controls = '&lt;ul class=&quot;controls&quot;&gt;&lt;li&gt;&lt;a class=&quot;active&quot; href=&quot;#&quot;&gt;1&lt;/a&gt;';
    for ( var i = 2; i &amp;lt;= items_size; i++) {
       controls += &#039;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#&quot;&gt;' + i + '&lt;/a&gt;&lt;/li&gt;';
    }
    controls += '&lt;/ul&gt;';
    $('#news_carousel').append(controls);

    $('#news_carousel ul.controls li a').click(function(event) {
      // if the ul is already moving, then do nothing
      if ($(&quot;#news_carousel ul.news:animated&quot;).length &amp;gt; 0) return false;

      var clicked_item = $(event.target);
      var current_active = $(&quot;#news_carousel ul.controls li a.active&quot;);
      var current_index = parseInt(current_active.text());
      var new_index = parseInt(clicked_item.text());
      var move = new_index - current_index; // get how many items it should be moved

      if (move != 0) {
        direction = (move &amp;gt; 0)? &quot;-=&quot;: &quot;+=&quot;;
        $('#news_carousel ul.news').animate({marginLeft: direction + widths[new_index-1] }, 300);
        clicked_item.addClass(&quot;active&quot;);
        current_active.removeClass(&quot;active&quot;);
      }

      return false;
    });
  }();
</pre>
<p>And that&#8217;s it! Around 100 lines of code and you have your own home-made news carousel. Hope you found it useful! <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(Pictures taken from: <a href="http://www.flickr.com/photos/christing/268490607/">http://www.flickr.com/photos/christing/268490607/</a> and <a href="http://www.flickr.com/photos/11717181@N02/1170861540/">http://www.flickr.com/photos/11717181@N02/1170861540/</a>.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucianopanaro.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucianopanaro.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucianopanaro.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=decodeuri.com&amp;blog=2195581&amp;post=18&amp;subd=lucianopanaro&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://decodeuri.com/2008/10/05/creating-a-news-carousel-with-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/55d4558687335fd7de57eb9a2786fb90?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucianopanaro</media:title>
		</media:content>
	</item>
	</channel>
</rss>