<?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>Sat, 07 Jan 2012 10:29:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Starting out with Zend Framework</title>
		<link>http://erisds.co.uk/code/starting-out-with-zend-framework</link>
		<comments>http://erisds.co.uk/code/starting-out-with-zend-framework#comments</comments>
		<pubDate>Sun, 16 Oct 2011 09:54:50 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=2279</guid>
		<description><![CDATA[<img src="http://erisds.co.uk/wp-content/uploads/2011/10/zf-logo-mark-150x150.png" alt="Zend Framework" title="Zend Framework" width="150" height="150" class="alignleft size-thumbnail wp-image-2320" />
I am on a mission to get to grips with using the <a title="Zend Framework Overview" href="http://framework.zend.com/about/overview" target="_blank">Zend Framework</a> for building web applications. At MOO, the Zend Framework is used in it's more toolkit-like component library capacity and I'm also pretty familiar with its structure from previous work with Magento (although that always seemed to over-complicate it). Using it as a full stack framework however, is a different challenge all together.


Related posts:<ol><li><a href='http://erisds.co.uk/symfony/symfony-what-is-it-good-for' rel='bookmark' title='Permanent Link: Symfony: What is it good for?'>Symfony: What is it good for?</a><small>Symfony is one of my favourite bits of technology. I’ve...</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/symfony/snippet-symfony-forms-setting-the-form-formatter' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Setting the Form Formatter'>Snippet: Symfony Forms &#8211; Setting the Form Formatter</a><small>By default Symfony displays forms in tables, with each new...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://erisds.co.uk/wp-content/uploads/2011/10/zf-logo-mark-150x150.png" alt="Zend Framework" title="Zend Framework" width="150" height="150" class="alignleft size-thumbnail wp-image-2320" /><br />
I am on a mission to get to grips with using the <a title="Zend Framework Overview" href="http://framework.zend.com/about/overview" target="_blank">Zend Framework</a> for building web applications. At MOO, the Zend Framework is used in it&#8217;s more toolkit-like component library capacity and I&#8217;m also pretty familiar with its structure from previous work with Magento (although that always seemed to over-complicate it). Using it as a full stack framework however, is a different challenge all together.</p>
<p>Let me explain the reasons why I&#8217;m investing time in ZF. If you&#8217;ve read this blog before you may be aware that I have a lot of experience working with the <a title="Symfony Framework" href="http://www.symfony-project.org/" target="_blank">Symfony Framework</a>, and I&#8217;m really fond of it. However after using it to write what was quite a sizeable app, I found it started to get quite slow, as well as finding the <a title="ORM - Object Relational Mapping" href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORMs</a> (I mostly used <a title="Propel ORM" href="http://www.propelorm.org/" target="_blank">Propel</a>) to be quite cumbersome (I like SQL!). I often spent a long time trying to figure out how Symfony wanted me to do something and therefore the flexibility of ZF in this regard is attractive.</p>
<p>Zend Framework also seems to be more of an industry standard, where Symfony is somewhat niche. Coming from Zend and with training and qualifications available, it seems like a good tool to add to my toolbox. The added benefit is my code &amp; approach are more likely to be understood by other people quickly, so if any of my side projects ever take off they should scale easily :)</p>
<h2>Tackling the Tutorials</h2>
<p>So on to the task of learning how to approach a site with ZF. I have two books that I&#8217;ve skim-read: <a href="http://www.amazon.co.uk/gp/product/1933988320/ref=as_li_ss_tl?ie=UTF8&#038;tag=muofer-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=1933988320">Zend Framework in Action</a> and <a href="http://www.amazon.co.uk/gp/product/0973862157/ref=as_li_ss_tl?ie=UTF8&#038;tag=muofer-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=0973862157">Php|architect&#8217;s Guide to Programming with Zend Framework</a> both are OK but pretty broad and don&#8217;t cover key topics in much detail. I&#8217;ve also been through the quickstart tutorial on Zend.com and some of the more advanced stuff. It&#8217;s clear that Zend Framework is a powerful tool, but mastering it is no simple task.</p>
<p>Flexibility &amp; being able to approach problems in multiple ways was one of the attractions of Zend Framework, but I find the complete lack of even a hint at where code should go quite disconcerting. I would be happy with some suggestions and debates on best practice, but even that seems limited. Therefore I have taken to searching Google for key topics with the flag &#8221;-site:<a href="http://zend.com/" target="_blank">zend.com</a>&#8221; so that I only get results from sites other than Zend because it is top in all search results but almost never has the answer!</p>
<p>One of the great things about Symfony is the Askeet/Jobeet tutorial. Every version of Symfony has had an in-depth tutorial that went with it, covering each aspect of the system. This provided a great learning tool, and also a fantastic talking point for debate and spin-off tutorials which looked at areas in even more depth.</p>
<p>In complete contrast, the Zend Framework&#8217;s own tutorials seems to be extremely lacking. Most of the documentation seems to cover only very basic examples, lacking in any real depth or clever stuff. For example the <a title="Learning multiuser authentication" href="http://framework.zend.com/manual/1.11/en/learning.multiuser.authentication.html" target="_blank">Getting started with Zend_Auth</a> tutorial lacks CSRF protection &amp; password salting &#8211; two things that should really be &#8220;the standard&#8221; IMO. The following tutorial on <a title="Learning Multiuser Authorisation" href="http://framework.zend.com/manual/1.11/en/learning.multiuser.authorization.html" target="_blank">Zend_Acl</a> is even more disappointing &#8211; it doesn&#8217;t give any clue as to where any of the code should go. You&#8217;re expected to know/guess or just make it up as you go.</p>
<h2>Finding an Approach for Key Concepts</h2>
<p>There are three areas in particular which seem to be vastly under-documented / under-exampled in my opinion: Models, Access Control and Routing. I despise patterns using one model class to represent both a collection and instance of an object, which is what appears in a vast number of tutorials. ZF seems to roughly support the ActiveRecord style pattern (which I&#8217;m used to from using Propel/Doctrine) through extending its <a title="Zend Db Table" href="http://framework.zend.com/manual/en/zend.db.table.html" target="_blank">Db_Table</a> &amp; <a title="Db Table Row" href="http://framework.zend.com/manual/en/zend.db.table.row.html" target="_blank">Db_Table_Row</a> classes and paired with <a title="Zend Db Select" href="http://framework.zend.com/manual/en/zend.db.select.html" target="_blank">Db_Select </a>it works as a kind of lo-fi ORM. It seems that many <a title="ActiveRecord does not suck" href="http://karwin.blogspot.com/2008/05/activerecord-does-not-suck.html" target="_blank">Zend folk are not a fan of ActiveRecord</a>, but the main documentation barely hints at any of this, or any other potential approaches. You have to leave the main site and search various blogs to get even a whiff of any of this knowledge.</p>
<p>Even more worrying/frustrating is the seeming lack of documentation around Access Control &amp; Routing. I&#8217;m used to these being baked-in in Symfony as both were managed via YAML config files, and the almost-comes-as-standard sfGuardPlugin provided fine grained control of users, groups and permissions. ZF provides tools, but little direction and it seems quite intensive to get either of these key parts of a web application up and running.</p>
<p>Frustratingly, there seems to be no such obvious place to setup these vital parts of the site (does everyone end up with enormous Bootstrap files?!?). I&#8217;ve already mentioned the vagaries of the ACL tutorial, but routing for me is pretty fundamental. In Symfony you define your database schema (which results in a database, models &amp; forms for CRUD), then write your routes, then you write code. In ZF I&#8217;ve really struggled to find anything which takes the same REST API-as-standard approach to configuring routes. I know <a title="Building RESTful Services with Zend Framework" href="http://weierophinney.net/matthew/archives/228-Building-RESTful-Services-with-Zend-Framework.html" target="_blank">it can be done</a>, it just doesn&#8217;t seem to be a standard way of thinking about things.</p>
<h2>Zend Framework vs Symfony?</h2>
<p>Well, this turned out to be much more of a Symfony vs Zend Framework post than I intended. It also seems that all the features of Zend that I thought would make it great to use have turned out to be what makes it a pain in the arse! I know that this is partially because I&#8217;ve been spoiled by Symfony doing so much of the heavy lifting for me, and by its awesome documentation. I am also aware I&#8217;m trying to do ORM-y things without an ORM. It seems that by keeping a tight reign on the approach to key concepts, Symfony had allowed itself to foster a much richer pool of easily-accessible documentation. Also, Symfony seems much more intent on <a title="Symfony Bloggers RSS Feed" href="http://trac.symfony-project.org/wiki/SymfonyBloggers" target="_blank">including its community</a> &amp; building great accessible documentation, instead of (or as well as) providing training courses.</p>
<p><strong>Note-to-self:</strong> I need to clear my mind of expectations and pre-conceptions about frameworks if I&#8217;m going to succeed at this!</p>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/symfony/symfony-what-is-it-good-for' rel='bookmark' title='Permanent Link: Symfony: What is it good for?'>Symfony: What is it good for?</a><small>Symfony is one of my favourite bits of technology. I’ve...</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/symfony/snippet-symfony-forms-setting-the-form-formatter' rel='bookmark' title='Permanent Link: Snippet: Symfony Forms &#8211; Setting the Form Formatter'>Snippet: Symfony Forms &#8211; Setting the Form Formatter</a><small>By default Symfony displays forms in tables, with each new...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/starting-out-with-zend-framework/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter OAuth: Simple cURL requests for your own data</title>
		<link>http://erisds.co.uk/code/twitter-oauth-simple-curl-requests-for-your-own-data</link>
		<comments>http://erisds.co.uk/code/twitter-oauth-simple-curl-requests-for-your-own-data#comments</comments>
		<pubDate>Mon, 10 Oct 2011 10:01:55 +0000</pubDate>
		<dc:creator>ErisDS</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[CURL]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://erisds.co.uk/?p=2239</guid>
		<description><![CDATA[<img src="http://erisds.co.uk/wp-content/uploads/2011/10/twitter_newbird_boxed_blueonwhite-150x150.png" alt="" title="twitter_newbird_boxed_blueonwhite" width="150" height="150" class="alignleft size-thumbnail wp-image-2261" />Having to include an entire PHP OAuth library every time I want to make a simple API request for some of my own data from a 3rd party app like Twitter really pisses me off. Perhaps this is unreasonable, but it's a problem I ran into for the 4th or 5th time today when trying to help <a href="http://john.onolan.org/" title="John O'Nolan's Awesome Blog" target="_blank">John O'Nolan</a> fetch his status count for his blog.


Related posts:<ol><li><a href='http://erisds.co.uk/internet/5-top-resources-for-twitter-success' rel='bookmark' title='Permanent Link: 5 Top Resources for Twitter Success'>5 Top Resources for Twitter Success</a><small>I've been experimenting with developing my Twitter account for the...</small></li>
<li><a href='http://erisds.co.uk/internet/digging-twitter' rel='bookmark' title='Permanent Link: Digging Twitter'>Digging Twitter</a><small>Last night this article hit the front page of Digg....</small></li>
<li><a href='http://erisds.co.uk/awesome/awesome-1-real-time-radiation-levels-twitter-heartbeat-bbc-fail' rel='bookmark' title='Permanent Link: Awesome: 1. Real-time radiation levels, Twitter heartbeat &#038; BBC #fail'>Awesome: 1. Real-time radiation levels, Twitter heartbeat &#038; BBC #fail</a><small>An introduction This is the first in a new series...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://erisds.co.uk/wp-content/uploads/2011/10/twitter_newbird_boxed_blueonwhite-150x150.png" alt="" title="twitter_newbird_boxed_blueonwhite" width="150" height="150" class="alignleft size-thumbnail wp-image-2261" />Having to include an entire PHP OAuth library every time I want to make a simple API request for some of my own data from a 3rd party app like Twitter really pisses me off. Perhaps this is unreasonable, but it&#8217;s a problem I ran into for the 4th or 5th time today when trying to help <a href="http://john.onolan.org/" title="John O'Nolan's Awesome Blog" target="_blank">John O&#8217;Nolan</a> fetch his status count for his blog.</p>
<p>Making a REST API request for your own twitter info using the &#8220;<a href="https://dev.twitter.com/docs/api/1/get/users/show" title="Twitter API GET users/show" target="_blank">user/show</a>&#8221; request is relatively simple as it doesn&#8217;t actually require authentication. That is until you upload the code to your server and discover that someone else sharing the server&#8217;s IP is very selfishly hogging all of the API requests.</p>
<p>Twitter very helpfully allows you to authenticate your own app for your account and generate an OAuth Access token, meaning you can skip the first few steps of the OAuth process. However after much Googling, I discovered there really aren&#8217;t that many examples of what to do next &#8211; they all seem to use one or another library &#8211; which seems totally unnecessary for making one request.</p>
<p>After much searching, finally I found one <a href="http://www.godlikemouse.com/2011/04/01/twitter-oauth-php-tutorial/" title="Twitter OAuth PHP Tutorial" target="_blank">awesome step-by-step tutorial</a> which explains <a href="http://www.godlikemouse.com/2011/04/01/twitter-oauth-php-tutorial/" title="Twitter OAuth PHP Tutorial" target="_blank">the first few steps of OAuth</a> and in particular how to build the OAuth HTTP headers and OAuth signature to sign the request. So I&#8217;ve taken lead from here, borrowed the helper functions and signature code, and put it all together to make a signed GET request to the API and return some ACTUAL data.</p>
<p>Here&#8217;s the couple of helper functions I borrowed, the first combines the request URI and parameters into a string as required by the OAuth signature. The second one takes an array of OAuth credentials/settings and combines them into the HTTP header for use with cURL.</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 />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 /></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> buildBaseString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$baseURI</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <a href="http://www.php.net/ksort"><span style="color: #990000;">ksort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$key</span>=&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</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: #b1b100;">return</span> <span style="color: #000088;">$method</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$baseURI</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</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><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> buildAuthorizationHeader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$oauth</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Authorization: OAuth '</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oauth</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$key</span>=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$r</span> <span style="color: #339933;">.=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span> <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Next you need to setup all of the OAuth credentials and settings. For Twitter, the tokens, keys and secrets you need will all be available on <a href="https://dev.twitter.com/apps" title="Twitter: My Apps" target="_blank">your App page</a>. If you haven&#8217;t got any Apps, you&#8217;ll need to <a href="https://dev.twitter.com/apps" title=" Twitter: My Apps" target="_blank">create one</a>, authorise it for your account and grab an access token.</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 />7<br />8<br />9<br />10<br />11<br />12<br />13<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;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://api.twitter.com/1/account/totals.json&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$oauth_access_token</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR TOKEN HERE&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$oauth_access_token_secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR TOKEN SECRET HERE&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$consumer_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR KEY HERE&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$consumer_secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR SECRET HERE&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$oauth</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'oauth_consumer_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$consumer_key</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'oauth_nonce'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'oauth_signature_method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HMAC-SHA1'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'oauth_token'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$oauth_access_token</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'oauth_timestamp'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'oauth_version'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The tricky bit, that doesn&#8217;t ever seem to be explained to well, is creating the OAuth signature. For HMAC-SHA1, this boils down to the following few lines:<br />
Create your URI base string by combining the URL you will be sending the request to with the oauth details as per the helper function. The key is the consumer secret and access token secret URL encoded and combined with an ampersand. Finally, the base url and the composite key are hashed and and base 64 encoded. Sounds really fraking complicated when you read the instructions, doesn&#8217;t look so bad when it&#8217;s written out in PHP.</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 /></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;">$base_info</span> <span style="color: #339933;">=</span> buildBaseString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$oauth</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$composite_key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$consumer_secret</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/rawurlencode"><span style="color: #990000;">rawurlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oauth_access_token_secret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$oauth_signature</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/base64_encode"><span style="color: #990000;">base64_encode</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/hash_hmac"><span style="color: #990000;">hash_hmac</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sha1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$base_info</span><span style="color: #339933;">,</span> <span style="color: #000088;">$composite_key</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$oauth</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_signature'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oauth_signature</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>To make the request we have to setup a HTTP header and a few other options to pass to cURL. The helper method does the hard work of creating the correct header format with the OAuth settings and then finally, it&#8217;s time to make the request with cURL.</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 />7<br />8<br />9<br />10<br />11<br />12<br />13<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;">$header</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>buildAuthorizationHeader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$oauth</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Expect:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> CURLOPT_HTTPHEADER <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$header</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_HEADER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_URL <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CURLOPT_SSL_VERIFYPEER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt_array"><span style="color: #990000;">curl_setopt_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$twitter_data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This works really well if you just want to fetch data from your own account &#8211; last statuses, mentions, favourites or your stats like in the example above. It should also work with any OAuth based API which supports HMAC-SHA1 signatures (Google, LinkedIn, Vimeo, the list goes on). If the API you&#8217;re working with doesn&#8217;t have a neat tool for getting an access token for your account like Twitter does, you can always do the first few authentication steps by using an <a href="http://term.ie/oauth/example/client.php" title="OAuth Online Client" target="_blank">online client</a>. Fill out your details and it will handle the first few exchanges, and once you have your access token you&#8217;re free to make requests using just the 50 lines of code above, instead of requiring an OAuth library.</p>
<p><a href="http://erisds.co.uk/wp-content/uploads/2011/10/twitter-oauth.zip" onClick="_gaq.push(['_trackPageview', '/downloads/twitter-oauth-001']);" title="Download the full Twitter OAuth Code Example">Download the full Twitter OAuth Code Example</a></p>
<p>Big Kudos to Jason Graves (aka. GodLikeMouse) for putting his excellent <a href="http://www.godlikemouse.com/2011/04/01/twitter-oauth-php-tutorial/" title="Twitter OAuth PHP Tutorial" target="_blank">PHP OAuth tutorial</a> on the web.</p>


<p>Related posts:<ol><li><a href='http://erisds.co.uk/internet/5-top-resources-for-twitter-success' rel='bookmark' title='Permanent Link: 5 Top Resources for Twitter Success'>5 Top Resources for Twitter Success</a><small>I've been experimenting with developing my Twitter account for the...</small></li>
<li><a href='http://erisds.co.uk/internet/digging-twitter' rel='bookmark' title='Permanent Link: Digging Twitter'>Digging Twitter</a><small>Last night this article hit the front page of Digg....</small></li>
<li><a href='http://erisds.co.uk/awesome/awesome-1-real-time-radiation-levels-twitter-heartbeat-bbc-fail' rel='bookmark' title='Permanent Link: Awesome: 1. Real-time radiation levels, Twitter heartbeat &#038; BBC #fail'>Awesome: 1. Real-time radiation levels, Twitter heartbeat &#038; BBC #fail</a><small>An introduction This is the first in a new series...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/twitter-oauth-simple-curl-requests-for-your-own-data/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</title>
		<link>http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8</link>
		<comments>http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8#comments</comments>
		<pubDate>Sun, 10 Oct 2010 17:56:20 +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=1700</guid>
		<description><![CDATA[Last year I published two parts of a three part tutorial (doh) on the <a href="http://developer.yahoo.com/yui/carousel/">YUI2 Carousel Widget</a>. Since that time, a new couple of new versions of the YUI2: Carousel widget have been released, and it seems my examples do not work with the updated code. This post covers the issues, the reasons why they occur and how to resolve them.


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/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>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Last year I published two parts of a three part tutorial (doh) on the <a href="http://developer.yahoo.com/yui/carousel/">YUI2 Carousel Widget</a> which worked with YUI Carousel 2.7.0. Since that time, a new couple of new versions of the YUI2: Carousel widget have been released (2.8.0 and 2.8.1), and it seems my examples do not work with the updated code. This post covers the issues, the reasons why they occur and how to resolve them.</p>
<p>In version 2.8.0 and 2.8.1 the introduction of code for handling rows has caused a bug which means that the carousels won&#8217;t display in Internet Explorer 6, 7 or 8 unless you explicitly set the width and height of the carousel elements in your CSS. If you want to use non-specific widths and heights, then you need to either use version 2.7.0 OR use <a href='http://erisds.co.uk/wp-content/uploads/2010/10/yui_carousel_2.8.1_fixed.js.zip' onClick="_gaq.push(['_trackPageview', '/downloads/yui2_carousel_2.8.1_fixed']);">my fixed version of 2.8.1</a>. The 2.8.* versions of the YUI2 Carousel also fix an issue with the positioning of elements in the carousel going wrong when zoomed in or out in Safari and other WebKit browsers such as Google Chrome so it&#8217;s a good idea to use the latest version if you can.</p>
<p>Also, to use version 2.8.* you need to use slightly different CSS to that in my two examples. Details of which are coming soon, along with updated tutorials and part 3.</p>
<h2>Fixing version 2.8.1</h2>
<p>I have used the YUI carousel in several projects, some of which both require the WebKit fix and to use carousels which vary in width and height dynamically. Therefore I have identified a fix for version 2.8.1.</p>
<p>To apply the fix, you will need to download the latest release of the carousel code:<br />
(Follow the link below, save the page as carousel.js inside your project and link to the file)</p>
<p><a href="http://yui.yahooapis.com/combo?2.8.1/build/carousel/carousel.js">http://yui.yahooapis.com/combo?2.8.1/build/carousel/carousel.js</a></p>
<p>Then locate and change lines 3602-3608:</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 /></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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>rows<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; size <span style="color: #339933;">=</span> getCarouselItemSize.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>carousel<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; size <span style="color: #339933;">=</span> size <span style="color: #339933;">*</span> rows<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Dom.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>carousel._carouselEl<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #339933;">,</span> size <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</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></div></td></tr></tbody></table></div>
<p>To the following:</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 /></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: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; size <span style="color: #339933;">=</span> getCarouselItemSize.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>carousel<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>rows<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; size <span style="color: #339933;">=</span> size <span style="color: #339933;">*</span> rows<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; Dom.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span>carousel._carouselEl<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #339933;">,</span> size <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</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>Alternatively, you can download my fixed version of the YUI Carousel here: <a href='http://erisds.co.uk/wp-content/uploads/2010/10/yui_carousel_2.8.1_fixed.js.zip' onClick="_gaq.push(['_trackPageview', '/downloads/yui2_carousel_2.8.1_fixed']);">YUI2 Carousel 2.8.1 fixed [ZIP File]</a></p>
<p>Hopefully this fixes the issues that many people have been having with the YUI Carousel in Internet Explorer. Also, this fixes a major roadblock in my release of part 3 of my YUI Carousel custom navigation series, so I hope to release the final part very soon.</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/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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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/technology/how-to-view-and-extract-plesk-backups' rel='bookmark' title='Permanent Link: How to view and extract Plesk backups'>How to view and extract Plesk backups</a><small> Have you ever had a minor developmental nightmare and...</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/technology/how-to-view-and-extract-plesk-backups' rel='bookmark' title='Permanent Link: How to view and extract Plesk backups'>How to view and extract Plesk backups</a><small> Have you ever had a minor developmental nightmare and...</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>10</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[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/internet/why-dont-all-html-elements-have-a-src-attribute' rel='bookmark' title='Permanent Link: Why don&#8217;t all HTML elements have a src attribute?'>Why don&#8217;t all HTML elements have a src attribute?</a><small>In the middle of writing a blog post about generating...</small></li>
<li><a href='http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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/internet/why-dont-all-html-elements-have-a-src-attribute' rel='bookmark' title='Permanent Link: Why don&#8217;t all HTML elements have a src attribute?'>Why don&#8217;t all HTML elements have a src attribute?</a><small>In the middle of writing a blog post about generating...</small></li>
<li><a href='http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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>10</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/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p class="small"><strong>Please Note:</strong> This tutorial is written for YUI version 2.7.0. It will not work correctly in versions 2.8 and 2.8.1. For more information and a fix please see <a href="http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8">my update article</a>.</p>
</blockquote>
<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/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://erisds.co.uk/code/yui-javascript-carousel-custom-navigation-part-2/feed</wfw:commentRss>
		<slash:comments>34</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/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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[<blockquote><p class="small"><strong>Please Note:</strong> This tutorial is written for YUI version 2.7.0. It will not work correctly in versions 2.8 and 2.8.1. For more information and a fix please see <a href="http://erisds.co.uk/code/yui2-javascript-carousel-an-update-about-version-2-8">my update article</a>.</p>
</blockquote>
<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/yui2-javascript-carousel-an-update-about-version-2-8' rel='bookmark' title='Permanent Link: YUI2: Javascript Carousel &#8211; An update about version 2.8.*'>YUI2: Javascript Carousel &#8211; An update about version 2.8.*</a><small>Last year I published two parts of a three part...</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>17</slash:comments>
		</item>
	</channel>
</rss>

