<?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>Musings of ErisDS &#187; Code</title>
	<atom:link href="http://erisds.co.uk/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://erisds.co.uk</link>
	<description>Web development, Symfony, Wordpress and general geekery</description>
	<lastBuildDate>Thu, 29 Jul 2010 11:02:52 +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>Getting Rid of Non Breaking Spaces (&amp;nbsp)</title>
		<link>http://erisds.co.uk/code/getting-rid-of-non-breaking-spaces-nbsp</link>
		<comments>http://erisds.co.uk/code/getting-rid-of-non-breaking-spaces-nbsp#comments</comments>
		<pubDate>Tue, 13 Jul 2010 12:12:54 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=1322</guid>
		<description><![CDATA[Have you ever tried to parse, process or preg_replace some HTML? Ever tried to do it when the HTML is UTF-8 encoded? Getting rid of white space can be tricky, here's a few tricks I've learned.


Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/symfony/symfony-new-static-text-management-plugin' rel='bookmark' title='Permanent Link: Symfony: New Static Text Management Plugin'>Symfony: New Static Text Management Plugin</a><small>Generally, when I find something interesting I use twitter &...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Have you ever tried to parse, process or preg_replace some HTML? Ever tried to do it when the HTML is UTF-8 encoded? Getting rid of white space can be tricky, here&#8217;s a few tricks I&#8217;ve learned.</p>
<p>I was playing around with debugging some HTML output from <a href="http://erisds.co.uk/category/symfony" title="Other articles about Symfony">Symfony</a> the other day. The HTML was dynamic content added through <a href="http://tinymce.moxiecode.com/">TinyMCE</a> and I needed to clean superfluous formatting so that the content output looked right. I hate regular expressions and this is the first time I&#8217;ve really needed to delve into them properly, so I totally hit a stumbling block when trying to get rid of &amp;nbsp; and other bits of whitespace.</p>
<p>I tried various methods of decoding the content first, to no avail. It turns out the answer is to look for the encoded characters. I found it quite tricky to discover what the encoded values for the white space were and also to detect exactly what white space characters were present, but hopefully these two tips will help you.</p>
<h2>UTF-8 representations of white space characters</h2>
<p>The following are common white space characters. If you&#8217;re missing a few, try out <a title="File Format Character Reference" href="http://www.fileformat.info/info/unicode/char/search.htm" target="_blank">File Format for finding character codes</a>. </p>
<blockquote><p>
\x20 &#8211; The standard space or &#8216;\s&#8217;<br />
\xC2\xA0 &#8211; The non-breaking space &#8216;&amp;nbsp;&#8217;<br />
\x0D &#8211; Carriage Return or &#8216;\r&#8217;<br />
\x0A &#8211; New Line or &#8216;\n&#8217;<br />
\x09 &#8211; The tab or &#8216;\t&#8217;
</p></blockquote>
<h2>Discovering which characters are present</h2>
<p>Figuring out exactly what white space characters are present in your encoded HTML can be tricky. I used <a href="http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm">XVI32</a>, which is a hex editor. If you view the source of the HTML you are trying to clean and copy &#038; paste an offending section into the right-hand window it will show the encoded characters in the left hand window.  </p>
<p><img src="http://erisds.co.uk/wp-content/uploads/2010/07/xvi32.png" alt="" title="xvi32" width="560" height="411" class="aligncenter size-full wp-image-1471" /></p>
<p>Hopefully this post might save you some time. If you have a similar problem that isn&#8217;t covered here, I&#8217;d be interested to know so <a href="#respond-box" title="leave a comment">drop me a comment!</a></p>
<p><ins datetime="2010-07-19T19:29:28+00:00"><strong>Update:</strong> I thought it would be helpful to add an example of how I used these character codes. This simple function replaces all encoded non-breaking spaces with a normal space. It&#8217;s only a small snippet, but it might be of use ;).</ins></p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> cleanNonBreakingSpaces<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\xC2\xA0/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You could replace the space with an encoded &#8220;normal&#8221; space character, although I&#8217;m not sure how well that will work:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\xC2\xA0/'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'\x20'</span><span style="color: #339933;">,</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Or you could check for both encoded or non-encoded non-breaking spaces:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(\xC2\xA0/|&amp;nbsp;)'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h3>Resources</h3>
<ul>
<li><a title="File Format Character Reference" href="http://www.fileformat.info/info/unicode/char/search.htm" target="_blank">File Format</a> &#8211; An excellent site for looking up character codes.</li>
<li><a title="XVI32 Freeware Hex Editor" href="http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm">XVI Freeware Hex Editor</a> &#8211; A great tool for discovering hidden characters.</li>
</ul>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/symfony/symfony-new-static-text-management-plugin' rel='bookmark' title='Permanent Link: Symfony: New Static Text Management Plugin'>Symfony: New Static Text Management Plugin</a><small>Generally, when I find something interesting I use twitter &...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/getting-rid-of-non-breaking-spaces-nbsp/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>HTML Canvas: Going Ninja with .Net Magazine&#8217;s Processing.js tutorial</title>
		<link>http://erisds.co.uk/code/html-canvas-going-ninja-with-net-magazines-processing-js-tutorial</link>
		<comments>http://erisds.co.uk/code/html-canvas-going-ninja-with-net-magazines-processing-js-tutorial#comments</comments>
		<pubDate>Sun, 23 May 2010 16:45:50 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=1072</guid>
		<description><![CDATA[<a href="http://erisds.co.uk/resources/html-5-canvas-ninja-mini-game-demo"><img class="alignleft size-medium wp-image-1155" title="HTML 5 Canvas: Ninja mini-game demo" src="http://erisds.co.uk/wp-content/uploads/2010/05/ninja-300x224.png" alt="" width="300" height="224" /></a>Yesterday I finally got around to sitting down with the Canvas and Processing.js tutorial in last month's issue of .Net Magazine (Issue 202, page 82). I've been wanting to play with Canvas since the HTML5 special of the <a title="ExplicitWeb" href="http://explicitweb.co.uk" target="_blank">ExplicitWeb podcast</a> we did back in March (subtle plug) and this tutorial seemed like a good way of getting started.


Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/wordpress/wordpress-2-8' rel='bookmark' title='Permanent Link: WordPress 2.8'>WordPress 2.8</a><small>Over the weekend I updated my blog to Wordpress 2.8....</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://erisds.co.uk/resources/html-5-canvas-ninja-mini-game-demo"><img class="alignleft size-medium wp-image-1155" title="HTML 5 Canvas: Ninja mini-game demo" src="http://erisds.co.uk/wp-content/uploads/2010/05/ninja-300x224.png" alt="" width="300" height="224" /></a>Yesterday I finally got around to sitting down and playing with the Canvas and Processing.js tutorial in last month&#8217;s issue of .Net Magazine (Issue 202, pages 82-85). I have been wanting to try my hand at HTML5 Canvas since Rob got me interested during the HTML5 special of the <a title="ExplicitWeb" href="http://explicitweb.co.uk" target="_blank">ExplicitWeb podcast</a> we recorded back in March (note the subtle plug) and this tutorial seemed like a good way of getting stuck in.</p>
<h3>Getting it working in Google Chrome (or not)</h3>
<p>Getting started, I fired up a text editor and did step one only to find that nothing appeared in Google Chrome but Firefox was working perfectly. I was certain Google Chrome supported canvas, <a title="Wikipedia: Canvas Element" href="http://en.wikipedia.org/wiki/Canvas_element" target="_blank">Wikipedia</a> said so, this <a title="HTML5 browser readiness visualisation" href="http://html5readiness.com/" target="_blank">awesome visualisation of HTML5 readiness</a> said so, hell even <a title="Processing JS homepage" href="http://processingjs.org/" target="_blank">the Processing.js homepage</a> said so, so it wasn&#8217;t even that Processing JS was the problem. After pouring over the code and a bit of Googling I stumbled across <a href="https://processing-js.lighthouseapp.com/projects/41284/tickets/418-2d-sketches-which-use-xhr-fail-to-load-on-chromium " target="_blank">this ticket on the Processing JS website</a>, which basically points to a <a title="Google Chrome ajax bug" href="http://code.google.com/p/chromium/issues/detail?id=23429" target="_blank">bug in Google Chrome</a> as a the source of the problem, but only in certain versions.</p>
<p>It turns out that processing adds a <kbd>data-src</kbd> attribute to the Canvas element, which is then used to pull in the Processing JS code via ajax. Some bug in Google Chrome means the ajax call fails, and therefore nothing happens. The work around is to include all your processing code on the page inside script tags, and use the <kbd>processing.init.js</kbd> script to load it in:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">...<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;processing.init.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span> <br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;application/processing&quot;</span>&gt;</span><br />
&nbsp; //processing code here...<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;canvas&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span><br />
...</div></td></tr></tbody></table></div>
<p>You can see an example of Processing JS done this way by viewing the full source of the <a href=" http://processingjs.org/source/basic-example/processingjs_basic-example.html">basic demo</a> on the Processing JS homepage. This works really well, except for the drawback of having all your Processing code inline on the page, which doesn&#8217;t play too well with WordPress. Therefore, the <a href="http://erisds.co.uk/resources/html-5-canvas-ninja-mini-game-demo" target="_blank">game demo page</a> doesn&#8217;t work in all versions of Google Chome (particularly not in the latest standard release but it does work in the latest beta release).</p>
<h3>Playing with some crazy eyes</h3>
<p>The problem with coding tutorials, I always find, is that you end up just copying and pasting without truly understanding the nuances of what you are doing: What does that function do? Which argument is which and what are they for? Why&#8217; is the code in that order? This was especially true with this Processing tutorial with it&#8217;s smattering of trigonometry &amp; use of functions like <kbd>ellipse</kbd> or <kbd>arc</kbd> with no description of the arguments.The only cure is to get stuck in.</p>
<p>The first thing I decided I wanted to do, was change the way the eyes move. It seems wrong to me that they remain equi-distant even when they are to the far left and right, I wanted to make them get closer together. The ninja&#8217;s head, body and eyes are all fixed on the y axis &#8211; they only move left-to-right so I figured I could do this quite easily.</p>
<p>The arc function used to draw the eyes takes 6 arguments: x position, y position, width, height, start angle, stop angle. Therefore to change the distance between the eyes I needed to change the first argument so that the way the x position of the left and right eye relate to each other is different based on the mouse&#8217;s position.</p>
<p>I changed this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">arc<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">10</span><span style="color: #339933;">-</span>sin<span style="color: #009900;">&#40;</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">7</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>TWO_PI<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Draw left eye</span><br />
arc<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">-</span>sin<span style="color: #009900;">&#40;</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">7</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>TWO_PI<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Draw right eye</span></div></td></tr></tbody></table></div>
<p>To this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">arc<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">10</span><span style="color: #339933;">-</span>sin<span style="color: #009900;">&#40;</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>TWO_PI<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Draw left eye</span><br />
arc<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">-</span>sin<span style="color: #009900;">&#40;</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>trackAngle<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>TWO_PI<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Draw right eye</span></div></td></tr></tbody></table></div>
<p>The first part of the x position <kbd>-10-sin(trackAngle)</kbd> placed the eyes and makes them move with the head. The modifying value (originally 7) is what makes the eyes move further than the head &#8211; so they move further left or right relative to the heads position. <kbd>trackAngle</kbd> is measured in radians and therefore ranges between -1.2 and 1.2. I multiplied this by 2 to make it more significant, and used it to change what the modifying value is. Now the eyes not only move further relative to the head, but also relative to each other.</p>
<p>This is quite a weird effect &#8211; but I&#8217;m pretty pleased with it :)</p>
<h3>Making a mini game</h3>
<p>Once I was happy with the eyes, and had gotten a good grasp of the math going on, I decided to make it even more interactive, and turn it into a little game. The little ninja&#8217;s arms flying all over the place seemed to want something to hit, so I introduced a dot which appears randomly somewhere on the screen, stays put for just over a second, and then disappears and reappears somewhere else. The dot disappears and reappears faster and faster as time goes on.</p>
<p>I added some code to the <kbd>mouseMoved</kbd> function to test if the mouse&#8217;s new position matches the dot&#8217;s position, and if so adds one to the score and forces the dot to move again. Finally I added a box and some text to display the score in the top right corner, which was interesting as the tutorial doesn&#8217;t cover text:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">void</span> drawScore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
PFont fontA <span style="color: #339933;">=</span> loadFont<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Arial&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
textFont<span style="color: #009900;">&#40;</span>fontA<span style="color: #339933;">,</span> <span style="color: #CC0000;">36</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ninjaStyle<span style="color: #009900;">&#40;</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> rect<span style="color: #009900;">&#40;</span>width<span style="color: #339933;">-</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> height<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">-</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
fill<span style="color: #009900;">&#40;</span>#CD3700<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
text<span style="color: #009900;">&#40;</span>score<span style="color: #339933;">,</span> width<span style="color: #339933;">-</span><span style="color: #CC0000;">57</span><span style="color: #339933;">,</span> height<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>height<span style="color: #339933;">-</span><span style="color: #CC0000;">41</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>So there we have it, a <a href="http://erisds.co.uk/resources/html-5-canvas-ninja-mini-game-demo" target="_blank">mini ninja fighting game</a>. You&#8217;ll need to visit the demo page to play the game as this blog isn&#8217;t (quite) yet running HTML5. </p>
<p>It doesn&#8217;t really do any of the things I&#8217;d expect a real game to (yet), like have a start / pause button, a sound track, or a leader board. It could probably do with a samurai sword whizzing around and splitting the dots in half&#8230; obviously with plenty of blood and gore. It does however get progressively more difficult, the most I&#8217;ve scored is 92. The test is to see if anyone can show me a screenshot with a score of 100 and a broken scorebox ;)</p>
<p class="small"><strong>Please note:</strong> the original code, imagery &#038; styles were all published in .Net Magazine and were created by Alistair MacDonald of <a href="http://www.bocoup.com">Bocoup.com</a></p>
<p></body></p>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/wordpress/wordpress-2-8' rel='bookmark' title='Permanent Link: WordPress 2.8'>WordPress 2.8</a><small>Over the weekend I updated my blog to Wordpress 2.8....</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/html-canvas-going-ninja-with-net-magazines-processing-js-tutorial/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</title>
		<link>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2</link>
		<comments>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2#comments</comments>
		<pubDate>Tue, 04 Aug 2009 12:28:07 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[widgets]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=692</guid>
		<description><![CDATA[<a href="http://erisds.co.uk/resources/yui-carousel-demo-2"><img src="http://erisds.co.uk/wp-content/uploads/2009/08/carousel-2.jpg" alt="YUI Carousel Example  2" title="YUI Carousel Example 2" width="350" height="257" class="alignleft size-full wp-image-749" /></a>Welcome to <strong>Part 2</strong> of my YUI Carousel series. If you haven't read it, <a href="http://erisds.co.uk/code/yui-javscript-carousel-custom-navigation-part-1">Part 1 is here</a>. <strong>Part 2</strong> uses class names to setup multiple carousel instances, and also drops the YUI CSS in favour of writing your own custom css. The final <strong>Part 3</strong> will show you how to write your own completely custom navigation for the carousel.

If you haven't already read the first part of this tutorial, I suggest that you do. If you want to follow along, then please setup your workspace with a basic HTML file and somewhere to put Javascript, CSS &#038; images now, <a href="http://erisds.co.uk/resources/yui-carousel-demo-2">the demonstration page is here</a> . I'll be using the same images as last time, same rules apply! 


Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/code/html-canvas-going-ninja-with-net-magazines-processing-js-tutorial' rel='bookmark' title='Permanent Link: HTML Canvas: Going Ninja with .Net Magazine&#8217;s Processing.js tutorial'>HTML Canvas: Going Ninja with .Net Magazine&#8217;s Processing.js tutorial</a><small>Yesterday I finally got around to sitting down with the...</small></li>
<li><a href='http://erisds.co.uk/wordpress/snippet-wordpress-admin-tidy-dashboard-widgets-by-role' rel='bookmark' title='Permanent Link: Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role'>Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role</a><small>Recently I worked on a Wordpress site where I wanted...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://erisds.co.uk/resources/yui-carousel-demo-2"><img src="http://erisds.co.uk/wp-content/uploads/2009/08/carousel-2.jpg" alt="YUI Carousel Example  2" title="YUI Carousel Example 2" width="350" height="257" class="alignleft size-full wp-image-749" /></a>Welcome to <strong>Part 2</strong> of my YUI Carousel series. This part uses class names to setup multiple carousel instances, and also drops the YUI CSS in favour of writing your own custom css. The final <strong>Part 3</strong> will show you how to ditch the YUI navigation completely, so that you can use custom labels.</p>
<p>I recommend that you read <a href="http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1"><strong>Part 1</strong></a> of this tutorial if you haven&#8217;t already. If you want to follow along, then please setup your workspace with a basic HTML file and somewhere to put Javascript, CSS &#038; images now, <a href="http://erisds.co.uk/resources/yui-carousel-demo-2">the demonstration page is here</a>. I&#8217;ll be using the same images as last time, same rules apply! </p>
<h2>Getting Started &#8211; The HTML</h2>
<p>The HTML for the carousel is the same as in <a href="http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1">Part 1</a>, except this time I have added titles to my images. Below I&#8217;ve only listed one carousel, but as you can see on the <a href="http://erisds.co.uk/resources/yui-carousel-demo-2">demonstration page</a>, the setup JavaScript we&#8217;ll be writing later will detect multiple carousels on the page. To add another carousel just copy the HTML structure including the container div, change the id&#8217;s but leave the classes intact.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;carousel&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-carousel&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;carousel-container&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ol.html"><span style="color: #000000; font-weight: bold;">ol</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;carousel-content&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm3.static.flickr.com/2181/3529457733_50200b99ab.jpg&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Baby Dolphin&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>A Baby Dolphin called Bob<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3543/3514480796_2243d70d6b.jpg&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span><span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Birds&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>Feeding the birds<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3306/3503294318_c34fab9d17.jpg&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Tiger&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span>Up close and personal with a tiger<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/h2.html"><span style="color: #000000; font-weight: bold;">h2</span></a>&gt;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ol.html"><span style="color: #000000; font-weight: bold;">ol</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span></div></td></tr></tbody></table></div>
<h2>Referencing YUI</h2>
<p>You&#8217;ll need to add the same JavaScript references right above your closing body tag as last time. That is the link to the combined YUI scripts, plus your own setup JavaScript file. This time though, <strong>DON&#8217;T</strong> include the YUI CSS, just add a link to your own CSS file at the top of the page. So these are the includes you should have:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;css/carousel.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Content here --&gt;</span><br />
<br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;amp;2.7.0/build/element/element-min.js&amp;amp;2.7.0/build/animation/animation-min.js&amp;amp;2.7.0/build/carousel/carousel-min.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/carousel.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span></div></td></tr></tbody></table></div>
<h2>Setting up &#8211; Javascript</h2>
<h3>Step 1 &#8211; Locate all carousel markup on the page</h3>
<p>The first step is the same as last time, to setup our namespace and object literal. We still need an <kbd>init</kbd> function, and inside it we use the <kbd>getElementsByClassName</kbd> function to populate our <kbd>carousels</kbd> property with an array of all the elements which have the class <kbd>carousel-container</kbd>. We then loop over carousels and call a <kbd>setup</kbd> function on each element.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">YAHOO.<span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ErisDS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
YAHOO.<span style="color: #660066;">ErisDS</span>.<span style="color: #660066;">Carousel</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; carousels<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><br />
&nbsp; init<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><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousels</span> <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'carousel-container'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousels</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousels</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> ...</div></td></tr></tbody></table></div>
<h3>Step 2 &#8211; Turn carousel markup into working carousels</h3>
<p>Now we need to write our <kbd>setup</kbd> function. It takes the element that is to be a carousel as an argument. The body of the function contains the same setup code as our original <kbd>init</kbd> function from <a href="http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1">Part 1</a>, where the settings are explained.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; setup<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>carousel_el<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> carousel <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">widget</span>.<span style="color: #660066;">Carousel</span><span style="color: #009900;">&#40;</span>carousel_el<span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; autoPlayInterval<span style="color: #339933;">:</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; isCircular<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; animation<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">1.0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; numVisible<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; carousel.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; carousel.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; &nbsp; carousel.<span style="color: #660066;">startAutoPlay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</p>
<p>Finally, don&#8217;t forget to call the function using <kbd>onDOMReady()</kbd>.</p>
<p></code></p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">onDOMReady</span><span style="color: #009900;">&#40;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">ErisDS</span>.<span style="color: #660066;">Carousel</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h2>Styling up - The Custom CSS</h2>
</p>
<p>This time we aren't going to use the YUI CSS at all, so we will need to do quite a bit of work ourselves. First up I'll show you the bare minimum needed to get the carousel working. Then we will go through styling the navigation, and finally the customisations to get it looking the same as the demo page.</p>
<h3>The Bare Minimum</h3>
<p>To get the carousel working horizontally, we need to get all the items to display side-by-side. We then need to hide all but the item that should be visible using <kbd>overflow:hidden</kbd>. We also need to clear margins and padding, but we don't need to set any width or height - the carousel will just resize to fit the largest element inside of it.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.carousel</span> .carousel-container<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32000px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span> li<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #00AA00;">*</span>zoom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p><img src="http://erisds.co.uk/wp-content/uploads/2009/08/carousel-plain.jpg" alt="Carousel Plain" title="Carousel Plain" width="300" height="330" class="alignright size-full wp-image-741" /></p>
<p>I can hear people all over the interweb screaming "nooooo!" at the IE hacks. Feel free to move these to <a href="http://www.quirksmode.org/css/condcom.html">conditional comments</a>, but for the purpose of tutorials using hacks makes it easier to show what I'm doing! All that extra code (plus the <kbd>position:relative</kbd>'s) is used to get IE6 &#038; 7 to display the items side-by-side whilst still hiding all but the first item.</p>
<p>On the right you can see how the carousel looks with just this basic styling - pretty nasty! Time to get creative. I want to get rid of the text in the navigation, put the navigation at the bottom, and add some nicer buttons<strong>*</strong>.</p>
<h3>The Navigation</h3>
<p>Because I want to put the navigation at the bottom &#038; have my carousel be fixed size, I need to set <kbd>width</kbd> and <kbd>height</kbd> on <kbd>carousel-container</kbd> and on the content <kbd>li</kbd>. I'm also going to add a <kbd>border</kbd> to contain the carousel. The following is the extra CSS I added.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.carousel</span> .carousel-container<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">364px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #cc00cc;">#451669</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span> li<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">364px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>The following code uses <kbd>position:absolute</kbd> to move the navigation to the bottom. I've also added a semi-transparent background to the navigation bar, and used the <kbd>inline-block</kbd> hack again to get the <kbd>UL</kbd> to sit in the middle for all browsers. In the second part I have turned the navigation into circles using the CSS3 <kbd>border-radius</kbd> property (IE users will see squares) &#038; gotten rid of the text with positioning.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.carousel</span> .yui-carousel-nav<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">31px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/bg.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span><br />
&nbsp; zoom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">middle</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul li<span style="color: #00AA00;">&#123;</span> &nbsp;<br />
&nbsp; <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#673191</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#673191</span><span style="color: #00AA00;">;</span><br />
&nbsp; -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul li a<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-10000px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul li<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> <br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul li<span style="color: #6666ff;">.hover</span><span style="color: #00AA00;">,</span> <br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> ul li<span style="color: #6666ff;">.yui-carousel-nav-page-selected</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#b6a2c4</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>Next let's replace those ugly buttons with something else. By setting the <kbd>width</kbd>, <kbd>height</kbd> and <kbd>padding</kbd> of the <kbd>button</kbd> we can hide it. Then I've added a background image to the <kbd>span</kbd>s and absolutely positioned them. I have also been adding hover states to all the navigation, but at the moment these will only work in browsers that support the <kbd>:hover</kbd> psuedo class on elements other than the <kbd>anchor</kbd> tag.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span button<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#carousel-2</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span button<span style="color: #3333ff;">:focus</span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span.yui-carousel-first-button<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">170px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/prev-arrow.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span.yui-carousel-next-button<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">170px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/next-arrow.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span<span style="color: #6666ff;">.yui-carousel-first-button</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span><br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span.prev-hover<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/prev-arrow-hover.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span<span style="color: #6666ff;">.yui-carousel-next-button</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span><br />
<span style="color: #6666ff;">.carousel</span> <span style="color: #6666ff;">.yui-carousel-nav</span> span.next-hover<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/next-arrow-hover.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>We can fix the hover states for the previous and next buttons with some Javascript, but I haven't found a way to get this to work for the list navigation yet as it gets rewritten everytime the carousel changes and therefore it loses the listeners. To add listeners to the buttons, first add the following code to the end of your <kbd>init</kbd> function.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> nav_buttons <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'yui-carousel-button'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>nav_buttons<span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mouseover</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>nav_buttons<span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mouseout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Next, after the setup function add a comma, and then add the following two functions. All this does is add a <kbd>class</kbd> on mouseover and then remove it on mouseout, we use two different classes to make it easier to reference the buttons in IE6. This could be replaced with a sprite image &#038; CSS that just changes the background position.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; mouseover<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> add_target <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">getTarget</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//add_target = YAHOO.util.Dom.getAncestorByTagName(target,'span');</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>add_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'yui-carousel-next-button'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>add_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'next-hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>add_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'yui-carousel-first-button'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>add_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'prev-hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; mouseout<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rem_target <span style="color: #339933;">=</span> YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">getTarget</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//rem_target = YAHOO.util.Dom.getAncestorByTagName(target,'span');</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>rem_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'next-hover'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>rem_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'next-hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>rem_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">'prev-hover'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>rem_target<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;prev-hover&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h3>Headings</h3>
<p>That's it for the functionaliy. Here's the CSS I used to style my main heading. It's almost identical to the nav-bar code.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span> li h2<span style="color: #00AA00;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">28px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'../images/bg.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">repeat</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p><a href="http://erisds.co.uk/resources/yui-carousel-demo-2">On the demo page</a>, the two carousels are the same size and styled very similarly. However this is not necessary as you do not need to specifiy the size (the carousel will default to the size of the largest item within it) and you can also use <kbd>id</kbd>'s to style the two carousels entirely differently in the CSS, yet still use the same Javascript to make them work. However the functionality for the two carousels would remain the same.</p>
<p>Well I think that's a wrap!  Not every single bit of CSS I used is here, but I will be providing all my files which I publish <strong>Part 3</strong> next week some time. Hope you've found this useful &#038; reasonably easy to follow. Please drop any suggestions or ideas in the comments!</p>
<p class="small"><strong>*</strong> I said nicer, not amazing... I'm no designer!!</p>
<h3>Resources</h3>
<ul>
<li><a href="http://developer.yahoo.com/yui/docs/YAHOO.widget.Carousel.html">Visit the API for a full list of available methods, properties and attributes.</a></li>
<li><a title="YUI Dependency Configurator" href="http://developer.yahoo.com/yui/articles/hosting/">YUI Dependency Configurator</a></li>
<li><a href="http://erisds.co.uk/resources/yui-carousel-demo-2">The demo page (2)</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 1'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</a><small>Welcome to my first JavaScript related post! I'm currently in...</small></li>
<li><a href='http://erisds.co.uk/code/html-canvas-going-ninja-with-net-magazines-processing-js-tutorial' rel='bookmark' title='Permanent Link: HTML Canvas: Going Ninja with .Net Magazine&#8217;s Processing.js tutorial'>HTML Canvas: Going Ninja with .Net Magazine&#8217;s Processing.js tutorial</a><small>Yesterday I finally got around to sitting down with the...</small></li>
<li><a href='http://erisds.co.uk/wordpress/snippet-wordpress-admin-tidy-dashboard-widgets-by-role' rel='bookmark' title='Permanent Link: Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role'>Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role</a><small>Recently I worked on a Wordpress site where I wanted...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>YUI: Javascript Carousel with Custom Navigation &#8211; Part 1</title>
		<link>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1</link>
		<comments>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1#comments</comments>
		<pubDate>Tue, 28 Jul 2009 12:22:50 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[widgets]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=501</guid>
		<description><![CDATA[<a href="http://erisds.co.uk/resources/yui-carousel-demo-i"><img class="alignleft size-full wp-image-675" title="Carousel 1" src="http://erisds.co.uk/wp-content/uploads/2009/07/carousel.jpg" alt="Carousel 1" width="350" height="254" /></a>Welcome to my first JavaScript related post! I'm currently in the process of both learning *proper* JavaScript and trying to get to grips with the YUI framework. If you have suggestions for how to improve the following code I'd love to hear them.

The YUI Carousel widget is currently in Beta, and the navigation that it generates is very basic and, unlike the rest of the YUI framework, doesn't have the necessary CSS hooks to style it properly. This may change with the release of YUI 3.0, but the Carousel widget isn't included yet. So for the time being this <strong>three-part series</strong> will show you how to setup a Carousel &#38; build custom navigation.


Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/code/getting-rid-of-non-breaking-spaces-nbsp' rel='bookmark' title='Permanent Link: Getting Rid of Non Breaking Spaces (&amp;nbsp)'>Getting Rid of Non Breaking Spaces (&amp;nbsp)</a><small>Have you ever tried to parse, process or preg_replace some...</small></li>
<li><a href='http://erisds.co.uk/wordpress/snippet-wordpress-admin-tidy-dashboard-widgets-by-role' rel='bookmark' title='Permanent Link: Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role'>Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role</a><small>Recently I worked on a Wordpress site where I wanted...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://erisds.co.uk/resources/yui-carousel-demo-1"><img class="alignleft size-full wp-image-675" title="Carousel 1" src="http://erisds.co.uk/wp-content/uploads/2009/07/carousel.jpg" alt="Carousel 1" width="350" height="254" /></a>Welcome to my first JavaScript related post! I&#8217;m currently in the process of both learning *proper* JavaScript and trying to get to grips with the YUI framework. If you have suggestions for how to improve the following code I&#8217;d love to hear them.</p>
<p>The YUI Carousel widget is currently in Beta, and the navigation that it generates is very basic and, unlike the rest of the YUI framework, doesn&#8217;t have the necessary CSS hooks to style it properly. This may change with the release of YUI 3.0, but the Carousel widget isn&#8217;t included yet. So for the time being this <strong>three-part series</strong> will show you how to setup a Carousel &amp; build custom navigation.</p>
<p><strong>Part 1</strong> will go through the HTML, CSS &amp; Javascript needed to setup a basic carousel using the YUI default skin. In <strong><a href="http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2">Part 2</a></strong> we&#8217;ll remove the YUI skin and write our own, introduce multiple instances via class names and do some customisation to the carousel. Finally, in <strong>Part 3</strong> I&#8217;ll go through all of the Javascript needed to write your own, completely custom navigation for the carousel.</p>
<h2>Getting Started &#8211; The HTML</h2>
<p>If you want to follow along, then setup your workspace with a basic HTML file and somewhere to put Javascript, CSS &amp; images now. I&#8217;ll be using some photos from my recent holiday, feel free to use these images for testing purposes, but please don&#8217;t publish them as your own work! If you want to see the finished carousel in action, then please check out <a href="http://erisds.co.uk/resources/yui-carousel-demo-1">the demonstration page</a>.</p>
<p>First we need to create the HTML list &amp; containers for our carousel along with some CSS classes for both reference and styling. This should include a container <kbd>&lt;div&gt;</kbd> (with class <kbd>carousel-container</kbd>) and an ordered list to contain our carousel items (class <kbd>carousel-content</kbd>). I have also added the classes <kbd>yui-skin-sam</kbd> and <kbd>carousel</kbd> to the body tag. Apart from those prefixed with &#8220;yui&#8221; all the classes I&#8217;m using are my own conventions and are not required by YUI.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yui-skin-sam carousel&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-carousel&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;carousel-container&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ol.html"><span style="color: #000000; font-weight: bold;">ol</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;carousel-content&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3543/3514480796_2243d70d6b.jpg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Birds&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3306/3503294318_c34fab9d17.jpg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Tiger&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3583/3530272148_4902b92aee.jpg&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;zebra&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;364&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ol.html"><span style="color: #000000; font-weight: bold;">ol</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>The <kbd>yui-skin-sam</kbd> class is a reference point for the YUI CSS so that the styling can be applied. For now I have added just three items to the list, and each one contains only an image. You can also include headers, paragraphs and any other HTML content to your carousel.</p>
<h2>Referencing YUI</h2>
<p>To turn our numbered list of images into a carousel we need to include reference to the YUI CSS and Javascript. Unlike many other frameworks, rather than downloading a local copy of the framework, it is normal to reference YUI direct from the Yahoo! Servers using the <a title="YUI Dependency Configurator" href="http://developer.yahoo.com/yui/articles/hosting/">YUI Dependency Configurator</a> to work out which files you need. For now I will show you which files to add:</p>
<p>We are going to need the carousel.css file to style the list properly. Add the following stylesheet link to the head of your HTML file.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">link</span></a> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.7.0/build/carousel/assets/skins/sam/carousel.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></div></td></tr></tbody></table></div>
<p>In order for the Carousel to work properly, we will need the YUI Dom collection &amp; Event utilities, the Element utility (which provides an object to represent HTML elements), the animation utility and finally, the carousel component. They are minimised and combined by YUI and served to you as a single file. YUI is an unobtrusive framework, so add the following lines of HTML to the very bottom of your page just above the <kbd>&lt;/body&gt;</kbd> tag.</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;amp;2.7.0/build/element/element-min.js&amp;amp;2.7.0/build/animation/animation-min.js&amp;amp;2.7.0/build/carousel/carousel-min.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<h2>Setting up &#8211; Javascript</h2>
<p>Now with all of the framework components in place, we can go about the work of setting up our carousel. For now we will do this by getting the id of the carousel&#8217;s wrapping <kbd>&lt;div&gt;</kbd>, but in <strong>Part II</strong> we will create the carousel based on class name so that we might have multiple instances. </p>
<p>Create a file called carousel.js in a Javascript folder, and link to it after the YUI scripts so it appears something like this:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/carousel.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></td></tr></tbody></table></div>
<p>Be aware that we create an instance of the carousel by passing in the <strong>id of the containing <kbd>&lt;div&gt;</kbd></strong>, not the ordered list. We also need to pass in a set of properties which are explained below. I have made our carousel variable into a property of our object so that we can easily have reference from additional methods of our object when we add more functionality later.</p>
<p>Once we have setup our carousel instance, we call <kbd>render</kbd>, <kbd>show</kbd>, and <kbd>startAutoPlay</kbd> to setup, display &amp; start the carousel. Of course none of this happens until the <kbd>init</kbd> method is called. As YUI is non-intrusive the methods to setup the carousel are called after the page has finished loading using <kbd>onDomReady</kbd>. I have added a test to the <kbd>onDomReady</kbd> function to ensure that the carousel id exists before we try using it to create an instance of the carousel widget.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//Setup a namespace to contain your own code within the YAHOO namespace</span><br />
YAHOO.<span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ErisDS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//Create our carousel object literal with an init method</span><br />
YAHOO.<span style="color: #660066;">ErisDS</span>.<span style="color: #660066;">Carousel</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; carousel<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span><br />
&nbsp; init<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><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousel</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">widget</span>.<span style="color: #660066;">Carousel</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my-carousel&quot;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; autoPlayInterval<span style="color: #339933;">:</span> <span style="color: #CC0000;">5000</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; isCircular<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; animation<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.5</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; numVisible<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousel</span>.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousel</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #006600; font-style: italic;">// display the widget</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">carousel</span>.<span style="color: #660066;">startAutoPlay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//onDomReady check to see if our carousel exists, and call the setup function</span><br />
YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Event</span>.<span style="color: #660066;">onDOMReady</span><span style="color: #009900;">&#40;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>YAHOO.<span style="color: #660066;">util</span>.<span style="color: #660066;">Dom</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my-carousel&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; YAHOO.<span style="color: #660066;">ErisDS</span>.<span style="color: #660066;">Carousel</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Copy the code above into your Javascript file. You should now have a working carousel.</p>
<h3>The Properties</h3>
<dl>
<dt>autoPlayInterval: 5000</dt>
<dd>How many milliseconds between transitions (i.e. 5 seconds)</dd>
<dt>isCircular: true </dt>
<dd>Previous &amp; next button are always active as when the carousel gets to the last item it carries on back to the first.</dd>
<dt>animation: { speed: 1.0 }</dt>
<dd>How long it takes for a transition from one item to the next to complete. 1.0 is one second.</dd>
<dt>numVisible: 1 </dt>
<dd>How many items are visible at any one time.</dd>
</dl>
<p><strong>Note:</strong> The configuration attribute used to make the YUI Carousel autoplay changed somewhere between YUI 2.6 &amp; 2.7 from <kbd>autoPlay</kbd>, to <kbd>autoPlayInterval</kbd>. It works the same way, but there is no backwards compatibility.</p>
<h2>Tidying up &#8211; A little Custom CSS</h2>
<p>Depending on your browser and any other CSS you have present on the page your carousel may have some spacing which makes it look untidy. The following CSS is designed to ensure the carousel fits snug around the images and looks smart.</p>
<div class="codecolorer-container css twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc00cc;">#my-carousel</span> .carousel-container<span style="color: #00AA00;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">364px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #cc00cc;">#my-carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #cc00cc;">#my-carousel</span> ol.carousel-<span style="color: #000000; font-weight: bold;">content</span> li<span style="color: #00AA00;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">500px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">364px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>The <a href="http://erisds.co.uk/resources/yui-carousel-demo-i">finished article is basically an image slideshow</a>.  In the next part we will drop the YUI CSS and customise the carousel and navigation. If you have had any problems following this tutorial or ended up with a different result, please drop me a comment and I&#8217;ll try to help out.</p>
<h3>Resources</h3>
<ul>
<li><a href="http://developer.yahoo.com/yui/docs/YAHOO.widget.Carousel.html">Visit the API for a full list of available methods, properties and attributes.</a></li>
<li><a title="YUI Dependency Configurator" href="http://developer.yahoo.com/yui/articles/hosting/">YUI Dependency Configurator</a></li>
<li><a href="http://erisds.co.uk/resources/yui-carousel-demo-1">The demo page (1)</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2' rel='bookmark' title='Permanent Link: YUI: Javascript Carousel with Custom Navigation &#8211; Part 2'>YUI: Javascript Carousel with Custom Navigation &#8211; Part 2</a><small>Welcome to Part 2 of my YUI Carousel series. If...</small></li>
<li><a href='http://erisds.co.uk/code/getting-rid-of-non-breaking-spaces-nbsp' rel='bookmark' title='Permanent Link: Getting Rid of Non Breaking Spaces (&amp;nbsp)'>Getting Rid of Non Breaking Spaces (&amp;nbsp)</a><small>Have you ever tried to parse, process or preg_replace some...</small></li>
<li><a href='http://erisds.co.uk/wordpress/snippet-wordpress-admin-tidy-dashboard-widgets-by-role' rel='bookmark' title='Permanent Link: Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role'>Snippet: WordPress Admin &#8211; Tidy Dashboard Widgets By Role</a><small>Recently I worked on a Wordpress site where I wanted...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-1/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
