<?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>nightdrops.com &#187; css</title>
	<atom:link href="http://www.nightdrops.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nightdrops.com</link>
	<description>like the color blue</description>
	<lastBuildDate>Thu, 01 Jul 2010 13:19:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Creating a Carousel using jQuery</title>
		<link>http://www.nightdrops.com/2009/creating-a-carousel-using-jquery/</link>
		<comments>http://www.nightdrops.com/2009/creating-a-carousel-using-jquery/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 20:22:05 +0000</pubDate>
		<dc:creator>kajyr</dc:creator>
				<category><![CDATA[Blog posts]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.nightdrops.com/?p=103</guid>
		<description><![CDATA[A Carousel is a component used to preview images, a little inline slideshow of thumbnails. There are quite a lot premade carousels ready to be copied and pasted if you don&#8217;t mind about, but i think that this is a nice example to start working with jQuery. Step zero: planning For the structural aspects of [...]]]></description>
			<content:encoded><![CDATA[<p>A Carousel is a component used to preview images, a little inline slideshow of thumbnails. There are quite a lot premade carousels ready to be copied and pasted if you don&#8217;t mind about, but i think that this is a nice example to start working with jQuery.</p>
<h3>Step zero: planning</h3>
<p>For the structural aspects of the carousel we will use a unordered list, masked by a fixed &#8211; width div. The list doesn&#8217;t have a maximum number of elements, we will count the and count their width with the Javascript; obiously the list should now be a traditional vertical list: we&#8217;ll handle this with css.</p>
<p>There&#8217;ll be also two clickable elements, outside the mask, to gently move the list left or right.</p>
<h3>The HTML</h3>
<p>The html is really simple: is just a list wrapped by a div. The div acts like a mask, hiding the images.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ddbb00;">&amp;lt;</span>div id=&quot;carousel&quot;<span style="color: #ddbb00;">&amp;gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Eul%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;ul&gt;</span></a></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Eli%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span> <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Eimg.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;...&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;150&quot;</span> <span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&gt;</span></a></span> <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Fli%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></a></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Eli%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span> <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Eimg.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;...&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;150&quot;</span> <span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&gt;</span></a></span> <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Fli%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></a></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Ful%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></a></span><br />
<br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Espan.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;left&quot;</span><a href="http://december.com/html/4/element/%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>left<span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Fspan%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></a></span> | <span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3Espan.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;right&quot;</span><a href="http://december.com/html/4/element/%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>right<span style="color: #009900;"><a href="http://december.com/html/4/element/%26amp%3Blt%3CSEMI%3E%2Fspan%26amp%3Bgt%3CSEMI%3E.html"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></a></span></div></div>
<p>We also have the left and right elements. They can be pretty much anything, a good graphic should do the work.</p>
<p>It is important to specify the image width attribute, because the Javascript code will need it to calculate the carousel width.</p>
<h3>The CSS</h3>
<p>The css is in charge of horizontalizing the list, and masking with the div.</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc00cc;">#carousel <span style="color: #66cc66;">&#123;</span></span><br />
        <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">hidden</span><span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">position</span><span style="color: #66cc66;">:</span><span style="color: #993333;">relative</span><span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">width</span><span style="color: #66cc66;">:</span> 500px<span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">height</span><span style="color: #66cc66;">:</span> 150px<span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">none</span><span style="color: #66cc66;">;</span><br />
    <span style="color: #66cc66;">&#125;</span></div></div>
<p>Note that we specify the height and width of the component here; we also specify position:relative, which should have really no sense, but is here only to cover a bug in IE7, just like is explained <a href="http://www.davidonzo.com/post/801/ie7-bug-overflowhidden-e-positionrelative/">here</a> (in italian)</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">    #carousel ul <span style="color: #66cc66;">&#123;</span><br />
        <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #66cc66;">:</span> 0px<span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #66cc66;">:</span> 0px<span style="color: #66cc66;">;</span><br />
        <span style="color: #000000; font-weight: bold;">position</span><span style="color: #66cc66;">:</span><span style="color: #993333;">relative</span><span style="color: #66cc66;">;</span><br />
    <span style="color: #66cc66;">&#125;</span><br />
    #carousel li <span style="color: #66cc66;">&#123;</span><br />
        <span style="color: #000000; font-weight: bold;">display</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">inline</span><span style="color: #66cc66;">;</span><br />
    <span style="color: #66cc66;">&#125;</span><br />
<br />
    #carousel img <span style="color: #66cc66;">&#123;</span><br />
        <span style="color: #000000; font-weight: bold;">border</span><span style="color: #66cc66;">:</span> 0px<span style="color: #66cc66;">;</span><br />
    <span style="color: #66cc66;">&#125;</span></div></div>
<p>This should explain by himself. The ul position is relative because we want to move it.</p>
<div id="attachment_119" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.nightdrops.com/wp-content/uploads/2009/01/carousel_step2.png"  rel="lightbox[alpha]"><img class="size-full wp-image-119" title="carousel_step2" src="http://www.nightdrops.com/wp-content/uploads/2009/01/carousel_step2.png" alt="So now you should have a non - moving carousel. Funny. Ah." width="400" height="136" /></a><p class="wp-caption-text">So now you should have a non moving carousel. Funny. Ah.</p></div>
<h3>And finally.. Javascript!</h3>
<p>Here&#8217;s where the magic happens..</p>
<p>Fist thing: importing the right files: jquery.js and the easing plugin (we want a sexy movement, not a linear one!). Remember to do this in the &lt;head&gt; tag</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #3366CC;">&quot;js/jquery-1.3.1.min.js&quot;</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #3366CC;">&quot;js/jquery.easing.1.2.js&quot;</span></div></div>
<p>Next, we will add some code (in a script tag, obiously):</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;#carousel ul img&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;width&quot;</span></div></div>
<p>What&#8217;s happening here?  We have a variable, called shift, which we will use to controll how much we want to slide the list each time; Next we bind a function to the document ready event &#8211; when all the page has finisched loading. We use the ulWidth variable to sum all the single thumbnail&#8217;s width. We initializa it to 0 and then, for each img tag in the list, we will sum the outerWidth() (and a little padding, to be nice); <a href="http://docs.jquery.com/CSS/outerWidth">outherWidth()</a> is a nice jQuery function that report the width of an element, including margins.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #3366CC;">&quot;#right&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;#carousel&quot;</span><span style="color: #3366CC;">&quot;#carousel&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #3366CC;">&quot;easeInExpo&quot;</span><span style="color: #3366CC;">&quot;#left&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;#carousel ul&quot;</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #3366CC;">&quot;easeInExpo&quot;</span></div></div>
<p>Here, we setup the onClick handlers over the left and right elements. When you&#8217;ll click one of these, the magic should happen..<br />
For each handler we read the <a href="http://docs.jquery.com/CSS/position">position().left</a> property and sum the amount that we want to scroll( the shift variable).</p>
<p>It it important to check that the right edge of the list (the left edge + the width) should stay out (on the right) of the viewport, or else we will see the list background. We don&#8217;t want to scroll when there are no more images!<br />
The same thing for the left edge, when we scroll left, or else there be space on the left of the first image.</p>
<p><strong>And&#8230; it works!</strong> Try the <a href="http://www.nightdrops.com/kj-includes/jQueryCarousel">demo</a>, or<a href="http://www.nightdrops.com/wp-content/uploads/2009/01/carousel.zip"> download the examples</a></p>
<h3>Further readings:</h3>
<p>the Carousel Pattern on the <a href="http://developer.yahoo.com/ypatterns/pattern.php?pattern=carousel">Yahoo Design Pattern Library</a> and the really good <a href="http://docs.jquery.com/Main_Page">JQuery documentation</a><br />
Credits to <a href="http://www.webdevlounge.com/javascript/creating-a-carousel-with-mootools/">webdevlonge</a> which has a really nice tutorial on creating a carousel with mootools</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nightdrops.com/2009/creating-a-carousel-using-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
