<?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>Lifesize Design &#187; CSS</title>
	<atom:link href="http://lifesizedesign.net/wordpress/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifesizedesign.net/wordpress</link>
	<description>Web and Graphic Design</description>
	<lastBuildDate>Thu, 21 Oct 2010 22:38:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Custom jQuery accordion tutorial</title>
		<link>http://lifesizedesign.net/wordpress/custom-jquery-accordion-tutorial/</link>
		<comments>http://lifesizedesign.net/wordpress/custom-jquery-accordion-tutorial/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 19:43:33 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[accordion]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://lifesizedesign.net/wordpress/?p=10</guid>
		<description><![CDATA[Ah, the much loved but often over used accordion. It has its time and place but we need to be sure we are actually facilitating the content or design and not just using it as a spiffy animation. When the content says &#8220;hey there, I&#8217;m an accordion&#8221; by all means use it. But, regardless of [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, the much loved but often over used accordion. It has its time and place but we need to be sure we are actually facilitating the content or design and not just using it as a spiffy animation. When the content says &#8220;hey there, I&#8217;m an accordion&#8221; by all means use it. But, regardless of your need, or lack there of, for an accordion, a lot can be learned about jQuery from building one from the ground up. This time around, we&#8217;ll be taking advantage of jQuery&#8217;s animation and event functions. So let&#8217;s dive right in.</p>
<p><strong>Please note:</strong> This tutorial assumes basic knowledge of HTML, CSS, and basic <a href="http://jquery.com/">jQuery</a> usage and syntax.</p>
<h3>View the <a href="http://lifesizedesign.net/tutorials/custom-accordion/">demo</a>.</h3>
<p>So as you can see from the demo, we are going to  be coding up your standard accordion. Nothing too amazing there but in addition to that we&#8217;ll be spicing it up a bit.  Using jQuery&#8217;s .css( ) function, it is easy to add icons to indicate that the item is expandable and collapsible. This adds a bit of style but also increases the user experience. It&#8217;s important that if we are going to add functionality to our site we need to let the user know that the functionality is there.<br />
<span id="more-10"></span><br />
Go ahead and create your project folder and a new file called index.html. We&#8217;ll add in the basic page structure, include our CSS, and link to jQuery.</p>
<pre class="brush: xml; highlight: [5];">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;main.css&quot;/&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js&quot;&gt;&lt;/script&gt;
		&lt;title&gt;Custom jQuery accordion demo&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
	&lt;div id=&quot;wrapper&quot;&gt;
		&lt;div id=&quot;header&quot;&gt;
			&lt;h1&gt;Custom jQuery accordion demo&lt;/h1&gt;
		&lt;/div&gt;
		&lt;div id=&quot;content&quot;&gt;
			...
		&lt;/div&gt;
	&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You might have noticed we are linking to <a href="http://code.google.com/apis/ajaxlibs/">Google&#8217;s hosted AJAX libraries</a>. There are many advantages to this but we won&#8217;t get into them now (if you are really curious check out <a href="http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/">this post</a>). For now just consider it &#8220;best practice&#8221;.</p>
<p>We can now move on to our accordion markup which we will be placing inside the #content DIV.</p>
<pre class="brush: xml;">
&lt;div class=&quot;itemWrapper&quot;&gt;
	&lt;h3 class=&quot;itemTitle&quot;&gt;Item One&lt;/h3&gt;
	&lt;p class=&quot;itemDesc&quot;&gt;Lorem ipsum...&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>As you can see, it&#8217;s a simple &lt;div&gt; wrapper with a header and paragraph with appropriate CSS classes. We will be using these classes to manipulate the content with jQuery and change its CSS on the fly.</p>
<p><strong>Now, let&#8217;s take a look at our jQuery.</strong></p>
<pre class="brush: jscript;">
function accordionList(){
	//we will use jQuery to hide the objects so if javascript is disabled the content is still displayed
	$('p.itemDesc').hide();
	$('.itemWrapper').css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
	$('.itemWrapper').click(
		function(){
			//set the p element to a variable for referencing
			var nextItem = $(this).children('p');
			//check to see if we are clicking on an already expanded item
			if((nextItem.is('p')) &amp;&amp; (nextItem.is(':visible'))) {
				//remove the following 2 lines and just return false for a non-toggling accordion
				nextItem.slideUp('normal');
				$(this).css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
				return false;
			}
			//check to see if we are clicking on a hiden item
			if((nextItem.is('p')) &amp;&amp; (!nextItem.is(':visible'))) {
				//colapse the item and change the icon
				$('.itemWrapper p:visible').slideUp('normal');
				$('.itemWrapper p:visible').parents('.itemWrapper').css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
				//expand the clicked item and change the icon
				nextItem.slideDown('normal');
				$(this).css('background',&quot;url('img/icon_symbolMinus.png') no-repeat top right&quot;);
				return false;
			}
		}
	);
}

$(document).ready(function(){
	accordionList();
});
</pre>
<p>So basically, what we are doing is creating a function called accoridonList( ) and calling that function when the document is ready.</p>
<p>Let us take a closer look at accordionList( ).</p>
<pre class="brush: jscript;">
$('p.itemDesc').hide();
$('.itemWrapper').css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
</pre>
<p>You can see the first thing we do is use jQuery to hide our P elements with the class of &#8220;itemDesc&#8221;. The point in doing it this way, as opposed to initially hiding the content applying the icon via CSS, is if a user has javascript disabled the content will still be displayed appropriately, allowing for graceful degradation. We next move on to handling the .click( ) event.</p>
<pre class="brush: jscript;">
$('.itemWrapper').click(
	function(){
		...
	}
);
</pre>
<p>Here we are saying, when the element with the class &#8220;itemWrapper&#8221; is clicked do something. Since we are not clicking on the element we want to affect we will make a pointer to it for easy reference.</p>
<pre class="brush: jscript;">
var nextItem = $(this).children('p');
</pre>
<p>This will create a variable called &#8220;nextItem&#8221;. This references the P element that is a child of our clicked upon element. In this case we are clicking on the wrapping DIV.</p>
<p>Next we create a conditional statement which checks to see if the .itemWrapper we are clicking on has been expanded and is visible.</p>
<pre class="brush: jscript;">
if((nextItem.is('p')) &amp;&amp; (nextItem.is(':visible'))) {
	//remove the following 2 lines and just return false for a non-toggling accordion
	nextItem.slideUp('normal');
	$(this).css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
	return false;
}
</pre>
<p>First we are asking if our pointer is referencing our P element. Then we checking to see if that element is visible. If it is we tell jQuery to collapse &#8220;nextItem&#8221; and change the wrappers background image to reflect that.</p>
<p>Our next if statement has a bit more to it. As you can see in the <a href="http://lifesizedesign.net/tutorials/custom-accordion/">demo</a> we are creating an accordion that will only allow one item to be open at a time. So we need to not only expand the clicked wrapper and change its icon but also find the expanded wrapper, collapse it, and change its icon to reflect that.</p>
<pre class="brush: jscript;">
if((nextItem.is('p')) &amp;&amp; (!nextItem.is(':visible'))) {
	//colapse the item and change the icon
	$('.itemWrapper p:visible').slideUp('normal');
	$('.itemWrapper p:visible').parents('.itemWrapper').css('background',&quot;url('img/icon_symbolPlus.png') no-repeat top right&quot;);
	//expand the clicked item and change the icon
	nextItem.slideDown('normal');
	$(this).css('background',&quot;url('img/icon_symbolMinus.png') no-repeat top right&quot;);
	return false;
}
</pre>
<p>Here, like before, we check to see if we are referencing our desired P element. But this time around we are asking if it is not visible to do something. First it finds the currently visible paragraph, if there is one, and hides it then changes the icon back to a plus symbol. The last thing to do is expand our clicked wrapper and change its icon to a minus symbol.</p>
<p>The final call to our function we just created.</p>
<pre class="brush: jscript;">
$(document).ready(function(){
	accordionList();
});
</pre>
<p>So that about wraps it up. The call to our accordionList( ) function will apply the animation effects to the .itemWrapper DIVs.</p>
<p>As for our CSS, it will provide basic styling and doesn&#8217;t need to be covered here.</p>
<p>If you have any questions leave a comment below. I hope you enjoyed the Custon jQuery Accordion tutorial. If there are any other web design and development tutorials you would like to see let me know in the comments.</p>
<p><em>Andrew Cline is a freelance web developer and designer in Portland, Oregon</em></p>
]]></content:encoded>
			<wfw:commentRss>http://lifesizedesign.net/wordpress/custom-jquery-accordion-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

