<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tutorialzine</title>
	
	<link>http://tutorialzine.com</link>
	<description>PHP MySQL jQuery CSS Tutorials, Resources and Freebies</description>
	<lastBuildDate>Thu, 04 Feb 2010 13:11:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Tutorialzine" /><feedburner:info uri="tutorialzine" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><feedburner:emailServiceId>Tutorialzine</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Making a Photoshoot Effect With jQuery &amp; CSS</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/81g_gBbeqZI/</link>
		<comments>http://tutorialzine.com/2010/02/photo-shoot-css-jquery/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 18:16:11 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=641</guid>
		<description><![CDATA[We are creating a photo shoot effect with our just-released PhotoShoot jQuery plug-in. With it you can convert a regular div on the page into a photo shooting stage simulating a camera-like feel.]]></description>
			<content:encoded><![CDATA[<p>Often, in your design or development tasks, you are presented with challenges that require a different approach than just plunging head over heels in coding. Research and experiments are a vital part of this process.</p>
<p>This is why this week&#8217;s tutorial is structured in a slightly different manner than usual. First we are presented with the main problems faced and their solutions, and after this we get round to building upon it.</p>
<p>We are creating a photo shoot effect with our just-released <a href="http://tutorialzine.com/2010/02/jquery-photoshoot-plugin/" target="_blank">PhotoShoot jQuery plug-in</a>. With it you can convert a regular div on the page into a photo shooting stage simulating a camera-like feel. Using this plug-in, we give visitors the ability to take shots of the background image.</p>
<p>Before starting with the tutorial, I would suggest you download the zip archive from the buttons above.</p>
<h3>Problem 1 &#8211; Blurring the image</h3>
<p>JavaScript does not support blurring an image directly. For example, there is <strong>no such thing</strong> as document.getElemetById(&#8216;image&#8217;).style.blur=2, no matter how useful it would&#8217;ve been.</p>
<p>The technique I&#8217;ve used and incorporated into the plug-in is actually quite simple &#8211; it just stacks up a bunch of divs, each holding the image as a background and whose opacity is lowered, one on top of the other. Those divs&#8217; positions are off by a few pixels at random, so you end up with a blurred effect.</p>
<blockquote><p>There is however a newer alternative to this technique, which I hope to implement in a future version of the plugin. It is achieved via the <strong>canvas</strong> HTML5 element, which allows low-level modifications of an image inside of the canvas. Unfortunately, canvas is not yet supported by a large portion of the currently used browsers, so a combination of the techniques would have to be used.</p></blockquote>
<h3>Problem 2 &#8211; Hiding the cursor</h3>
<p>CSS does not provide a way for hiding the cursor from view. E.g. you <strong>cannot</strong> specify a CSS rule like <strong>cursor : none</strong>. There is a neat workaround  though. CSS gives you the ability to specify a custom cursor in a <strong>.cur</strong> file with the <strong>css:url()</strong> rule. These files support transparency, so you just need to make a completely transparent cursor and assign it to the area in which you wish the cursor to be hidden.</p>
<p>Unfortunately Google Chrome has issues with completely blank cursors, so a special version has to be tailored which contains one white pixel (still better than nothing).</p>
<p>Another trouble maker is Opera, which does not support custom cursors altogether. There are no workarounds. It is not that much of a big deal though, everything else runs fine in Opera.</p>
<h3>Problem 3 &#8211; No support for masks</h3>
<p>A solution to this problem is to use the background property of the viewfinder div to display the original image. By specifying a negative top and left margin and updating it on every mouse move, we can give users  the impression that the viewfinder clears up the blurring of the scenery below.</p>
<blockquote><p>Masking is the process in which only a part of an image or shape is shown, with the rest clipped away. In the case of this tutorial, a mask would&#8217;ve given us the ability to have a regular version of the image above the blurred one, and mask it, so that only the part that intersects with the viewfinder is shown and thus giving us the impression that it un-blurs the image below.</p></blockquote>
<p>The solutions to these problems are implemented in the plug-in for us, which lifts a lot of the development burden and we can start building upon it.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>As most of the work is handled by the PhotoShoot jQuery plug-in, our work is reduced to only provide a div that is going to be transformed into a photo shooting stage (we still have to pass a configuration object holding the image we want displayed, along with a few other options though).</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;div id="main"&gt;

&lt;!-- The plugin automatically inserts the needed markup here --&gt;

&lt;/div&gt;
</pre>
<p>You can have this div anywhere on your page. You&#8217;ll need to specify a fixed width and height in the stylesheet in order for this to work properly. After the page loads and the plug-in is initialized, additional code is inserted into this div.</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;div id="main"&gt;

&lt;div class="blur" style="......"&gt;&lt;/div&gt;
&lt;div class="blur" style="......"&gt;&lt;/div&gt;
&lt;!--  8 more blur divs --&gt;

&lt;div class="overlay" style="opacity: 0.2;"&gt;&lt;/div&gt;

&lt;div style="......" class="viewFinder"&gt;
&lt;img src="photoShoot/viewfinder.png" width="300" height="200"&gt;
&lt;/div&gt;

&lt;!-- Additional html for the shots is inserted here. Not part of the plug-in.  --&gt;

&lt;/div&gt;
</pre>
<p>A whole lot has changed here. As mentioned earlier, the blur effect is achieved by stacking transparent divs on top of each other &#8211; the <strong>blur</strong> divs. After this is the overlay div, which darkens the layers below it, according to the opacity option passed to the plug-in.</p>
<p>Finally we have the viewfinder, which follows the mouse movements on the area and has the non-blurred version of the image as its background.</p>
<p>To ensure maximum flexibility, the plug-in provides a way to execute a user-defined function when a click occurs. This is exactly what we use to simulate a camera flash and to insert a new shot in the album div, which is not part of the plug-in.</p>
<div id="attachment_644" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/02/i1.jpg"><img class="size-full wp-image-644" title="A PhotoShoot Effect With Pure jQuery &amp; CSS" src="http://tutorialzine.com/wp-content/uploads/2010/02/i1.jpg" alt="A PhotoShoot Effect With Pure jQuery &amp; CSS" width="620" height="460" /></a><p class="wp-caption-text">A PhotoShoot Effect With Pure jQuery &amp; CSS</p></div>
<h3>Step 2 &#8211; CSS</h3>
<p>The plug-in comes with its own stylesheet (<strong>photoShoot/jquery.photoShoot-1.0.css</strong> in the demo files) which defines the look of the photo shoot components, so we are only left with styling the rest of the page.</p>
<h4>styles.css</h4>
<pre class="brush:css">#main{
	/* This div is converted to a photoShoot stage by the Photo Shoot plug-in */
	margin:0 auto;
	width:960px;
	height:600px;
}

.shot{
	/* These contain a scaled down version of the background image: */

	border:3px solid #FCFCFC;
	float:right;
	position:relative;
	margin-left:10px;
	overflow:hidden;

	/* Adding a CSS3 shadow below the shots: */

	-moz-box-shadow:0 0 2px black;
	-webkit-box-shadow:0 0 2px black;
	box-shadow:0 0 2px black;
}

.shot img{
	display:block;
}

.album{
	/* This div holds the shots */
	bottom:50px;
	height:110px;
	overflow:hidden;
	position:absolute;
	right:20px;
	width:490px;
}

.album .slide{
	/* The slide div is contained in album  */
	width:700px;
	height:110px;
	position:relative;
	left:-210px;
}
</pre>
<p>Each shot is dynamically inserted by our own custom <strong>shoot</strong> function when a click event occurs (as you will see in the next step of the tutorial). The shots are basically a scaled down version of the background image (this means that the image is downloaded once and used multiple times), which are offset to the top and bottom, according to the position of the viewfinder in the moment the event occurred.</p>
<p>The <strong>album</strong> and <strong>slide</strong> divs are added by our own jQuery script (not by the plug-in). The principle here is that the slide div is larger than its parent, the album div, and the shot is slid to the left when inserted, but more on that in a moment.</p>
<div id="attachment_645" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/02/i2.jpg"><img class="size-full wp-image-645" title="The shots" src="http://tutorialzine.com/wp-content/uploads/2010/02/i2.jpg" alt="The shots" width="620" height="260" /></a><p class="wp-caption-text">The shots</p></div>
<h3>Step 3 &#8211; jQuery</h3>
<p>The photoShoot plug-in itself will not be discussed here as you can read more about it on its <a href="http://tutorialzine.com/2010/02/jquery-photoshoot-plugin/" target="_blank">official page</a>. We do need, however, some additional jQuery code which:</p>
<ul>
<li>Inserts the <strong>.album</strong> to the <strong>#main</strong> div;</li>
<li>Chooses a random flickr image from an array to be fed to the plug-in;</li>
<li>Creates the options object;</li>
<li>Defines a custom <strong>shot</strong> function which is called on mouse click by the plug-in;</li>
<li>Calls the plug-in with the .<strong>photoshoot()</strong> method.</li>
</ul>
<h4>script.js</h4>
<pre class="brush:js">$(document).ready(function(){

	// This code is executed after the DOM has been completely loaded

	// Assigning the jQuery object to a variable for speed:
	var main = $('#main');

	// Setting the width of the photoshoot area to
	// 1024 px or the width of the document - whichever is smallest:

	main.width(Math.min(1024,$(document).width()));

	// Creating an array with four possible backgrounds and their sizes:
	var pics = new Array(
				{ url:'http://farm4.static.flickr.com/3595/3405361333_77f2a5e731_b.jpg', size:{x:1024,y:677}},
				{ url:'http://farm4.static.flickr.com/3028/2753126743_4249a4e948_b.jpg', size:{x:1024,y:768}},
				{ url:'http://farm4.static.flickr.com/3641/3595250019_5a1237899a_b.jpg', size:{x:1024,y:768}},
				{ url:'http://farm3.static.flickr.com/2592/4018062274_1f7f23597d_o.jpg', size:{x:1158,y:756}}
	);

	// Choosing a random picture to be passed to the PhotoShoot jQuery plug-in:
	var bg = pics[parseInt(Math.random()*4)];

	// Creating an options object (try tweeking the variables):
	var opts = {
		image		:	bg.url,
		onClick		:	shoot,
		opacity		:	0.8,
		blurLevel	:	4
	}

	// Calling the photoShoot plug-in and converting the #main div to a photo shoot stage:
	main.photoShoot(opts);

	// Adding the album holder to the stage:
	$('&lt;div class="album"&gt;').html('&lt;div class="slide" /&gt;').appendTo(main);

	// Our own shoot function (it is passed as onClick to the options array above):
	function shoot(position){
		// This function is called by the plug-in when the button is pressed

		// Setting the overlay's div to white will create the illusion of a camera flash:
		main.find('.overlay').css('background-color','white');

		// The flash will last for 100 milliseconds (a tenth of the second):
		setTimeout(function(){main.find('.overlay').css('background-color','')},100);

		// Creating a new shot image:
		var newShot = $('&lt;div class="shot"&gt;').width(150).height(100);
		newShot.append( $('&lt;img src="'+bg.url+'" width="'+(bg.size.x/2)+'" height="'+(bg.size.y/2)+'" /&gt;').css('margin',-position.top*0.5+'px 0 0 -'+position.left*0.5+'px') );

		// Removing the fourth shot (the count starts from 0):
		$('.shot').eq(3).remove();

		// Adding the newly created shot to the album div, but moved 160px to the right.
		// We start an animation to slide it in view:

		newShot.css('margin-right',-160).prependTo('.album .slide').animate({marginRight:0},'slow');
	}
});
</pre>
<p>Each time you click the area, a new shot is added to the <strong>slide</strong> div with a negative margin to the right. After this an animation starts, which slides it in view and pushes the other shots to the left, hiding the leftmost one.</p>
<p>It is important to remove the shots that are not visible with the <strong>remove</strong>() method. This way we prevent cluttering of the DOM with unneeded elements.</p>
<div id="attachment_646" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/02/i3.jpg"><img class="size-full wp-image-646" title="The jQuery Animation" src="http://tutorialzine.com/wp-content/uploads/2010/02/i3.jpg" alt="The jQuery Animation" width="620" height="260" /></a><p class="wp-caption-text">The jQuery Animation</p></div>
<p><strong>With this our Photoshoot effect is complete!</strong></p>
<h3>Conclusion</h3>
<p>Today we used a problem solving approach to create photo shoot effect with pure CSS and JavaScript. You can freely use the techniques demonstrated here and build upon the code. There are many possible uses especially in navigation systems and promotional sites.</p>
<p>If you liked this tutorial, be sure to <a href="http://twitter.com/Tutorialzine" target="_blank">follow us on twitter</a> for the latest web development news and links.</p>
<p>A big thanks goes to <a href="http://www.flickr.com/photos/haglundc/3405361333/" target="_blank">haglundc</a> for the <a href="http://www.flickr.com/photos/haglundc/3405361333/" target="_blank">landscape photo</a> used throughout this tutorial.</p>
<p><strong>What do you think? How would you use it?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=81g_gBbeqZI:Sappdv4go3M:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=81g_gBbeqZI:Sappdv4go3M:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=81g_gBbeqZI:Sappdv4go3M:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=81g_gBbeqZI:Sappdv4go3M:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=81g_gBbeqZI:Sappdv4go3M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/81g_gBbeqZI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/02/photo-shoot-css-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/02/photo-shoot-css-jquery/</feedburner:origLink></item>
		<item>
		<title>jQuery PhotoShoot Plugin 1.0</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/vDT2lYy6bDk/</link>
		<comments>http://tutorialzine.com/2010/02/jquery-photoshoot-plugin/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 18:15:44 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=649</guid>
		<description><![CDATA[The jQuery PhotoShoot plugin gives you the ability to convert any div on your web page into a complete photo shooting effect.]]></description>
			<content:encoded><![CDATA[<p>The jQuery PhotoShoot plugin gives you the ability to convert any div on your web page into a photo shooting effect, complete with a view finder. You can check out the demonstration above, or a <a href="http://tutorialzine.com/2010/02/photo-shoot-css-jquery/" target="_blank">nice tutorial on how to use it here</a>.</p>
<h3>Usage</h3>
<p>First, you need to upload the plug-in to your server (it would be best to keep the folder structure intact), and include the stylesheet and js file to the page where you want it to show.</p>
<pre class="brush:html">&lt;link rel="stylesheet" type="text/css" href="photoShoot/jquery.photoShoot-1.0.css" /&gt;

&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="photoShoot/jquery.photoShoot-1.0.js"&gt;&lt;/script&gt;
</pre>
<p>The plug-in depends on version 1.3.2 of jQuery, but will work fine with newer versions as well.</p>
<p>After this you can convert any div on your page by calling the <strong>.photoShoot()</strong> method and passing a configuration object.</p>
<pre class="brush:js">document.ready(function(){

	var config = {
		image : 'picture.jpg',
	}

	$('#selector').photoShoot(config)
});
</pre>
<p>You can pass additional parameters with the config object, according to the table below.</p>
<h3>Parameters</h3>
<p>The following parameters are supported:</p>
<table class="fancy-table" border="0" cellspacing="2" cellpadding="5">
<caption> Supported Parameters<br />
</caption>
<tbody>
<tr>
<th width="70">Parameter</th>
<th width="93">Default Value</th>
<th width="413">Description</th>
</tr>
<tr>
<td>image</td>
<td>&#8220;&#8221;</td>
<td>Required parameter. Specify the URL of the image to be shown.</td>
</tr>
<tr>
<td>blurLevel</td>
<td>4</td>
<td>The higher the blur level, the more blurred the image.</td>
</tr>
<tr>
<td>opacity</td>
<td>0.92</td>
<td>The lower the opacity, the darker the background behind the viewfinder.</td>
</tr>
<tr>
<td>viewFinder</td>
<td>{ width:300, height:200, img:&#8221; }</td>
<td>Expects an object with the properties width, height and img. With it you can change the size and png graphic of the view finder. If no img is provided, the default one is shown.</td>
</tr>
<tr>
<td>onClick</td>
<td>function(){}</td>
<td>Expects the name of the function that is going to be executed on click. An object is passed as a parameter. See below.</td>
</tr>
</tbody>
</table>
<h3>The onClick function</h3>
<p>You can provide your function to be executed when a click occurs. An <strong>object</strong> containing the location (left and top) of the viewfinder, relative to the background, is passed as the only <strong>parameter</strong>. Here is how it works:</p>
<pre class="brush:js">document.ready(function(){

	var config = {
		image	: 'picture.jpg',
		blurLevel	: 6,
		opacity	: 0.8,
		onClick	: alertPosition
	}

	function alertPosition(param){
		alert("The viewfinder is located at: "+param.left+"x"+param.top);
	}

	$('#selector').photoShoot(config)
});</pre>
<h3>License</h3>
<p>The plugin is distributed under an MIT license.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=vDT2lYy6bDk:uLevO7bZ3Nw:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=vDT2lYy6bDk:uLevO7bZ3Nw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=vDT2lYy6bDk:uLevO7bZ3Nw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=vDT2lYy6bDk:uLevO7bZ3Nw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=vDT2lYy6bDk:uLevO7bZ3Nw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/vDT2lYy6bDk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/02/jquery-photoshoot-plugin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/02/jquery-photoshoot-plugin/</feedburner:origLink></item>
		<item>
		<title>Sweet AJAX Tabs With jQuery 1.4 &amp; CSS3</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/hySb3NOq944/</link>
		<comments>http://tutorialzine.com/2010/01/sweet-tabs-jquery-ajax-css/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 15:00:14 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=633</guid>
		<description><![CDATA[In this tutorial we are making sweet AJAX-powered tabs with CSS3 and the newly released version 1.4 of jQuery]]></description>
			<content:encoded><![CDATA[<p>Organizing the content of a page in a both intuitive and eye-catching manner, is a must in modern web design. One principle that has been around for some time is dividing text into tabs. This allows you to squeeze much more content in a seemingly limited space and provide a structured way of accessing it.</p>
<p>Today we are making an AJAX-powered tab page with CSS3 and the newly released version 1.4 of jQuery, so be sure to download the zip archive from the button above and continue with step one.</p>
<h3>Step 1 – XHTML</h3>
<p>As usual, we start off with the XHTML markup.</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;ul class="tabContainer"&gt;
	&lt;!-- The jQuery generated tabs go here --&gt;
&lt;/ul&gt;

&lt;div class="clear"&gt;&lt;/div&gt;

&lt;div id="tabContent"&gt;
	&lt;div id="contentHolder"&gt;
		&lt;!-- The AJAX fetched content goes here --&gt;
	&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>If you think the markup looks too simple to be true, you are right. As you can see, we are missing the code for the tabs, because it is inserted dynamically by jQuery on page load. This makes it extremely easy to add new tabs as you only need to add them on the JavaScript side (more on that in a moment).</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;li&gt;
	&lt;a href="#" class="tab green"&gt;Tab two
		&lt;span class="left"&gt;&lt;/span&gt;
		&lt;span class="right"&gt;&lt;/span&gt;
	&lt;/a&gt;
&lt;/li&gt;
</pre>
<p>This is the markup that is inserted by jQuery for each tab. It consists of a LI element positioned inside of the .<strong>tabContainer</strong> unordered list above, and contains a hyperlink with two spans. Those show the left and the right part of the background image and thus enable the tabs to stretch and give room for the text label inside.</p>
<p>Also notice the <strong>green</strong> class of the link &#8211; it determines the background, text color and hover state of the tab, as you will see in the next step of this tutorial.</p>
<div id="attachment_636" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i12.png"><img class="size-full wp-image-636" title="Sweet AJAX-ed Tabs With jQuery 1.4 &amp; CSS3" src="http://tutorialzine.com/wp-content/uploads/2010/01/i12.png" alt="Sweet AJAX-ed Tabs With jQuery 1.4 &amp; CSS3" width="620" height="460" /></a><p class="wp-caption-text">Sweet AJAX-ed Tabs With jQuery 1.4 &amp; CSS3</p></div>
<h3>Step 2 – CSS</h3>
<p>With the markup in place, we can take a more detailed look at the styling of the tab page.</p>
<h4>styles.css &#8211; Part 1</h4>
<pre class="brush:css">.tabContainer{
	/* The UL */
	float:right;
	padding-right:13px;
}

#contentHolder{
	background-color:#EEEEEE;
	border:2px solid #FFFFFF;
	height:300px;
	margin:20px;

	color:#444444;
	padding:15px;
}

#tabContent{
	background-color:#333;
	border:1px solid #444;
	margin-top:-15px;
	width:100%;
}

#tabContent, .tabContainer li a,#contentHolder{
	-webkit-box-shadow:0 0 2px black;
	-moz-box-shadow:0 0 2px black;
	box-shadow:0 0 2px black;
}

.tabContainer li{
	/* This will arrange the LI-s next to each other */
	display:inline;
}

.tabContainer li a,.tabContainer li a:visited{
	/* Styling the hyperlinks of the tabs as colorful buttons */

	float:left;
	font-size:18px;

	/* display:block allows for additinal CSS rules to take effect, such as paddings: */
	display:block;

	padding:7px 16px 1px;
	margin:4px 5px;
	height:29px;

	/* Giving positioning */
	position:relative;

	/* CSS3 text-shadow */
	text-shadow:1px 1px 1px #CCCCCC;
}
</pre>
<p>Here we use a number of CSS3 rules that add up to the overall feel of the page. First is the <strong>box-shadow</strong> property, which adds a shadow below the tabs, the <strong>#tabContent</strong> div and the <strong>#contentHolder</strong>.</p>
<p>After this we have the <strong>text-shadow</strong> property, which adds a light-colored shadow (more of a outer glow in this case), which adds an inset feel to the text of the tabs.</p>
<h4>styles.css &#8211; Part 2</h4>
<pre class="brush:css">#overLine{
	/* The line above the active button. */
	position:absolute;

	height:1px;
	background-color:white;
	width:90px;

	float:left;
	left:1px;
	top:-5px;
	overflow:hidden;
}

#main{
	margin:0 auto;
	position:relative;
	width:700px;
}

ul .left{
	/* The left span in the hyperlink */

	height:37px;
	left:0;
	position:absolute;
	top:0;
	width:10px;
}

ul .right{
	/* The right span in the hyperlink */

	height:37px;
	right:0;
	position:absolute;
	top:0;
	width:10px;
}

/* Styling the colors individually: */

ul a.green{	background:url(img/green_mid.png) repeat-x top center;	color:#24570f;}
ul a.green span.left{ background:url(img/green_left.png) no-repeat left top;}
ul a.green span.right{ background:url(img/green_right.png) no-repeat right top;}

/* .. Analogical styles for the red, blue and orange color .. */

/* The hover states: */
ul a:hover{	background-position:bottom center; text-decoration:none;}
ul a:hover span.left{ background-position:left bottom;}
ul a:hover span.right{ background-position:right bottom;}

.preloader{
	display:block;
	margin:120px auto;
}
</pre>
<p>In the second part of the code, you can see that we define different backgrounds for the hyperlink and the left and right spans, depending on the color class that is assigned. This way we can successfully change a number of CSS styles and as a result have a completely different design of the tab, by just setting a different class name for the hyperlink.</p>
<div id="attachment_637" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i32.png"><img class="size-full wp-image-637" title="The Tabs" src="http://tutorialzine.com/wp-content/uploads/2010/01/i32.png" alt="The Tabs" width="620" height="260" /></a><p class="wp-caption-text">The Tabs</p></div>
<h3>Step 3 – jQuery</h3>
<p>This is where the magic happens. First we need to include the jQuery library in the page:</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;link rel="stylesheet" type="text/css" href="styles.css" /&gt;

&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;
</pre>
<p>We include the latest version of jQuery from Google&#8217;s CDN and immediately after it, we add our own <strong>script.js</strong> file, which contains all of our scripts.</p>
<p>Here is a detailed explanation of what exactly jQuery does:</p>
<ul>
<li>The page is opened in a      visitor’s browser and the jQuery  library is downloaded from Google’s      Content Depository Network;</li>
<li><strong>$(document).ready()</strong> is queued for execution and the function  that is provided as a      parameter is run when the DOM has  finished loading;</li>
<li>The Tabs object is looped with      the <strong>$.each()</strong> method      and individual <strong>&lt;LI&gt;</strong> elements are created and appended  to the      <strong>.tabContainer</strong> <strong>&lt;UL&gt;</strong> (covered in step one);</li>
<li>Event listeners for the <strong>click</strong> event on the tabs are set up.</li>
</ul>
<p>You can view the code below:</p>
<h4>script.js &#8211; Part 1</h4>
<pre class="brush:js">$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	/* Defining an array with the tab text and AJAX pages: */

	var Tabs = {
		'Tab one'	: 'pages/page1.html',
		'Tab two'	: 'pages/page2.html',
		'Tab three'	: 'pages/page3.html',
		'Tab four'	: 'pages/page4.html'
	}

	/* The available colors for the tabs: */
	var colors = ['blue','green','red','orange'];

 	/* The colors of the line above the tab when it is active: */
	var topLineColor = {
		blue:'lightblue',
		green:'lightgreen',
		red:'red',
		orange:'orange'
	}

	/* Looping through the Tabs object: */
	var z=0;
	$.each(Tabs,function(i,j){
		/* Sequentially creating the tabs and assigning a color from the array: */

		var tmp = $('&lt;li&gt;&lt;a href="#" class="tab '+colors[(z++%4)]+'"&gt;'+i+' &lt;span class="left" /&gt;&lt;span class="right" /&gt;&lt;/a&gt;&lt;/li&gt;');

		/* Setting the page data for each hyperlink: */

		tmp.find('a').data('page',j);

		/* Adding the tab to the UL container: */
		$('ul.tabContainer').append(tmp);
	})
</pre>
<p>The source above is the first part of <strong>script.js</strong>, which you can find in the download archive. You can add your own tabs on the page by inserting a new property to the Tabs object. The left part holds the name of the tab in single quotes, and the right part (after the semicolon) contains the AJAX URL which is going to be fetched and displayed in the <strong>#contentHolder</strong> div.</p>
<p>In the following second part of the script, you&#8217;ll see jQuery 1.4 in action, as we create a new div element (that acts as the line above the active tab) and pass an object with the ID and CSS properties instead of setting them separately with the <strong>.attr()</strong> and <strong>.css()</strong> methods.</p>
<h4>script.js &#8211; Part 2</h4>
<pre class="brush:js">	/* Caching the tabs into a variable for better performance: */
	var the_tabs = $('.tab');

	the_tabs.click(function(e){

		/* "this" points to the clicked tab hyperlink: */
		var element = $(this);

 		/* If it is currently active, return false and exit: */
		if(element.find('#overLine').length) return false;

 		/* Detecting the color of the tab (it was added to the class attribute in the loop above): */

		var bg = element.attr('class').replace('tab ','');

 		/* Removing the line: */
		$('#overLine').remove();

 		/* Creating a new div element with jQuery 1.4 by passing an additional object parameter: */

		$('&lt;div&gt;',{
			id:'overLine',
			css:{
				display:'none',
				width:element.outerWidth()-2,
				background:topLineColor[bg] || 'white'
			}}).appendTo(element).fadeIn('slow');

 		/* Checking whether the AJAX fetched page has been cached: */

		if(!element.data('cache'))
		{
			/* If no cache is present, show the gif preloader and run an AJAX request: */
			$('#contentHolder').html('&lt;img src="img/ajax_preloader.gif" width="64" height="64" class="preloader" /&gt;');

 			$.get(element.data('page'),function(msg){
				$('#contentHolder').html(msg);

 				/* After page was received, add it to the cache for the current hyperlink: */
				element.data('cache',msg);
			});
		}
		else $('#contentHolder').html(element.data('cache'));

 		e.preventDefault();
	})

	/* Emulating a click on the first tab, so that the content area is not empty: */
	the_tabs.eq(0).click();

});
</pre>
<p>Notice the use of the jQuery <strong>data()</strong> method throughout the code. It assigns arbitrary data to an element by calling the method with two parameters <strong>$(‘#selector’).data(‘label’,”String Data”)</strong>. This assigns the string “String Data” to the element and we can later access it (or check if it has been set) by calling the data method without the second parameter.</p>
<p>This way we set up a simple caching system for the AJAX requests. The first time an AJAX call is made, the text of the response (held in the <strong>msg</strong> variable) is stored at <strong>data(‘cache’)</strong>. On consecutive calls we check for this cache variable and return it instead of firing a new request. This way we remove unnecessary server load and improve the responsiveness of the tabs.</p>
<div id="attachment_638" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i23.png"><img class="size-full wp-image-638" title="Active Tab" src="http://tutorialzine.com/wp-content/uploads/2010/01/i23.png" alt="Active Tab" width="620" height="260" /></a><p class="wp-caption-text">Active Tab</p></div>
<p>With this our sweet AJAX-ed tabs are complete!</p>
<h3>Conclusion</h3>
<p>Today we created AJAX &#8211; enabled colorful tabs with jQuery 1.4 and CSS3. We also implemented a simple caching mechanism using the data() method. The source code is easily modifiable and adding new tabs is as easy as adding new properties to the Tabs object.</p>
<p>If you liked this tutorial, be sure to <a href="http://twitter.com/Tutorialzine" target="_blank">follow us on twitter</a> for early previews and interesting links from the world of web development.</p>
<p><strong>What do you think? How would you improve this example?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=hySb3NOq944:JavmLFrKYn0:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=hySb3NOq944:JavmLFrKYn0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=hySb3NOq944:JavmLFrKYn0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=hySb3NOq944:JavmLFrKYn0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=hySb3NOq944:JavmLFrKYn0:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/hySb3NOq944" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/01/sweet-tabs-jquery-ajax-css/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/01/sweet-tabs-jquery-ajax-css/</feedburner:origLink></item>
		<item>
		<title>AJAX-enabled Sticky Notes With PHP &amp; jQuery</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/myPLnq4pDRA/</link>
		<comments>http://tutorialzine.com/2010/01/sticky-notes-ajax-php-jquery/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 21:45:02 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=623</guid>
		<description><![CDATA[Today we are making an AJAX-enabled Sticky Note system. It will give visitors the ability to create notes with a live preview, and move them around on the screen.]]></description>
			<content:encoded><![CDATA[<p>Today we are making an AJAX-enabled Sticky Note management system. It will give visitors the ability to create notes with a live preview, and move them around on the screen. Every movement is going to be sent to the back-end via AJAX and saved in the database.</p>
<p>We are using version <strong>1.4</strong> of jQuery and the <a href="http://fancybox.net/" target="_blank">fancybox plugin</a> (you can also check our <a href="http://tutorialzine.com/2009/11/hovering-gallery-css3-jquery/" target="_blank">CSS3 Lightbox Gallery Tutorial</a>, where we also used fancybox).</p>
<p>You can download the example files and keep the demo site open in a tab so that it is easier to follow the steps of the tutorial.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>The first step is to create the necessary XHTML structure. The markup in the main demonstration file &#8211; <strong>demo.php</strong> is pretty straightforward, as you can see for yourself from the code below.</p>
<h4>demo.php</h4>
<pre class="brush:html">&lt;div id="main"&gt;
&lt;a id="addButton" class="green-button" href="add_note.html"&gt;Add a note&lt;/a&gt;

&lt;?php echo $notes?&gt;

&lt;/div&gt;</pre>
<p>It contains the <strong>main</strong> div, which holds all the notes and  limits their movement during the dragging process. The rest is generated dynamically by PHP after running a SELECT query against the database, and after storing the result in the <strong>$notes</strong> variable as you will see in the next step.</p>
<p>If you click the &#8220;Add a note&#8221; button on the demonstration site, you will see that a form with a live preview pops up. This functionality is provided by the fancybox, which fetches <strong>add_note.html</strong> (which contains the form) and shows it within a pop-up.</p>
<h4>add_note.html</h4>
<pre class="brush:html">&lt;h3 class="popupTitle"&gt;Add a new note&lt;/h3&gt;

&lt;!-- The preview: --&gt;
&lt;div id="previewNote" class="note yellow" style="left:0;top:65px;z-index:1"&gt;
&lt;div class="body"&gt;&lt;/div&gt;
&lt;div class="author"&gt;&lt;/div&gt;
&lt;span class="data"&gt;&lt;/span&gt;
&lt;/div&gt;

&lt;div id="noteData"&gt; &lt;!-- Holds the form --&gt;
&lt;form action="" method="post" class="note-form"&gt;

&lt;label for="note-body"&gt;Text of the note&lt;/label&gt;
&lt;textarea name="note-body" id="note-body" class="pr-body" cols="30" rows="6"&gt;&lt;/textarea&gt;

&lt;label for="note-name"&gt;Your name&lt;/label&gt;
&lt;input type="text" name="note-name" id="note-name" class="pr-author" value="" /&gt;

&lt;label&gt;Color&lt;/label&gt; &lt;!-- Clicking one of the divs changes the color of the preview --&gt;
&lt;div class="color yellow"&gt;&lt;/div&gt;
&lt;div class="color blue"&gt;&lt;/div&gt;
&lt;div class="color green"&gt;&lt;/div&gt;

&lt;!-- The green submit button: --&gt;
&lt;a id="note-submit" href="" class="green-button"&gt;Submit&lt;/a&gt;

&lt;/form&gt;
&lt;/div&gt;</pre>
<p>In the form you can fill in the text of the note, your name and choose a color. Providing a way for users to see how their note will appear in the page in real time is a useful addition which serves another practical purpose &#8211; when clicking the submit button and the lightbox window closes, the preview note is copied to the <strong>main</strong> div, which saves us from writing additional code.</p>
<div id="attachment_628" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i11.png"><img class="size-full wp-image-628" title="Note Creation Form With Live Preview" src="http://tutorialzine.com/wp-content/uploads/2010/01/i11.png" alt="Note Creation Form With Live Preview" width="620" height="460" /></a><p class="wp-caption-text">Note Creation Form With Live Preview</p></div>
<h3>Step 2 &#8211; PHP</h3>
<p>As mentioned earlier, PHP fills the <strong>$notes</strong> variable by running a query againts the database and outputs it on the page. Lets see how this works.</p>
<h4>demo.php</h4>
<pre class="brush:php">$query = mysql_query("SELECT * FROM notes ORDER BY id DESC");

$notes = '';
$left='';
$top='';
$zindex='';
while($row=mysql_fetch_assoc($query))
{
	// The xyz column holds the position and z-index in the form 200x100x10:
	list($left,$top,$zindex) = explode('x',$row['xyz']);
	$notes.= '
		&lt;div class="note '.$row['color'].'" style="left:'.$left.'px;top:'.$top.'px;	z-index:'.$zindex.'"&gt;
		'.htmlspecialchars($row['text']).'
		&lt;div class="author"&gt;'.htmlspecialchars($row['name']).'&lt;/div&gt;
		&lt;span class="data"&gt;'.$row['id'].'&lt;/span&gt;
	&lt;/div&gt;';
}</pre>
<p>The notes table not only stores the text and the author of the note, but it also has a dedicated column for the <strong>x</strong> and <strong>y</strong> coordinates, as well for the <strong>z-index</strong> (or stacking order). Those are stored in the <strong>xyz</strong> field of the table, which is updated by AJAX.</p>
<p>After the visitor clicks on the &#8220;<strong>Add a note</strong>&#8221; button, <strong>fancybox</strong> grabs <strong>add_note.html</strong> (which was covered in step one) and displays the live preview form. When it is submitted, the data is sent via AJAX to <strong>post.php</strong>, given below.</p>
<h4>post.php</h4>
<pre class="brush:php">// Escaping the input data:
$author = mysql_real_escape_string(strip_tags($_POST['author']));
$body = mysql_real_escape_string(strip_tags($_POST['body']));
$color = mysql_real_escape_string($_POST['color']);
$zindex = (int)$_POST['zindex'];

/* Inserting a new record in the notes DB: */
mysql_query('	INSERT INTO notes (text,name,color,xyz)
VALUES ("'.$body.'","'.$author.'","'.$color.'","0x0x'.$zindex.'")');

if(mysql_affected_rows($link)==1)
{
	// Return the id of the inserted row:
	echo mysql_insert_id($link);
}
else echo '0';</pre>
<p>After escaping all the input data and inserting it in the table, the script checks whether there were any rows affected after the insert query. If <strong>mysql_affected_rows</strong> returns 1, this would mean that the insert was successful and the automatically assigned <strong>auto_increment</strong> ID is outputted.</p>
<p>AJAX is also used to save the positions of the individual notes after the end of each movement. The JavaScript code that actually requests these AJAX calls is presented in step 4 of this tutorial. The PHP code is included below:</p>
<h4>update_position.php</h4>
<pre class="brush:php">// Validating the input data:
if(!is_numeric($_GET['id']) || !is_numeric($_GET['x']) || !is_numeric($_GET['y']) || !is_numeric($_GET['z']))
die("0");

// Escaping:
$id = (int)$_GET['id'];
$x = (int)$_GET['x'];
$y = (int)$_GET['y'];
$z = (int)$_GET['z'];

// Saving the position and z-index of the note:
mysql_query("UPDATE notes SET xyz='".$x."x".$y."x".$z."' WHERE id=".$id);

echo "1";</pre>
<p>After making sure that the input data is valid, the script updates the xyz field of the row for the note in question and prints 1 on success.</p>
<p>Lets continue with step three.</p>
<h3>Step 3 &#8211; CSS</h3>
<p>All the markup is in place, so it is time to throw in some fancy styling. The styles are defined in styles.css. I divided the file in three parts.</p>
<h4>styles.css &#8211; Part 1</h4>
<pre class="brush:css">.note{
	height:150px;
	padding:10px;
	width:150px;
	position:absolute;
	overflow:hidden;
	cursor:move;

	font-family:Trebuchet MS,Tahoma,Myriad Pro,Arial,Verdana,sans-serif;
	font-size:22px;

	/* Adding a CSS3 shadow below the note, in the browsers which support it: */
	-moz-box-shadow:2px 2px 0 #DDDDDD;
	-webkit-box-shadow:2px 2px 0 #DDDDDD;
	box-shadow:2px 2px 0 #DDDDDD;
}

#fancy_ajax .note{ cursor:default; }

/* Three styles for the notes: */

.yellow{
	background-color:#FDFB8C;
	border:1px solid #DEDC65;
}

.blue{
	background-color:#A6E3FC;
	border:1px solid #75C5E7;
}

.green{
	background-color:#A5F88B;
	border:1px solid #98E775;
}

/* Each note has a data span, which holds its ID */
span.data{ display:none; }

/* The "Add a note" button: */
#addButton{
	position:absolute;
	top:-70px;
	left:0;
}</pre>
<p>In the first part we define the appearance of the notes and provide three color schemes &#8211; yellow, blue and green. Those color classes are also used in the live preview form when creating a note.</p>
<p>Every note has special span element with a class name of <strong>data</strong>, which holds the internal ID it is assigned in the database. This ID is used by jQuery and returned with the AJAX calls back to the server in order to update the note&#8217;s position and z-index.  We are hiding this span from view with <strong>display:none</strong>.</p>
<div id="attachment_627" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i22.png"><img class="size-full wp-image-627" title="Three Styles Of Notes" src="http://tutorialzine.com/wp-content/uploads/2010/01/i22.png" alt="Three Styles Of Notes" width="620" height="260" /></a><p class="wp-caption-text">Three Styles Of Notes</p></div>
<h4>styles.css &#8211; Part 2</h4>
<pre class="brush:css">/* Green button class: */
a.green-button,a.green-button:visited{
	color:black;
	display:block;
	font-size:10px;
	font-weight:bold;
	height:15px;
	padding:6px 5px 4px;
	text-align:center;
	width:60px;

	text-shadow:1px 1px 1px #DDDDDD;
	background:url(img/button_green.png) no-repeat left top;
}

a.green-button:hover{
	text-decoration:none;
	background-position:left bottom;
}

.author{
	/* The author name on the note: */
	bottom:10px;
	color:#666666;
	font-family:Arial,Verdana,sans-serif;
	font-size:12px;
	position:absolute;
	right:10px;
}

#main{
	/* Contains all the notes and limits their movement: */
	margin:0 auto;
	position:relative;
	width:980px;
	height:500px;
	z-index:10;
	background:url(img/add_a_note_help.gif) no-repeat left top;
}</pre>
<p>The second part of the file defines the green button class, complete with a hover state and a CSS3 text-shadow. These are a few tiny details that may not look like much, but leave a good overall impression to the user.</p>
<h4>styles.css &#8211; Part 3</h4>
<pre class="brush:css">h3.popupTitle{
	border-bottom:1px solid #DDDDDD;
	color:#666666;
	font-size:24px;
	font-weight:normal;
	padding:0 0 5px;
}

#noteData{
	/* The input form in the pop-up: */
	height:200px;
	margin:30px 0 0 200px;
	width:350px;
}

.note-form label{
	display:block;
	font-size:10px;
	font-weight:bold;
	letter-spacing:1px;
	text-transform:uppercase;
	padding-bottom:3px;
}

.note-form textarea, .note-form input[type=text]{
	background-color:#FCFCFC;
	border:1px solid #AAAAAA;
	font-family:Arial,Verdana,sans-serif;
	font-size:16px;
	height:60px;
	padding:5px;
	width:300px;
	margin-bottom:10px;
}

.note-form input[type=text]{	height:auto; }

.color{
	/* The color swatches in the form: */
	cursor:pointer;
	float:left;
	height:10px;
	margin:0 5px 0 0;
	width:10px;
}

#note-submit{	margin:20px auto; }</pre>
<p>In the last part of styles.css, we add CSS rules for the live preview form, staring from the H3 heading and finishing with the color swatches on the bottom.</p>
<h3>Step 4 &#8211; jQuery</h3>
<p>jQuery manages the front-end and all of the AJAX requests. In order to be able to use the library, we first need to include a few lines to the head section of demo.php:</p>
<h4>demo.php</h4>
<pre class="brush:html">&lt;link rel="stylesheet" type="text/css" href="styles.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.2.6.css" media="screen" /&gt;

&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="fancybox/jquery.fancybox-1.2.6.pack.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</pre>
<p>We include jQuery and jQuery UI from Google&#8217;s content repository network, as well as the rest of the css and js files needed for this tutorial. Now lets dig a bit deeper into our jQuery script.</p>
<h4>script.js &#8211; Part 1</h4>
<pre class="brush:js">$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	var tmp;

	$('.note').each(function(){
		/* Finding the biggest z-index value of the notes */

		tmp = $(this).css('z-index');
		if(tmp&gt;zIndex) zIndex = tmp;
	})

	/* A helper function for converting a set of elements to draggables: */
	make_draggable($('.note'));

	/* Configuring the fancybox plugin for the "Add a note" button: */
	$("#addButton").fancybox({
		'zoomSpeedIn'		: 600,
		'zoomSpeedOut'		: 500,
		'easingIn'			: 'easeOutBack',
		'easingOut'			: 'easeInBack',
		'hideOnContentClick': false,
		'padding'			: 15
	});

	/* Listening for keyup events on fields of the "Add a note" form: */
	$('.pr-body,.pr-author').live('keyup',function(e){

		if(!this.preview)
			this.preview=$('#previewNote');

		/* Setting the text of the preview to the contents of the input field, and stripping all the HTML tags: */
		this.preview.find($(this).attr('class').replace('pr-','.')).html($(this).val().replace(/&lt;[^&gt;]+&gt;/ig,''));
	});

	/* Changing the color of the preview note: */
	$('.color').live('click',function(){

		$('#previewNote').removeClass('yellow green blue').addClass($(this).attr('class').replace('color',''));
	});</pre>
<p>First the script finds the maximum <strong>z-index</strong> value, so that it can cache it into the <strong>zIndex</strong> variable and increment it before assigning it to the notes in the beginning of every drag movement. This way, when you start dragging a note, it is moved to the top of the stack.</p>
<p>Another interesting moment is that we use the jQuery <strong>live()</strong> method to listen for events, instead of the regular <strong>bind()</strong>. This is so, because the page elements we are listening for events on, are created only when the form is shown and, once defined, <strong>live()</strong> event listeners are active on all elements yet to be created.</p>
<h4>script.js &#8211; Part 2</h4>
<pre class="brush:js">	/* The submit button: */
	$('#note-submit').live('click',function(e){

		if($('.pr-body').val().length&lt;4)
		{
			alert("The note text is too short!")
			return false;
		}

		if($('.pr-author').val().length&lt;1)
		{
			alert("You haven't entered your name!")
			return false;
		}

		$(this).replaceWith('&lt;img src="img/ajax_load.gif" style="margin:30px auto;display:block" /&gt;');

		var data = {
			'zindex'	: ++zIndex,
			'body'		: $('.pr-body').val(),
			'author'		: $('.pr-author').val(),
			'color'		: $.trim($('#previewNote').attr('class').replace('note',''))
		};

		/* Sending an AJAX POST request: */
		$.post('ajax/post.php',data,function(msg){

			if(parseInt(msg))
			{
				/* msg contains the ID of the note, assigned by MySQL's auto increment: */
				var tmp = $('#previewNote').clone();

				tmp.find('span.data').text(msg).end().css({'z-index':zIndex,top:0,left:0});
				tmp.appendTo($('#main'));

				make_draggable(tmp)
			}

			$("#addButton").fancybox.close();
		});

		e.preventDefault();
	})
});</pre>
<p>Here we are listening for the click event on the form submit link. Once clicked, the data is validated and sent with the <strong>$.post</strong> method. If the insert was successful, the lightbox is hidden and the newly created note is added to the page. Notice that we are using the <strong>make_draggable </strong>function, which is included below.</p>
<div id="attachment_629" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i31.png"><img class="size-full wp-image-629" title="The Note Position And Stacking Are Saved With AJAX" src="http://tutorialzine.com/wp-content/uploads/2010/01/i31.png" alt="The Note Position And Stacking Are Saved With AJAX" width="620" height="260" /></a><p class="wp-caption-text">The Note Position And Stacking Are Saved With AJAX</p></div>
<h4>script.js &#8211; Part 3</h4>
<pre class="brush:js">var zIndex = 0;

function make_draggable(elements)
{
	/* Elements is a jquery object: */
	elements.draggable({
		containment:'parent',
		start:function(e,ui){ ui.helper.css('z-index',++zIndex); },
		stop:function(e,ui){

			/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
			$.get('ajax/update_position.php',{
				x		: ui.position.left,
				y		: ui.position.top,
				z		: zIndex,
				id	: parseInt(ui.helper.find('span.data').html())
			});
		}
	});
}</pre>
<p>In the last part of script.js, we have the <strong>make_draggable</strong> function. It turns a set of jQuery elements passed as a parameter into draggable objects. I&#8217;ve moved this functionality into a separate function  because we need to create draggables in twice &#8211; once on initial page load, and once when we add a new note to the page.</p>
<h3>Step 5 &#8211; MySQL</h3>
<p>If you plan on running and building upon this demo, you&#8217;ll need to set up a working version on your server. You&#8217;ll need to execute the code from <strong>table.sql</strong> of the download archive in phpMyAdmin and fill in your database credentials in <strong>connect.php</strong>.</p>
<p>With this our AJAX-ed Sticky Note system is complete!</p>
<h3>Conclusion</h3>
<p>Today we created an Ajax Enabled Sticky Note Management System and demonstrated how we could use one of the readily available plug-ins for the jQuery library to build a dynamic interface, complete with a live preview.</p>
<p><strong>What do you think? How would you improve this code?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=myPLnq4pDRA:YkJtg0vR6q0:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=myPLnq4pDRA:YkJtg0vR6q0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=myPLnq4pDRA:YkJtg0vR6q0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=myPLnq4pDRA:YkJtg0vR6q0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=myPLnq4pDRA:YkJtg0vR6q0:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/myPLnq4pDRA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/01/sticky-notes-ajax-php-jquery/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/01/sticky-notes-ajax-php-jquery/</feedburner:origLink></item>
		<item>
		<title>Halftone Navigation Menu With jQuery &amp; CSS3</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/aPVjKw5pOUA/</link>
		<comments>http://tutorialzine.com/2010/01/halftone-navigation-menu-jquery-css/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 12:14:57 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=610</guid>
		<description><![CDATA[Today we are making a CSS3 &#038; jQuery halftone-style navigation menu, which will allow you to display animated halftone-style shapes in accordance with the navigation links, and will provide a simple editor for creating additional shapes as well.]]></description>
			<content:encoded><![CDATA[<p>Providing an intuitive, yet eye-catching navigation on your site, is one of the ingredients for a great design. And with the performance improvements in newer browsers and the rise of JavaScript frameworks such as jQuery, it is becoming easier to include beautiful animations in your page designs.</p>
<p>Today we are making a <strong>CSS3 &amp; jQuery</strong> halftone-style navigation menu, which will allow you to display animated halftone-style shapes in accordance with the navigation links, and will provide a simple editor for creating additional shapes as well.</p>
<blockquote><p><strong>Halftone</strong> is the reprographic technique that simulates continuous tone imagery through the use of dots, varying either in size or in spacing. &#8220;Halftone&#8221; can also be used to refer specifically to the image that is produced by this process.</p>
<p><a style="font-size: 10px;" href="http://en.wikipedia.org/wiki/Halftone" target="_blank">The original article at Wikipedia</a>.</p></blockquote>
<p>So lets start with step one!</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>The first step is to lay down the XHTML structure of the menu. The whole menu is positioned inside an unordered list <strong>UL</strong>, which is the most suitable way of organizing your site&#8217;s navigation. Inside each of the <strong>LI</strong> elements are the hyperlinks, that are later styled to look like buttons via some interesting CSS code that you will see in a moment.</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;div id="main"&gt;

&lt;div id="navigation"&gt;

&lt;ul class="menuUL"&gt;
&lt;!-- The class names that are assigned to the links correspond to name of the shape that is shown on hover: --&gt;
&lt;li&gt;&lt;a href="#" class="house"&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" class="wrench"&gt;Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" class="envelope"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#" class="info"&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="clear"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div id="stage"&gt;
&lt;!-- The dot divs are shown here --&gt;
&lt;/div&gt;

&lt;/div&gt;</pre>
<p>If you plan to use this menu on your site, you&#8217;ll first need to modify the <strong>href</strong> attributes, so that they correspond to your site&#8217;s structure.</p>
<p>Worth noticing are also the <strong>class</strong> names that are assigned to each of the links. Those are not used to style them, but rather to tell jQuery which shape to render.</p>
<p>Lastly, there is our <strong>#stage</strong> div, which is filled with special <strong>.dot</strong> divs that act as pixels for our shapes. You can create custom shapes by setting the menu in service mode, which is covered in step three.</p>
<div id="attachment_616" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i3.jpg"><img class="size-full wp-image-616" title="The Grid" src="http://tutorialzine.com/wp-content/uploads/2010/01/i3.jpg" alt="The Grid" width="620" height="260" /></a><p class="wp-caption-text">The Grid</p></div>
<h3>Step 2 &#8211; CSS</h3>
<p>It is time to add some life to the design, by specifying the CSS rules. I divided the style sheet in two parts. You can see the rest of the code in <strong>styles.css</strong> in the demonstration files.</p>
<h4>styles.css &#8211; Part 1</h4>
<pre class="brush:css">.menuUL li{
	/* This will arrange the LI-s next to each other */
	display:inline;
}

.menuUL li a,.menuUL li a:visited{
	/* Styling the hyperlinks of the menu as buttons */

	float:left;
	font-weight:bold;
	background:url(img/button_bg.jpg) repeat-x center bottom #666666;

	/* display:block allows for additional CSS rules to take effect, such as paddings: */
	display:block;
	border:1px solid #4D4D4D;
	color:#CCCCCC;
	border-top-color:#565656;

	padding:4px 6px;
	margin:4px 5px;
	height:16px;

	/* Setting a CSS3 box shadow around the button */

	-moz-box-shadow:0 0 1px black;
	-webkit-box-shadow:0 0 1px black;
	box-shadow:0 0 1px black;

	/* CSS3 text shadow */
	text-shadow:0 1px black;
}

.menuUL li a:hover{
	/* On hover show the top, lighter, part of the background: */
	background-position:center top;
	text-decoration:none;
}
</pre>
<p>In this first part, you can see that we are displaying the <strong>LI</strong> elements <strong>inline</strong>, which will arrange them one next to each other and thus allow us to form a horizontally oriented navigation menu in a compatible cross-browser fashion.</p>
<p>The hyperlinks inside them are displayed as block elements and floated to the left. Also some <strong>CSS3</strong> rules are applied such as <strong>box-shadow,</strong> for casting a shadow under the buttons, and <strong>text-shadow,</strong> for shadows under the text of the button.</p>
<p>Those are all small details that contribute to the overall feel of the page, but are in no way compulsory for the browsing process. This is great for users whose browsers don&#8217;t support CSS3 yet (most notably the IE family).</p>
<h4>styles.css &#8211; Part 2</h4>
<pre class="brush:css">#navigation{
	/* The navigation menu bar: */
	background:#222222;
	border:1px solid #111111;
	float:left;
	padding:5px 10px;
}

#navigation,.menuUL li a{
	/* CSS3 rounded corners for both the navigation bar and the buttons: */
	-moz-border-radius:4px;
	-webkit-border-radius:4px;
	-khtml-border-radius:4px;
	border-radius:4px;
}

#stage{
	/* The stage contains the individual divs that comprise the halftone icon: */
	height:300px;
	position:absolute;
	right:50px;
	top:20px;
	width:400px;
}

.dot{
	/* The stage contains 192 .dot divs: */
	float:left;
	height:25px;
	width:25px;
}

.dot.active{
	/* When assigned the active class, the div shows a background image of a dot: */
	background:url(img/dot.png) no-repeat center center;
}

.clear{
	/* Old-school clear fix hack to clear the floats: */
	clear:both;
}

#main{
	margin:0 auto;
	position:relative;
	width:900px;
}
</pre>
<p>In the lines above you can see the rest of the CSS3 rules that are used. There we add rounded corners via the <strong>border-radius</strong> property (supported by most of the modern browsers) for both the navigation bar and individual buttons at once.</p>
<div id="attachment_617" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i1.jpg"><img class="size-full wp-image-617" title="A Halftone Navigation Menu" src="http://tutorialzine.com/wp-content/uploads/2010/01/i1.jpg" alt="A Halftone Navigation Menu" width="620" height="460" /></a><p class="wp-caption-text">A Halftone Navigation Menu</p></div>
<h3>Step 3 &#8211; jQuery</h3>
<p>After finishing with all the styling, it is time to throw in some JavaScript. The first step is to include the jQuery library to the head section of the document we are working on.</p>
<p>As I mentioned earlier, there are two modes available for the navigation menu. The first one is the default one, which shows a previously defined shape in a smooth animation when you hover your mouse over a navigation button.</p>
<p>The second one allows you to create your own shapes by clicking on the grid. You can later export the shape and insert it as an array in the shapes object. To make jQuery show it, you just need to insert the name of your just-created shape in the <strong>class</strong> attribute of the navigation link you wish to associate it with.</p>
<p>Now lets see how this works.</p>
<h4>script.js &#8211; Part 1</h4>
<pre class="brush:js">/* Set serviceMode to true to create your own shapes: */
var serviceMode=false;

$(document).ready(function(){

	/* This code is executed after the DOM has been completely loaded */

	var str=[];
	var perRow = 16;

	/* Generating the dot divs: */
	for(var i=0;i&lt;192;i++)
	{
		str.push('&lt;div class="dot" id="d-'+i+'" /&gt;');
	}

	/* Joining the array into a string and adding it to the inner html of the stage div: */
	$('#stage').html(str.join(''));

	/* Using the hover method: */
	$('#navigation li a').hover(function(e){

		/* serviceDraw is a cut-out version of the draw function, used for editing and composing shapes: */

		if(serviceMode)
			serviceDraw($(this).attr('class'));
		else
			draw($(this).attr('class'));
	}, function(e){});

	/* Caching the dot divs into a variable for performance: */
	dots = $('.dot');

	if(serviceMode)
	{
		/* If we are in service mode, show borders around the dot divs, add the export link, and listen for clicks: */

		dots.css({
			border:'1px solid black',
			width:dots.eq(0).width()-2,
			height:dots.eq(0).height()-2,
			cursor:'pointer'
		});

		$('&lt;div/&gt;').css({
			position:'absolute',
			bottom:-20,
			right:0
		}).html('&lt;a href="" onclick="outputString();return false;"&gt;[Export Shape]&lt;/a&gt;').appendTo('#stage');

		dots.click(function(){
			$(this).toggleClass('active');
		});
	}
});
</pre>
<p>In the topmost part of the file is the <strong>serviceMode</strong> variable. By setting it to <strong>true,</strong> you can start creating your own shapes. Just don&#8217;t forget to set it back to false after you&#8217;ve finished, so that your visitors don&#8217;t see the grid and the export link. It would be even better if you keep a dedicated service mode version locally and use a different one for your site (this way you can also strip the unnecessary code for the service mode from the production version).</p>
<p>After the DOM has finished loading (on <strong>$(document).ready()</strong>) we populate the <strong>#stage</strong> with a grid of 192 divs (16 per row), that will form the pixels of the image.</p>
<h4>script.js &#8211; Part 2</h4>
<pre class="brush:js">var shapes={
	/* Each shape is described by an array of points. You can add your own shapes here, just don't forget to add a coma after each array, except for the last one */

	house:[22,37,38,39, .... 166,167,168,169],
	wrench:[22,23,24,25,26 .... 148,163],
	envelope:[34,35,36,37, .... 153,154,155,156],
	info:[22,23,38,39, .... 151,166,167,168]
}

var stopCounter = 0;
var dots;

function draw(shape)
{
	/* This function draws a shape from the shapes object */

	stopCounter++;
	var currentCounter = stopCounter;

	dots.removeClass('active').css('opacity',0);

	$.each(shapes[shape],function(i,j){

		setTimeout(function(){
		/* If a different shape animaton has been started during the showing of the current one, exit the function  */

		if(currentCounter!=stopCounter) return false;

 		dots.eq(j).addClass('active').fadeTo('slow',0.4);

		/* The fade animation is scheduled for 10*i millisecond into the future: */
	},10*i);

});
}

function serviceDraw(shape)
{
	/* A cut out version of the draw function, used in service mode */

	dots.removeClass('active');

	$.each(shapes[shape],function(i,j){
		dots.eq(j).addClass('active');
	});
}

function outputString()
{
	/* Exports the positions of the active dot divs as a comma-separated string: */

	var str=[];
	$('.dot.active').each(function(){
		str.push(this.id.replace('d-',''));
	})

	prompt('Insert this string as an array in the shapes object',str.join(','));
}
</pre>
<p>In the second part, you can see the <strong>shapes</strong> object. It contains four default shapes, which are set as class names to the navigation links. You can customize them by loading them in the service mode, or you can remove them completely and add your own.</p>
<p>After this we define the <strong>draw</strong>, <strong>serviceDraw</strong> and the <strong>outputString</strong> function. The latter two are used only in <strong>serviceMode</strong> and help you create your own shapes. If you do not plan to use the service mode, you can go ahead and remove them (just don&#8217;t forget to also remove lines <strong>34-54</strong> from the first part of <strong>script.js</strong> above).</p>
<div id="attachment_618" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i21.png"><img class="size-full wp-image-618" title="Creating Your Own Shapes" src="http://tutorialzine.com/wp-content/uploads/2010/01/i21.png" alt="Creating Your Own Shapes" width="620" height="460" /></a><p class="wp-caption-text">Creating Your Own Shapes</p></div>
<p>With this our CSS3 &amp; jQuery navigation menu is complete!</p>
<h3>Conclusion</h3>
<p>Today we created a sleek animated navigation menu with the help of <strong>jQuery and CSS3</strong>. The menu is compatible with all the major browsers and works perfectly fine even if JavaScript is disabled. As most of the XHTML markup necessary for the animation is inserted dynamically by JavaScript, the page holds a bare amount of code and is SEO friendly.</p>
<p><strong>What do you think? How would you improve this code?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=aPVjKw5pOUA:3cbXeui_WgQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=aPVjKw5pOUA:3cbXeui_WgQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=aPVjKw5pOUA:3cbXeui_WgQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=aPVjKw5pOUA:3cbXeui_WgQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=aPVjKw5pOUA:3cbXeui_WgQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/aPVjKw5pOUA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/01/halftone-navigation-menu-jquery-css/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/01/halftone-navigation-menu-jquery-css/</feedburner:origLink></item>
		<item>
		<title>Advanced Event Timeline With PHP, CSS &amp; jQuery</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/MI2TvnwgF-M/</link>
		<comments>http://tutorialzine.com/2010/01/advanced-event-timeline-with-php-css-jquery/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:00:46 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=591</guid>
		<description><![CDATA[Welcoming the new year and making an Advanced Event Timeline with the help of PHP, MySQL, CSS &#038; jQuery, that will display a pretty time line with clickable events. Adding new ones is going to be as easy as inserting a row in the database.]]></description>
			<content:encoded><![CDATA[<p>First I want to take the time to thank you, Tutorialzine&#8217;s readers, for all your support the past year and to wish you a happy new 2010! A lot of interesting tutorials are on their way and there are also some nice changes planned which I am sure you&#8217;ll like.</p>
<p>Now lets start the year with a brand new tut!</p>
<p>It all began last week, when I received this letter from one of our readers:</p>
<blockquote><p>Hi,</p>
<p>I love your site! It&#8217;s the best out there.</p>
<p>I was wondering if you could create one day a tutorial on how to create a sleek event timeline like Google&#8217;s 10th Birthday Timeline, available at <a href="http://www.google.com/tenthbirthday/#start" target="_blank">http://www.google.com/tenthbirthday/</a>. That would be much appreciated! I am sure many other people would be grateful for that tutorial too.</p>
<p>Thanks, and happy new year!</p>
<p>Vinnie</p></blockquote>
<p>I am always on the look for good tutorial ideas, so Vinnie sent his email right on time!</p>
<p>Today we are making an Advanced Event Timeline with the help of PHP, MySQL, CSS &amp; jQuery, that will display a pretty time line with clickable events. Adding new ones is going to be as easy as inserting a row in the database.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>First, be sure to grab the example files from the button above, so that you can easily follow what is going on.</p>
<p>The first step of this tut is to create the XHTML structure, as seen in <strong>demo.php</strong>.</p>
<h4>demo.php</h4>
<pre class="brush:html">&lt;div id="timelineLimiter"&gt; &lt;!-- Hides the overflowing timelineScroll div --&gt;
&lt;div id="timelineScroll"&gt; &lt;!-- Contains the timeline and expands to fit --&gt;

&lt;!-- PHP code that generates the event list --&gt;

&lt;div class="clear"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div id="scroll"&gt; &lt;!-- The year time line --&gt;
&lt;div id="centered"&gt; &lt;!-- Sized by jQuery to fit all the years --&gt;
&lt;div id="highlight"&gt;&lt;/div&gt; &lt;!-- The light blue highlight shown behind the years --&gt;
&lt;?php echo $scrollPoints ?&gt; &lt;!-- This PHP variable holds the years that have events --&gt;
&lt;div class="clear"&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;div id="slider"&gt; &lt;!-- The slider container --&gt;
&lt;div id="bar"&gt; &lt;!-- The bar that can be dragged --&gt;
&lt;div id="barLeft"&gt;&lt;/div&gt;  &lt;!-- Left arrow of the bar --&gt;
&lt;div id="barRight"&gt;&lt;/div&gt;  &lt;!-- Right arrow, both are styled with CSS --&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;</pre>
<p>I have omitted some of the PHP code that generates the events so we can take a better look at the markup (we will get back to it in the next step).</p>
<p>Te main idea is that we have two divs &#8211; <strong>timelineLimiter</strong> and <strong>timelineScroll</strong> positioned inside it. The former takes the width of the screen, and the latter is expanded to fit all the event sections that are inserted inside it. This way only a part of the larger inner div is visible and the rest can be scrolled to the left and right by a jQuery slider we will be making in step 4.</p>
<p>Now lets take a look at the PHP back-end.</p>
<div id="attachment_594" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i2.png"><img class="size-full wp-image-594" title="Advanced Event Timeline With PHP MySQL CSS jQuery" src="http://tutorialzine.com/wp-content/uploads/2010/01/i2.png" alt="Advanced Event Timeline With PHP MySQL CSS jQuery" width="620" height="260" /></a><p class="wp-caption-text">Advanced Event Timeline With PHP MySQL CSS jQuery</p></div>
<h3>Step 2 &#8211; PHP</h3>
<p>PHP selects all the events in the database and groups the events by year in the <strong>$dates</strong> array. It later loops through it and outputs all the events as <strong>&lt;li&gt;</strong> elements inside of unordered lists which belong to each of the event years.</p>
<h4>demo.php</h4>
<pre class="brush:php">// We first select all the events from the database ordered by date:

$dates = array();
$res = mysql_query("SELECT * FROM timeline ORDER BY date_event ASC");

while($row=mysql_fetch_assoc($res))
{
	// Store the events in an array, grouped by years:
	$dates[date('Y',strtotime($row['date_event']))][] = $row;
}

$colors = array('green','blue','chreme');
$scrollPoints = '';

$i=0;
foreach($dates as $year=&gt;$array)
{
	// Loop through the years:

	echo '
		&lt;div class="event"&gt;
		&lt;div class="eventHeading '.$colors[$i++%3].'"&gt;'.$year.'&lt;/div&gt;
		&lt;ul class="eventList"&gt;';

	foreach($array as $event)
	{
		// Loop through the events in the current year:

		echo '&lt;li class="'.$event['type'].'"&gt;
			&lt;span class="icon" title="'.ucfirst($event['type']).'"&gt;&lt;/span&gt;
			'.htmlspecialchars($event['title']).'

 			&lt;div class="content"&gt;
				&lt;div class="body"&gt;'.($event['type']=='image'?'&lt;div style="text-align:center"&gt;&lt;img src="'.$event['body'].'" alt="Image" /&gt;&lt;/div&gt;':nl2br($event['body'])).'&lt;/div&gt;
				&lt;div class="title"&gt;'.htmlspecialchars($event['title']).'&lt;/div&gt;
				&lt;div class="date"&gt;'.date("F j, Y",strtotime($event['date_event'])).'&lt;/div&gt;
			&lt;/div&gt;
 			&lt;/li&gt;';
	}

	echo '&lt;/ul&gt;&lt;/div&gt;';

 	// Generate a list of years for the time line scroll bar:
	$scrollPoints.='&lt;div class="scrollPoints"&gt;'.$year.'&lt;/div&gt;';

}</pre>
<p>Thus the complete markup for the page is generated. Now we are ready to apply some styles.</p>
<div id="attachment_595" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i1.png"><img class="size-full wp-image-595" title="The Event Sections" src="http://tutorialzine.com/wp-content/uploads/2010/01/i1.png" alt="The Event Sections" width="620" height="260" /></a><p class="wp-caption-text">The Event Sections</p></div>
<h3>Step 3 &#8211; CSS</h3>
<p>After we&#8217;ve inserted the CSS stylesheet to the head section of the document, we can start laying down the rules. Only the more interesting ones are included here. You can view the rest in <strong>styles.css</strong>.</p>
<h4>styles.css</h4>
<pre class="brush:css">.event{
	/* Contains the section title and list with events */
	float:left;
	padding:4px;
	text-align:left;
	width:300px;
	margin:0 5px 50px;
}

.eventList li{
	/* The individual events */
	background:#F4F4F4;
	border:1px solid #EEEEEE;
	list-style:none;
	margin:5px;
	padding:4px 7px;

	/* CSS3 rounded corners */
	-moz-border-radius:4px;
	-webkit-border-radius:4px;
	border-radius:4px;
}

.eventList li:hover{
	/* The hover state: */
	cursor:pointer;
	background:#E6F8FF;
	border:1px solid #D4E6EE;
	color:#548DA5;
}

li span{
	/* The event icon */
	display:block;
	float:left;
	height:16px;
	margin-right:5px;
	width:16px;
}

/* Individual background images for each type of event: */

li.news span.icon { 	background:url(img/icons/newspaper.png) no-repeat; }
li.image span.icon { 	background:url(img/icons/camera.png) no-repeat; }
li.milestone span.icon { 	background:url(img/icons/chart.png) no-repeat; }

#timelineLimiter{
	/* Hides the overflowing timeline */
	width:100%;
	overflow:hidden;
	padding-top:10px;
	margin:40px 0;
}

#scroll{
	/* The small timeline below the main one. Hidden here and shown by jQuery if JS is enabled: */
	display:none;
	height:30px;

	background:#F5F5F5;
	border:1px solid #EEEEEE;
	color:#999999;
}

.scrollPoints{
	/* The individual years */
	float:left;
	font-size:1.4em;
	padding:4px 10px;
	text-align:center;
	width:100px;

	position:relative;
	z-index:10;
}</pre>
<p>Here the .event class styles the event years sections (these are the divs that group events that have happened in the same year).</p>
<p>Near the middle of the code you can see that we&#8217;ve used some CSS3 rounded corners which work in the majority of browsers (not supported by IE and Opera).</p>
<p>We also define individual background images for each of the event types &#8211; image, news or milestone.</p>
<h3>Step 4 &#8211; jQuery</h3>
<p>The last step is to insert a layer of interactivity into the browser. We will be doing this with the help of the jQuery JavaScript library, included in the head section of <strong>demo.php</strong>.</p>
<p>I&#8217;ve split the code below in two parts so they are more comprehensible.</p>
<h4>script.js &#8211; Part 1</h4>
<pre class="brush:js">$(document).ready(function(){

	/* This code is executed after the DOM has been completely loaded */

	/* The number of event sections / years with events */

	var tot=$('.event').length;

	$('.eventList li').click(function(e){

		showWindow('&lt;div&gt;'+$(this).find('div.content').html()+'&lt;/div&gt;');
	});

	/* Each event section is 320 px wide */
	var timelineWidth = 320*tot;

	var screenWidth = $(document).width();

	$('#timelineScroll').width(timelineWidth);

	/* If the timeline is wider than the screen show the slider: */
	if(timelineWidth &gt; screenWidth)
	{
		$('#scroll,#slider').show();
		$('#centered,#slider').width(120*tot);

		/* Making the scrollbar draggable: */
		$('#bar').width((120/320)*screenWidth).draggable({

			containment: 'parent',
			drag: function(e, ui) {

			if(!this.elem)
			{
				/* This section is executed only the first time the function is run for performance */

				this.elem = $('#timelineScroll');

				/* The difference between the slider's width and its container: */
				this.maxSlide = ui.helper.parent().width()-ui.helper.width();

				/* The difference between the timeline's width and its container */
				this.cWidth = this.elem.width()-this.elem.parent().width();

				this.highlight = $('#highlight');
			}

			/* Translating each movement of the slider to the timeline: */
			this.elem.css({marginLeft:'-'+((ui.position.left/this.maxSlide)*this.cWidth)+'px'});

			/* Moving the highlight: */
			this.highlight.css('left',ui.position.left)
		}

	});

	$('#highlight').width((120/320)*screenWidth-3);
}
});</pre>
<p>As you may have noticed in the PHP section of the tut (if not check it out &#8211; around line 33) that with each event we include a set of div elements which contain additional information (title, text and date). Those are hidden with <strong>display:none</strong> in our CSS file, and are accessed by jQuery so that the pop-up window can be populated with data without the need of sending AJAX requests (not to mention that this content is visible to search engines and is great for SEO). So it is a win-win solution.</p>
<p>The data itself is being fetched in the second part of the script below:</p>
<h4>script.js &#8211; Part 2</h4>
<pre class="brush:js">function showWindow(data)
{
	/* Each event contains a set of hidden divs that hold
additional information about the event: */

	var title = $('.title',data).text();
	var date = $('.date',data).text();
	var body = $('.body',data).html();

	$('&lt;div id="overlay"&gt;').css({

		width:$(document).width(),
		height:$(document).height(),
		opacity:0.6

	}).appendTo('body').click(function(){

		$(this).remove();
		$('#windowBox').remove();
 	});

 	$('body').append('&lt;div id="windowBox"&gt;&lt;div id="titleDiv"&gt;'+title+'&lt;/div&gt;'+body+'&lt;div id="date"&gt;'+date+'&lt;/div&gt;&lt;/div&gt;');

 	$('#windowBox').css({

		width:500,
		height:350,
		left: ($(window).width() - 500)/2,
		top: ($(window).height() - 350)/2
	});
}</pre>
<p>In this function we are basically treating the parameter passed from Part 1 above, where the function is called, as regular HTML and use the standard jQuery selectors thus populating the title, date and body variables.</p>
<div id="attachment_596" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2010/01/i3.png"><img class="size-full wp-image-596" title="The Timeline Explained" src="http://tutorialzine.com/wp-content/uploads/2010/01/i3.png" alt="The Timeline Explained" width="620" height="460" /></a><p class="wp-caption-text">The Timeline Explained</p></div>
<h3>Step 5 &#8211; MySQL</h3>
<p>This last step is only needed if you plan to run the demo on your own server, or like an addition to your current site.</p>
<p>To make it all tick, you have to recreate the timeline MySQL table from <strong>timeline.sql</strong>, provided in the download archive. You will also need to fill in your database credentials in <strong>connect.php</strong>.</p>
<p>With this our Event Timeline is complete!</p>
<h3>Conclusion</h3>
<p>Today we created a timeline that you can modify to showcase the important events that mark your days. It is easily modifiable and you are free to use it in your or your clients&#8217; projects.</p>
<p><strong>Be sure to leave a comment if you liked it : )</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=MI2TvnwgF-M:wNHpkP7N8L4:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=MI2TvnwgF-M:wNHpkP7N8L4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=MI2TvnwgF-M:wNHpkP7N8L4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=MI2TvnwgF-M:wNHpkP7N8L4:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=MI2TvnwgF-M:wNHpkP7N8L4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/MI2TvnwgF-M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2010/01/advanced-event-timeline-with-php-css-jquery/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2010/01/advanced-event-timeline-with-php-css-jquery/</feedburner:origLink></item>
		<item>
		<title>A Colorful Clock With CSS &amp; jQuery</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/DQam1GFEplM/</link>
		<comments>http://tutorialzine.com/2009/12/colorful-clock-jquery-css/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 20:13:10 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=580</guid>
		<description><![CDATA[In this tutorial we are going to create a colorful jQuery &#038; CSS clock, which will get the user's local time and display a colorful animated clock. It uses our new &#038; just released tzineClock plug-in for jQuery.]]></description>
			<content:encoded><![CDATA[<p>The first wave of Christmas holidays passed and we are looking forward to New Year&#8217;s Eve for a fresh start at all those things we failed to do the last 12 months.</p>
<p>And in the mood of the upcoming holiday, this week we are going to make a <a href="http://tutorialzine.com/2009/12/colorful-content-accordion-css-jquery/" target="_blank">colorful</a> jQuery &amp; CSS clock, which will help you keep track of those precious last seconds of the year.</p>
<p>This is also the first tutorial that features our first very own jQuery plug-in &#8211; <strong>tzineClock</strong> (soon to be released officially in a post of its own).</p>
<p>Go ahead, download the demo files and continue with step one.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>As usual, we start with the XHTML markup. The difference is, that the XHTML is not contained in <strong>demo.html</strong>, but is dynamically inserted into the page by jQuery (well there is some markup left there after all we need at least one container div for the clock to be inserted in).</p>
<p>This saves us from having to manually type similar blocks of code for each one of the dials (there are three of them, one for the hours, the minutes and the seconds).</p>
<p>Lets take a look at the XHTML that is inserted by jQuery:</p>
<h4>jquery.tzineClock.js</h4>
<pre class="brush:html">&lt;!-- The first class (green in this case) is assigned dynamically --&gt;
&lt;div class="green clock"&lt;/div&gt;

&lt;!-- This div holds the value of the unit monitored - seconds, minutes or hours --&gt;
&lt;div class="display"&gt;&lt;/div&gt;

&lt;!-- A black area that hides the underlying background --&gt;
&lt;div class="front left"&gt;&lt;/div&gt;

&lt;!-- The left part of the background: --&gt;
&lt;div class="rotate left"&gt;
&lt;div class="bg left"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;!-- The right part of the background: --&gt;
&lt;div class="rotate right"&gt;
&lt;div class="bg right"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;</pre>
<p>This code is contained in <strong>jquery.tzineClock/jquery.tzineClock.js</strong>. It is generated three times &#8211; once for the hours, minutes and seconds. Those are later animated and updated every second, as you&#8217;ll see in a moment.</p>
<p>There are three classes that are assigned to the topmost container during the generation process &#8211; green, blue and orange. Just by assigning one of those classes, we change the color of the dial.</p>
<p>Lets continue with the next step.</p>
<div id="attachment_585" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2009/12/i13.jpg"><img class="size-full wp-image-585" title="A Colorful jQuery &amp; CSS Clock" src="http://tutorialzine.com/wp-content/uploads/2009/12/i13.jpg" alt="A Colorful jQuery &amp; CSS Clock" width="620" height="260" /></a><p class="wp-caption-text">A Colorful jQuery &amp; CSS Clock</p></div>
<h3>Step 2 &#8211; CSS</h3>
<p>Before our style sheets can have any effect on the page, we have to include them in the head section of the file:</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;link rel="stylesheet" type="text/css" href="styles.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="jquery.tzineClock/jquery.tzineClock.css" /&gt;</pre>
<p>Those lines import <strong>styles.css</strong> and <strong>jquery.tzineClock.css</strong> in the page. The first one styles the demo page, and the second &#8211; the colorful dials (it is part of the plugin).</p>
<p>We can now take a closer look at the CSS rules.</p>
<h4>styles.css</h4>
<pre class="brush:css">body,h1,h2,h3,p,quote,small,form,input,ul,li,ol,label{
	/* Simple page reset */
	margin:0;
	padding:0;
}

body{
	/* Setting default text color, background and a font stack */
	color:#dddddd;
	font-size:13px;
	background: #302b23;
	font-family:Arial, Helvetica, sans-serif;
}

#fancyClock{
	margin:40px auto;
	height:200px;
	border:1px solid #111111;
	width:600px;
}</pre>
<p>Those few lines are all that is needed to style the demo page. We first implement a simple CSS reset, which will insure that the elements on the page look the same across the different browsers.</p>
<p>Later we style the body of the page and finally the <strong>fancyClock</strong> div, in which we will later insert the three dials.</p>
<h4>jquery.tzineClock.css</h4>
<pre class="brush:css">.clock{
	/* The .clock div. Created dynamically by jQuery */
	background-color:#252525;
	height:200px;
	width:200px;
	position:relative;
	overflow:hidden;
	float:left;
}

.clock .rotate{
	/* There are two .rotate divs - one for each half of the background */
	position:absolute;
	width:200px;
	height:200px;
	top:0;
	left:0;
}

.rotate.right{
	display:none;
	z-index:11;
}

.clock .bg, .clock .front{
	width:100px;
	height:200px;
	background-color:#252525;
	position:absolute;
	top:0;
}

.clock .display{
	/* Holds the number of seconds, minutes or hours respectfully */
	position:absolute;
	width:200px;
	font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
	z-index:20;
	color:#F5F5F5;
	font-size:60px;
	text-align:center;
	top:65px;
	left:0;

	/* CSS3 text shadow: */
	text-shadow:4px 4px 5px #333333;
}

/* The left part of the background: */

.clock .bg.left{ left:0; }

/* Individual styles for each color: */
.orange .bg.left{ background:url(img/bg_orange.png) no-repeat left top; }
.green .bg.left{ background:url(img/bg_green.png) no-repeat left top; }
.blue .bg.left{	background:url(img/bg_blue.png) no-repeat left top; }

/* The right part of the background: */
.clock .bg.right{ left:100px; }

.orange .bg.right{ background:url(img/bg_orange.png) no-repeat right top; }
.green .bg.right{ background:url(img/bg_green.png) no-repeat right top; }
.blue .bg.right{ background:url(img/bg_blue.png) no-repeat right top; }

.clock .front.left{
	left:0;
	z-index:10;
}</pre>
<p><strong>jquery.tzineClock.css</strong> is a part of our plugin (alongside <strong>jquery.tzineClock.js</strong>) and it styles the colorful dials themselves.</p>
<p>One of the more interesting moments is the use of individual rules that style the colors of the dials, as I mentioned in step one.</p>
<p>You can learn more about the animation from the illustration below:</p>
<div id="attachment_584" class="wp-caption alignnone" style="width: 630px"><a href="http://tutorialzine.com/wp-content/uploads/2009/12/i22.png"><img class="size-full wp-image-584" title="The animation explained" src="http://tutorialzine.com/wp-content/uploads/2009/12/i22.png" alt="The animation explained" width="620" height="460" /></a><p class="wp-caption-text">The animation explained</p></div>
<h3>Step 3 &#8211; jQuery</h3>
<p>Moving all the JavaScript to the plugin makes it really easy to reuse the code and at the same time enables us to leverage the power of jQuery&#8217;s selectors and methods.</p>
<p>To be able to use the jQuery library, we first need to include a couple of scripts in the page:</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.tzineClock/jquery.tzineClock.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</pre>
<p>The first file is the library itself, included from Google&#8217;s CDN, later we have the plug-in and lastly the script file that runs the demo.</p>
<h4>script.js</h4>
<pre class="brush:js">$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	$('#fancyClock').tzineClock();

});</pre>
<p>If you&#8217;ve followed some of our previous tutorials, you are probably expecting to see some 50+ lines of code here, but this time our scripts file contains only one line of code &#8211; a call to our plug-in.</p>
<p>This makes it extremely easy to include the code in an existing site (which is the purpose of jquery plugins in the first place).</p>
<p>Lets dig a little deeper into the plugin:</p>
<h4>jquery.tzineClock.js &#8211; Part 1</h4>
<pre class="brush:js">(function($){
	// A global object used by the functions of the plug-in:
	var gVars = {};

	// Extending the jQuery core:

	$.fn.tzineClock = function(opts){

		// "this" contains the elements that were selected when calling the plugin: 		$('elements').tzineClock();
		// If the selector returned more than one element, we use the first one:
		var container = this.eq(0);
		if(!container)
		{
			try{
				console.log("Invalid selector!");
			} catch(e){}

			return false;
		}

		if(!opts) opts = {};

		var defaults = {
			/* Additional options will be added in future versions of the plugin. */
		};

		/* Merging the provided options with the default ones (will be used in future versions of the plugin): */

		$.each(defaults,function(k,v){
			opts[k] = opts[k] || defaults[k];
		});

		// Calling the setUp function and passing the container,
		// will be available to the setUp function as "this":

		setUp.call(container);

		return this;
	}

	function setUp()
	{
		// The colors of the dials:
		var colors = ['orange','blue','green'];

		var tmp;
		for(var i=0;i&lt;3;i++)
		{
			// Creating a new element and setting the color as a class name:

			tmp = $('&lt;div&gt;').attr('class',colors[i]+' clock').html(
				'&lt;div class="display"&gt;&lt;/div&gt;'+
				'&lt;div class="front left"&gt;&lt;/div&gt;'+
				'&lt;div class="rotate left"&gt;'+
				'&lt;div class="bg left"&gt;&lt;/div&gt;'+
				'&lt;/div&gt;'+
				'&lt;div class="rotate right"&gt;'+
				'&lt;div class="bg right"&gt;&lt;/div&gt;'+
				'&lt;/div&gt;'
			);

			// Appending to the fancyClock container:
			$(this).append(tmp);

			// Assigning some of the elements as variables for speed:
			tmp.rotateLeft = tmp.find('.rotate.left');
			tmp.rotateRight = tmp.find('.rotate.right');
			tmp.display = tmp.find('.display');

			// Adding the dial as a global variable. Will be available as gVars.colorName
			gVars[colors[i]] = tmp;
		}

		// Setting up a interval, executed every 1000 milliseconds:
		setInterval(function(){

			var currentTime = new Date();
			var h = currentTime.getHours();
			var m = currentTime.getMinutes();
			var s = currentTime.getSeconds();

			animation(gVars.green, s, 60);
			animation(gVars.blue, m, 60);
			animation(gVars.orange, h, 24);
		},1000);
	}</pre>
<p>Making a plug-in for jQuery comes down to defining a custom function through the <strong>jQuery.fn</strong> method. This way your function is available on any elements that you normally use jQuery on.</p>
<p>For example, in <strong>script.js</strong> we select the div width an id of <strong>fancyClock</strong> and use the tzineClock() method on it: <strong>$(&#8216;#fancyClock&#8217;).tzineClock();</strong>. The elements we selected are later passed to the tzineClock function and are available through the &#8220;<strong>this</strong>&#8221; property.</p>
<p>I have left place for future improvements of the plugin, like passing configuration options for the dimensions of the clock, color themes etc. Those will however be implemented in future releases of the plugin.</p>
<p>Because there might be more than one element selected, we extract only the first one of the set with the <strong>eq(0)</strong> method. Later we have the <strong>setUp()</strong> function that inserts the markup for the dials and sets up the interval which will update the figures every second.</p>
<h4>jquery.tzineClock.js &#8211; Part 2</h4>
<pre class="brush:js">	function animation(clock, current, total)
	{
		// Calculating the current angle:
		var angle = (360/total)*(current+1);

		var element;

		if(current==0)
		{
			// Hiding the right half of the background:
			clock.rotateRight.hide();

			// Resetting the rotation of the left part:
			rotateElement(clock.rotateLeft,0);
		}

		if(angle&lt;=180)
		{
			// The left part is rotated, and the right is currently hidden:
			element = clock.rotateLeft;
		}
		else
		{
			// The first part of the rotation has completed, so we start rotating the right part:
			clock.rotateRight.show();
			clock.rotateLeft.show();

			rotateElement(clock.rotateLeft,180);
			element = clock.rotateRight;

			angle = angle-180;
		}

		rotateElement(element,angle);

		// Setting the text inside of the display element, inserting a leading zero if needed:
		clock.display.html(current&lt;10?'0'+current:current);
	}

	function rotateElement(element,angle)
	{
		// Rotating the element, depending on the browser:
		var rotate = 'rotate('+angle+'deg)';

		if(element.css('MozTransform')!=undefined)
			element.css('MozTransform',rotate);

		else if(element.css('WebkitTransform')!=undefined)
			element.css('WebkitTransform',rotate);

		// A version for internet explorer using filters, works but is a bit buggy (no surprise here):

		else if(element.css("filter")!=undefined)
		{
			var cos = Math.cos(Math.PI * 2 / 360 * angle);
			var sin = Math.sin(Math.PI * 2 / 360 * angle);

			element.css("filter","progid:DXImageTransform.Microsoft.Matrix(M11="+cos+",M12=-"+sin+",M21="+sin+",M22="+cos+",SizingMethod='auto expand',FilterType='nearest neighbor')");
			element.css("left",-Math.floor((element.width()-200)/2));
			element.css("top",-Math.floor((element.height()-200)/2));
		}
	}
})(jQuery)</pre>
<p>The last two of the functions used by the plug-in are <strong>animation</strong> and <strong>rotateElement</strong>. The first one updates the dials according to the value passed (we also pass a parameter with the maximum value so that the function can calculate the rotation).</p>
<p>The next function is the one that actually rotates the passed element. The rotation works only for Firefox, Safari, Chrome and IE6+. Internet Explorer does not support the <strong>CSS3 rotation</strong> used by the other browsers, but provides a proprietary filter property which allows for a similar transformation.</p>
<p>With this our colorful jQuery clock is complete!</p>
<h3>Conclusion</h3>
<p>Today we created a colorful clock with the help of CSS, jQuery and our first plug-in. You are free to use the code given here in your own projects. As a bonus, I&#8217;ve included the <strong>PSD</strong> file that I used to make the backgrounds, so you can easily create new colors and designs for the dials.</p>
<p><strong>What do you think? How would you improve this code?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=DQam1GFEplM:5nDb38vCrrc:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=DQam1GFEplM:5nDb38vCrrc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=DQam1GFEplM:5nDb38vCrrc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=DQam1GFEplM:5nDb38vCrrc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=DQam1GFEplM:5nDb38vCrrc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/DQam1GFEplM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2009/12/colorful-clock-jquery-css/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2009/12/colorful-clock-jquery-css/</feedburner:origLink></item>
		<item>
		<title>Christmas Wishes With Facebook Connect</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/sYsG9XkcJb0/</link>
		<comments>http://tutorialzine.com/2009/12/christmas-wishes-facebook-connect/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 01:52:01 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=565</guid>
		<description><![CDATA[Today, we will develop a webapp which will allow users to add christmas wishes using facebook connect and share them with their friends. We are using CSS, jQuery, PHP, AJAX and MySQL for data storage.]]></description>
			<content:encoded><![CDATA[<p>Providing a simple way for users to authenticate themselves can turn a web application in a much more personal place. But a registration is a time taking process which visitors prefer to go without.</p>
<p>This is exactly where services such as Facebook Connect step in. They let you dive inside their multimillion user base, skipping that tedious registration process that drives people off. Another benefit is that Connect enables you to communicate user activity back to facebook, which is a great promotional tool.</p>
<p>Today, we will develop a webapp which will allow users to add christmas wishes using facebook connect and share them with their friends. We are using CSS, jQuery, PHP, AJAX and MySQL for data storage.</p>
<p>Feel free to download the tutorial files and continue with step one.</p>
<h3>Step 1 &#8211; Creating a facebook application</h3>
<p>The first step you have to take, is to enable developer functionality for your facebook account.  Providing that you already have a registration, you have to visit the <a href="http://facebook.com/developers/" target="_blank">following URL</a> and install the developer application.</p>
<p>After you&#8217;ve done that, you&#8217;ll need to set up a new application, which will generate a special API key you&#8217;ll later have to include in your JavaScript files (more on that in a moment).</p>
<p>For more information on facebook connect, you can watch this <a href="http://www.facebook.com/video/video.php?v=630563174283" target="_blank">great video by the facebook team</a>, and visit the <a href="http://wiki.developers.facebook.com/index.php/Facebook_Connect_Tutorial1" target="_blank">facebook wiki</a>.</p>
<div id="attachment_569" class="wp-caption alignnone" style="width: 630px"><img class="size-full wp-image-569" title="Facebook Developers" src="http://tutorialzine.com/wp-content/uploads/2009/12/i12.jpg" alt="Facebook Developers" width="620" height="260" /><p class="wp-caption-text">Facebook Developers</p></div>
<p>The main idea is that we are going to allow users to authenticate themselves with facebook connect, and store only their unique facebook IDs in the database as their identity. We are later using Connect to dynamically fetch additional details about the person (such as the profile picture and the person&#8217;s name) and insert it directly into the page.</p>
<p>Another benefit of using Connect is that the wish can easily be posted to the user&#8217;s wall with a link to your web app &#8211; a great way to promote your site.</p>
<h3>Step 2 &#8211; XHTML</h3>
<p>Now that we&#8217;ve set everything up, we can start off by laying down the XHTML structure of the site.</p>
<p>The main part of the markup is generated at run time by the PHP back-end and inserted into the page at designated places.</p>
<h4>index.php</h4>
<pre class="brush:html">&lt;div id="main"&gt;

&lt;h1 class="tutTitle"&gt;Christmas Wishes With Facebook Connect&lt;/h1&gt;

&lt;div id="wishesContainer"&gt;

&lt;?php
// Outputting the wishes to the page:
echo $wishes;
?&gt;

&lt;/div&gt;

&lt;?php

// If this is the first page, show the submit form:

if($page==1):
?&gt;

&lt;div id="addWish" class="wish add"&gt;
&lt;div class="stage"&gt;
&lt;div class="topIcon"&gt;&lt;/div&gt;
&lt;div class="profile-pic"&gt;&lt;fb:profile-pic uid="1" facebook-logo="true" size="s"&gt;&lt;/fb:profile-pic&gt;&lt;/div&gt;
&lt;div class="name"&gt;&lt;/div&gt;
&lt;div id="share" class="body"&gt;

&lt;div class="toggleContent"&gt;
Connect with facebook to post a wish&lt;br /&gt;
&lt;fb:login-button onlogin=""&gt;&lt;/fb:login-button&gt;
&lt;/div&gt;

&lt;div class="toggleContent" style="display:none"&gt;
&lt;form action="" method="post" id="wForm"&gt;
&lt;p&gt;
&lt;textarea name="wish" id="wish" cols="20" rows="2"&gt;&lt;/textarea&gt;
&lt;input name="" id="sbutton" type="submit" value="Submit" /&gt;
&lt;label for="cb"&gt;Post my wish to facebook as well&lt;/label&gt;
&lt;input name="cb" id="cb" type="checkbox" value="1" checked="checked" /&gt;
&lt;input name="key" id="key" type="hidden" value="&lt;?php echo $_SESSION['key'] ?&gt;" /&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="clear"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</pre>
<p>On line 9 you can see that we output the contents of the <strong>$wishes</strong> variable on the page. It holds all the markup formed according to the individual entries in the database (covered in detail in Step 4).</p>
<p>Also worth noting are those unfamiliar <strong>fb:</strong> tags that you&#8217;ve probably never seen before.  Those are special tags used by facebook (hence the <strong>fb</strong> prefix) and are dynamically replaced with real data from facebook&#8217;s severs based on the attributes of those tags.</p>
<p>But in order to use those, we have to insert a special <strong>xmlns</strong> (XML namespace) attribute in the opening <strong>&lt;html&gt;</strong> tag of our document:</p>
<pre class="brush:html">&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"&gt;</pre>
<p>Also you can see the submit form that allows the visitors to add their Christmas wishes to the webapp. Worth noting is that this form is only shown if the user is on the first (or home) page of the site.</p>
<p>Lets continue with the next step.</p>
<div id="attachment_570" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-570" title="Christmas Wishes With Facebook Connect" src="http://tutorialzine.com/wp-content/uploads/2009/12/i4.jpg" alt="Christmas Wishes With Facebook Connect" width="600" height="460" /><p class="wp-caption-text">Christmas Wishes With Facebook Connect</p></div>
<h3>Step 3 &#8211; CSS</h3>
<p>Functionality is nothing without looks. So lets add in some styles.</p>
<p>All of our styles are positioned positioned in<strong> demo.css</strong>. I&#8217;ve divided the code into three chunks so it is easier to digest:</p>
<h4>demo.css &#8211; Part 1</h4>
<pre class="brush:css">body{
	/* Setting default text color, background and a font stack */
	color:#eeeeee;
	font-size:13px;
	background:url(img/flakes.jpg) repeat;
	font-family:Arial, Helvetica, sans-serif;
}

h1.tutTitle{
	/* Hiding the H1 - it is only visble by search engines */
	display:none;
}

#main{
	/* The main container */
	margin:15px auto;
	text-align:center;
	width:865px;
	position:relative;
	display:none;
}

a, a:visited {
	color:#0196e3;
	text-decoration:none;
	outline:none;
}

a:hover{
	text-decoration:underline;
}

#header{
	width:100%;
	height:160px;

	/* Setting the background of the header to show in the center of the page
	   and blend seemlessly with the background color: */

	background:url(img/header_bg.jpg) no-repeat #ad0303 center bottom;
	border-bottom:1px solid #d84b4e;
	text-align:center;
	margin-bottom:60px;

	/* Relative positioning, so that the #backToTut div is positioned accordingly */
	position:relative;
}

#backToTut{
	/* Positioning the "Back to the tut" link */
	position:absolute;
	right:30px;
	top:40px;
}

#logo{
	width:880px;
	text-align:left;
	margin:0 auto;
}

.wish{
	/* A common class, shared by all .wish divs */
	padding:10px;
	text-align:left;
	width:600px;
	margin:0 auto 45px;
	position:relative;
}

/* Individual CSS rules for each color: */

.wish.green{
	background:url(img/green_bg.jpg);
	border:1px solid #3ccd1b;
}

.wish.red{
	background:url(img/red_bg.jpg);
	border:1px solid #e9403c;
}

.wish.add{
	background:url(img/blue_bg.jpg);
	border:1px solid #2976BB;
}</pre>
<p>The site has a basic structure with a header on the top (the div with an id of <strong>header</strong>), which takes the full width of the page and the main container, which holds all the content of the site and is centered on the page.</p>
<p>The header&#8217;s background is an image with a fixed width (aren&#8217;t they all) which fades gracefully to the background color of this same div. This way no matter how wide the visitor&#8217;s screen is, they are guaranteed to see a uniform heading section of the site.</p>
<p>You can also see that we&#8217;ve defined a general <strong>.wish</strong> class, which we build on by defining custom backgrounds for the different color classes.</p>
<h4>demo.css &#8211; Part 2</h4>
<pre class="brush:css">.profile-pic{
	float:left;
	margin-right:10px;
}

div div.profile-pic img{
	/* Re-designing the facebook avatars */
	border:2px solid white;

	/* CSS3 Box Shadow */
	-moz-box-shadow:0 0 1px #666666;
	-webkit-box-shadow:0 0 1px #666666;
	box-shadow:0 0 1px #666666;
}

a.fbconnect_login_button {
	/* The facebook button */
	display:block;
	margin-top:7px;
}

.stage{
	/* The .stage divs, which hold the text of the wish */
	background:url(img/bg_gwg.png) repeat-y  center top #eeeeee;
	border:3px solid white;
	padding:8px;

	/* CSS3 Box Shadow */
	-moz-box-shadow:0 0 3px #666666;
	-webkit-box-shadow:0 0 3px #666666;
	box-shadow:0 0 3px #666666;

	/* Fixing the peekaboo bug in IE by triggering the
	   hasLayout mechanism with the zoom property */
	zoom:1;
}

.topIcon{
	/* The icons at the top */
	width:128px;
	height:120px;
	position:absolute;
	top:-40px;
	right:-40px;
}

.wish.red .topIcon{
	background:url(img/mistletoe.png) no-repeat;
}

.wish.green .topIcon{
	background:url(img/jingle.png) no-repeat;
}

.name{
	color:#888888;
	margin-left:114px;
}

.body{
	/* Text of the wish */
	color:#555555;
	font-size:20px;
	padding-top:6px;
	margin-left:114px;
}

#addWish .name{
	/* Initially hiding the name field of the comment form */
	display:none;
}

#cb{
	/* The checkbox */
	position:relative;
	top:2px
}

#sbutton{
	/* The submit button */
	background:#155C9C;
	border:1px solid #093F6F;
	color:white;
	cursor:pointer;
	font-family:Arial,Verdana,Sans-serif;
	font-size:12px;
	padding:3px;
}</pre>
<p>There is one aspect of Facebook Connect that might surprise you at first &#8211; it automatically includes a stylesheet in your page that might overwrite the styles you&#8217;ve set up for things such as the user avatars and names.</p>
<p>For this purpose you can use the <strong>!important</strong> modifier next to your rules (font-size:12px !important;) or do what I&#8217;ve done &#8211; provide more elements in the description of the classes, which adds more weight to your rules and overrides those of facebook (<strong> </strong><strong>div div.profile-pic img</strong> instead of just <strong>.profile-pic img</strong>, line 6).</p>
<h4>demo.css &#8211; Part 3</h4>
<pre class="brush:css">#wish{
	/* The textarea */
	border:1px solid #AAAAAA;
	color:#606060;
	font-family:Arial,Verdana,Sans-serif;
	font-size:20px;
	padding:3px;
	width:400px;
	display:block;
}

label{
	font-size:10px;
}

a img{
	border:none;
}

/* Styling the pagination at the bottom of the page: */

.pagination{
	font-size:10px;
	margin:-15px auto 10px;
	text-transform:capitalize;
	width:620px;
}

.pagination a,.pagination a:visited,.pagination span{
	background-color:#F2F2F2;
	border:1px solid white;
	color:#545454;
	display:inline;
	float:left;
	margin:3px 5px 0 0;
	padding:4px 6px;
	text-decoration:none;
}

.pagination a:hover{
	background-color:#eaf7ff;
	color:#444444;
}

.pagination span{
	background-color:#BBBBBB;
	border:1px solid #CCCCCC;
	color:#666666;
}

/* The clearfix hack */

.clear{
	clear:both;
}

p{
	/* The tut info on the bottom of the page */
	padding:10px;
	text-align:center;
}</pre>
<p>In the third part of the code, we style the form elements and the pagination, displayed on the bottom of the page.</p>
<div id="attachment_571" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-571" title="The Wish Container" src="http://tutorialzine.com/wp-content/uploads/2009/12/i5.jpg" alt="The Wish Container" width="600" height="230" /><p class="wp-caption-text">The Wish Container</p></div>
<h3>Step 4 &#8211; PHP</h3>
<p>As I mentioned in step one, PHP generates all of the christmas wishes XHTML markup dynamically, after fist selecting them from the database. Lets see how this works.</p>
<h4>index.php</h4>
<pre class="brush:php">// Error reporting

error_reporting(E_ALL^E_NOTICE);

// Including additional files
require "connect.php";
require "functions.php";

// If there is no key, create a new one
if(!$_SESSION['key'])
{
	// Making a new unique key:
	$key = md5(time().$_SERVER['REMOTE_ADDR']);
	// Saving it to the session. Will be used in post.php;
	$_SESSION['key'] = $key;
}

// The total number of wishes. Used in the pagination:
list($total)=mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM christmas_wishes"));

$results_per_page=5;
$page=1;
if((int)$_GET['page']&gt;1) $page = (int)$_GET['page'];

$start=($page-1)*$results_per_page;

// Selecting the wishes from the database. Using te LIMIT cause:
$res = mysql_query("SELECT * FROM christmas_wishes ORDER BY id DESC LIMIT ".$start.",".$results_per_page);

$wishes='';
$c = '';
$i=0;

while($row=mysql_fetch_assoc($res))
{
	$c='red';
	if($i++%2 == 0) $c='green';
	$wishes .= '
		&lt;div class="wish '.$c.'"&gt;
		&lt;div class="stage"&gt;
		&lt;div class="topIcon"&gt;&lt;/div&gt;
		&lt;div class="profile-pic"&gt;&lt;fb:profile-pic uid="'.$row["facebook_id"].'" facebook-logo="true" size="s"&gt;&lt;/fb:profile-pic&gt;&lt;/div&gt;
		&lt;div class="name"&gt;&lt;fb:name uid="'.$row["facebook_id"].'" useyou="false"&gt;&lt;/fb:name&gt;&amp;nbsp;wished&lt;/div&gt;
		&lt;div class="body"&gt;'.htmlspecialchars($row["wish"]).'&lt;/div&gt;
		&lt;div class="clear"&gt;&lt;/div&gt;
		&lt;/div&gt;
		&lt;/div&gt;
	';
}</pre>
<p>First we generate a special key which is used to check whether the form has been submitted properly and put it add it as a session variable.</p>
<p>As you can see from the XHTML section of the tut, this key is also inserted as a hidden field inside of the form and is later submitted back to the server along the textual body and other data.</p>
<p>It is then checked whether it corresponds with the key in the session. If it does not, this means that the form was submitted without visiting <strong>index.php</strong> first (a sure sign of a spam bot).</p>
<p>This technique will also prevent double posting, because the session key is unset after a successful insert in the database.</p>
<p>The form is submitted via AJAX, which sends the data to <strong>post.php</strong>, which is given below:</p>
<h4>ajax/post.php</h4>
<pre class="brush:php">// Error reporting

error_reporting(E_ALL^E_NOTICE);
require "../connect.php";
require "../functions.php";

if(!$_POST['uid'] || !$_POST['wish'] || !$_POST['key']) die('0');

// Checking to see whether the key is in place:
if(!$_SESSION['key'] || $_SESSION['key']!=$_POST['key']) die('0');

$uid = (int)$_POST['uid'];
if(!$uid) die(err('Please login to facebook first!'));

// Escaping the string and stripping the HTML:
$wish = mysql_real_escape_string(strip_tags($_POST['wish']),$link);

if(mblen($wish)&lt;4) die(err('Your wish is too short!'));

/* Inserting a new record in the christmas_wishes DB: */
mysql_query('	INSERT INTO christmas_wishes (facebook_id,wish)
			VALUES ('.$uid.',"'.$wish.'")');

if(mysql_affected_rows($link)==1)
{
	echo '{status:1}';
	unset($_SESSION['key']);
}
else echo err('Something went wrong. Try again!'.mysql_error($link));</pre>
<p>It is pretty straightforward. The variables are validated, the data is escaped and inserted in the database. A JSON object is returned which has a status and text property (the latter is initialized only if there is an error and contains the error message).</p>
<p>There is another PHP file &#8211; <strong>functions.php</strong>, which will not be discussed here, but is worth taking a look. Now lets take a look at jQuery.</p>
<div id="attachment_572" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-572" title="The Submit Form" src="http://tutorialzine.com/wp-content/uploads/2009/12/i6.jpg" alt="The Submit Form" width="600" height="230" /><p class="wp-caption-text">The Submit Form</p></div>
<h3>Step 5 &#8211; jQuery</h3>
<p>Before being able to use the jQuery library, we have to include it into the page.</p>
<h4>index.php</h4>
<pre class="brush:html">&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.scrollTo-min.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</pre>
<p>I included jQuery directly from Google&#8217;s AJAX CDN &#8211; this enables us to have the benefits of their huge infrastructure and the likely possibility that the library is already cached in the visitors browser.</p>
<p>The next scripts are the facebook connect library, the jQuery scrollTo plugin (which you&#8217;ll see in action in a moment) and our own script file.</p>
<p>All of our JavaScript code is located inside <strong>script.js</strong>. I&#8217;ve split the code in two parts:</p>
<h4>script.js &#8211; Part 1</h4>
<pre class="brush:js">$(function(){

	/* This code is run on page load */

	/* Showing the wishes, hidden by the CSS. */
	$('#main').show();

	/* Listening for the submit event on the form */
	$('#wForm').submit(function(e){
		addWish();
		e.preventDefault();
	});

	function init(){
		/* Run on page load if the user is logged into facebook */
		$('#share .toggleContent').toggle();

		$('#addWish .name').html('Hi, &lt;fb:name uid="loggedinuser" useyou="false" firstnameonly="true"&gt;&lt;/fb:name&gt;, add your wish below:').show();
		$('#addWish .profile-pic').children().attr('uid',FB.Connect.get_loggedInUser());
		FB.XFBML.Host.parseDomTree();
	}

	/* Initializing facebook connect */
	FB.init("bcf260ae02d453654be01ec11e2d0aa0","xd_receiver.htm",{"ifUserConnected" : init});</pre>
<p>In this first part of the script, we set the code to be run after the DOM has been completely loaded (and all page elements are accessible).</p>
<p>We later define a <strong>init()</strong> function that displays the submit form and initialize facebook connect (notice the <strong>ifUserConnected</strong> property &#8211; the <strong>init()</strong> functon is run on page load if the user has logged in previously).</p>
<p>The first parameter of the <strong>FB.init</strong> function is the <strong>API key</strong> of the app we created earlier.</p>
<h4>script.js &#8211; Part 2</h4>
<pre class="brush:js">var commencing = false;

function addWish()
{
	/* If there is another AJAX process already active stop the function: */
	if(commencing) return false;

	/* Stripping HTML tags */
	var wish = $('#wish').val().replace(/&lt;[^&gt;]*&gt;/g,'');

	if(wish.length&lt;3) { alert('Your wish is too short or empty!'); return false; }
	if(!FB.Connect.get_loggedInUser()) { alert('You are not logged in!'); return false; }

	commencing=true;

	/* Sending the data to post.php */
	$.post('ajax/post.php',{uid:FB.Connect.get_loggedInUser(),wish:wish,key:$('#key').val()},function(msg){

		/* This function is run if the data was successfully sent */
		commencing=false;

		if(msg.status)
		{
			/* If there were no errors */
			if($('#cb').attr('checked'))
			{
				var attachment = {
					name:'Christmas Wishes',
					href:'http://christmas-wishes.tutorialzine.com/',
					description:'What do you wish for this christmas?'
				};

				var action_link='';

				setTimeout(function(){
					FB.Connect.streamPublish(wish, attachment, action_link);
				},1000);
			}

			/* Insert a new wish by duplicating and changing the first wish currently on the page */
			var el = $('.wish').eq(0).clone()

			if(el.hasClass('green'))
			{
				el.addClass('red').removeClass('green');
			}
			else el.addClass('green').removeClass('red');

			el.find('.body').html(wish);
			el.hide().html(el.html().replace(/uid=\"\d+\"/g,'uid="'+FB.Connect.get_loggedInUser()+'"'));
			$('#wishesContainer').prepend(el);

			/* Parsing the fb tags: */
			FB.XFBML.Host.parseDomElement(el.get(0));

			el.fadeIn('slow');

			/* Scrolling the page to show the newly added wish into view: */
			$.scrollTo(el,800);
			$('#share').html('Thank you!');
		}
		else
		{
			alert('There was an error trying to add your comment!\nPlease try again in a few moments.');
			return false;
		}
	},'json')
}
});</pre>
<p>The second part of the code holds the <strong>addWish()</strong> function which posts the data via AJAX.</p>
<p>You can see that I&#8217;ve used a variable called commencing to figure out whether there is an active transfer in the progress, which allows only one AJAX call at once (and thus preventing unnecessary double-posting caused by multiple clicks on the submit button).</p>
<p>Also, you can see on line 39 that I&#8217;ve used the <a href="http://demos.flesler.com/jquery/scrollTo/" target="_blank">jQuery ScrollTo plugin</a> to scroll the window smoothly so that the newly inserted element is in view.</p>
<h3>Step 6 &#8211; MySQL</h3>
<p>In order to make this work on your own server, you&#8217;ll need to create the <strong>christmas_wishes </strong>MySQL table by running <strong>tables.sql</strong>, which is included in the download archive.</p>
<p>You&#8217;ll have to change your MySQL credentials in <strong>connect.php</strong> and replace the API key used in <strong>script.js</strong> with your own.</p>
<p>With this our Christmas Wishes webapp is complete!</p>
<h3>Conclusion</h3>
<p>Today we created a fully blown webapp which implemented facebook connect. You are free to build upon this source code and use it in your own personal or commercial projects.<br />
Have a merry Christmas and a happy New Year!</p>
<p><strong>What do you think? How would you improve it this code?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=sYsG9XkcJb0:kdU0G7k035U:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=sYsG9XkcJb0:kdU0G7k035U:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=sYsG9XkcJb0:kdU0G7k035U:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=sYsG9XkcJb0:kdU0G7k035U:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=sYsG9XkcJb0:kdU0G7k035U:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/sYsG9XkcJb0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2009/12/christmas-wishes-facebook-connect/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2009/12/christmas-wishes-facebook-connect/</feedburner:origLink></item>
		<item>
		<title>Animated Sharing Bar With jQuery &amp; CSS</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/HenglpMKNp0/</link>
		<comments>http://tutorialzine.com/2009/12/animated-share-buttons-jquery-css/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 22:13:48 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=552</guid>
		<description><![CDATA[This week we are using pure JavaScript with the help of the jQuery framework, to make an animated sharing bar, which will enable your website visitors to share your posts on a number of social networks.]]></description>
			<content:encoded><![CDATA[<p>Social networks can make a big difference on the popularity of a blog. Sites that communicate better and understand social media are usually the most popular.</p>
<p>A move towards this goal would be to find a way to encourage your visitors to share your content on the networks they are most active on.</p>
<p>Today we are doing just that, by using pure JavaScript with the jQuery framework, to make an animated sharing bar, which will enable your website visitors to share posts on a number of social networks.</p>
<p>Go ahead and download the demo files and continue with step one of this tut.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>As usual, we start off with the XHTML markup. The sharing area is constructed by using three main div containers:</p>
<ul>
<li><strong>#share</strong> &#8211; this element acts as a container which holds the other two. It also has some CSS3 properties applied, such as rounded corners;</li>
<li><strong>#stage</strong> &#8211; this is where the animation takes place. On page load jQuery detects the horizontal and vertical centers and uses them to position the rotating divs. It is also floated to the left;</li>
<li><strong>#share-label</strong> &#8211; this has the &#8220;share the love&#8221; image as a background. It is also floated to the left and thus positioned right next to the <strong>#stage</strong> div.</li>
</ul>
<p>But lets have a detailed look:</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;div id="share"&gt;
&lt;div id="stage"&gt;

&lt;div class="btn digg"&gt;&lt;div class="bcontent"&gt;&lt;a class="DiggThisButton"&gt;&lt;/a&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class="btn tweetmeme"&gt;&lt;div class="bcontent"&gt;&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class="btn dzone"&gt;&lt;div class="bcontent"&gt;&lt;script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class="btn reddit"&gt;&lt;div class="bcontent"&gt;&lt;script type="text/javascript" src="http://www.reddit.com/button.js?t=2"&gt;&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class="btn facebook"&gt;&lt;div class="bcontent"&gt;&lt;a name="fb_share" type="box_count" href="http://www.facebook.com/sharer.php"&gt;Share&lt;/a&gt;&lt;script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"&gt;&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;

&lt;/div&gt;

&lt;div id="share-label"&gt;
&lt;!-- This is where the share some love image appears --&gt;
&lt;/div&gt;

&lt;/div&gt;</pre>
<p>The code above is all you need to add to your page (in addition to the js and css files), so you can show the animated bar below your articles.</p>
<p>Each button has a generic <strong>btn</strong> class and an individual one (digg, tweetmeme, etc). This makes it possible to gather the common rules shared by the buttons in a single class and make it easier to customize at a later time.</p>
<p>Also notice that inside of each of the button divs, there is an individual JavaScript file that actually generates the button on the page. Those are provided by the social networks and usually display the button exactly where the script tag is inserted.</p>
<p>Now lets see how these elements are styled.</p>
<div id="attachment_557" class="wp-caption alignnone" style="width: 630px"><img class="size-full wp-image-557" title="An Animated Sharing Bar" src="http://tutorialzine.com/wp-content/uploads/2009/12/i21.png" alt="An Animated Sharing Bar" width="620" height="260" /><p class="wp-caption-text">An Animated Sharing Bar</p></div>
<h3>Step 2 &#8211; CSS</h3>
<p>I&#8217;ve divided the CSS code into two parts.</p>
<h4>demo.css &#8211; Part 1</h4>
<pre class="brush:css">#share{
	/* The share box container */
	width:500px;
	background:#ececec;
	height:220px;
	margin:60px auto;
	overflow:hidden;

	-moz-border-radius:12px;
	-webkit-border-radius:12px;
	border-radius:12px;
}

#share-label{
	/* The image on the right */
	background:url(img/share.png) no-repeat 50% 50%;
	float:left;
	height:220px;
	width:200px;
}

#stage{
	/* This is where the animation takes place */
	position:relative;

	border-right:1px solid #DDDDDD;
	width:290px;
	height:220px;
	background:white;
	float:left;

	border-bottom-left-radius:12px;
	border-top-left-radius:12px;

	-moz-border-radius-bottomleft:12px;
	-moz-border-radius-topleft:12px;

	-webkit-border-bottom-left-radius:12px;
	-webkit-border-top-left-radius:12px;
}

.btn{
	/* This class is assigned to every share button */
	background-color:white;
	height:90px;
	left:0;
	top:0;
	width:60px;
	position:relative;
	margin:20px 0 0 10px;
	float:left;
}

.bcontent{
	/* Positioned inside the .btn container */
	position:absolute;
	top:auto;
	bottom:20px;
	left:0;
}</pre>
<p>Maybe at this stage you are wondering how the actual buttons are animated in a circular movement on the page.</p>
<p>A major part of the technique takes place inside the <strong>#stage</strong> CSS class above. The stage itself is relatively positioned, which enables the buttons (which have absolute positioning assigned by jQuery below, and are direct descendants) to be positioned accordingly.</p>
<p>This means that setting top and left to zero on the buttons, positions them in the top-left corner of the stage.</p>
<p>You&#8217;ll see the rest of the technique in step 3.</p>
<h4>demo.css &#8211; Part 2</h4>
<pre class="brush:css">/* Individual rules for every share button */

.digg{	background:url(img/digg_reflection.png) no-repeat -4px bottom;}
.reddit{	background:url(img/reddit_reflection.png) no-repeat -4px bottom;}
.facebook{	background:url(img/facebook_reflection.png) no-repeat bottom center;}
.tweetmeme{	background:url(img/twit_reflection.png) no-repeat -5px bottom;}
.dzone{	background:url(img/dzone_reflection.png) no-repeat -7px bottom;}

.thanksto{
	position:absolute;
	bottom:2px;
	right:4px;
	font-size:10px;
}

.thanksto a,.thanksto a:visited{
	color:#BBB;
}

/* Customizing the facebook share button */

span.fb_share_no_count {
	display:block;
}

span.fb_share_count_top.fb_share_no_count {
	line-height:54px;
}

span.fb_share_count_nub_top.fb_share_no_count{
	display:none;
}

span.fb_share_no_count span.fb_share_count_inner {
	background:#3B5998 url(http://static.fbshare.me/f_only.png) no-repeat scroll 20px 5px;
	display:block;
}</pre>
<p>In the second part of the style sheet, we assign individual rules to each button which define a unique background image with the reflection. After this we customize the facebook button, so that its styling matches the rest of the buttons.</p>
<p>With this we can continue with step 3.</p>
<div id="attachment_558" class="wp-caption alignnone" style="width: 630px"><img class="size-full wp-image-558" title="The Sharing Bar Deconstructed" src="http://tutorialzine.com/wp-content/uploads/2009/12/i1.jpg" alt="The Sharing Bar Deconstructed" width="620" height="460" /><p class="wp-caption-text">The Sharing Bar Deconstructed</p></div>
<h3>Step 3 &#8211; jQuery</h3>
<p>With the speed with which modern browsers render JavaScript, it gets easier to make eye-catching animations, previously reserved only for technologies as Adobe Flash.</p>
<p>But before getting down to work, we need to include two  script files in the head of our page:</p>
<pre class="brush:html">&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</pre>
<p>The first one includes the jQuery library from Google&#8217;s CDN, and the second line includes our own <strong>script.js</strong> file, covered in detail below:</p>
<h4>script.js</h4>
<pre class="brush:js">$(document).ready(function(){

	/* This code is run on page load */

	var deg=0;
	var dif=-3;

	/* Assigning the buttons to a variable for speed: */
	var arr = $('.btn');

	/* Caching the length of the array in a vаriable: */
	var len = arr.length;

	/* Finding the centers of the animation container: */
	var centerX = $('#stage').width()/2 - 40;
	var centerY = $('#stage').height()/2 - 60;

	/* Applying relative positioning to the buttons: */
	arr.css('position','absolute');

	/* The function inside the interval is run 25 times per second */
	setInterval(function(){

		/* This forms an area with no activity on mouse move in the middle of the stage */
		if(Math.abs(dif)&lt;0.5) return false;

		/* Increment the angle: */
		deg+=dif;

		/* Loop through all the buttons: */
		$.each(arr,function(i){

			/* Calculate the sine and cosine with the new angle */
			var eSin = Math.sin(((360/len)*i+deg)*Math.PI/180);
			var eCos = Math.cos(((360/len)*i+deg)*Math.PI/180);

			/* Setting the css properties */
			$(this).css({
				top:centerY+25*eSin,
				left:centerX+90*eCos,
				opacity:0.8+eSin*0.2,
				zIndex:Math.round(80+eSin*20)
			});
		})
	},40);

	/* Detecting the movements of the mouse and speeding up or reversing the rotation accordingly: */
	var over=false;
	$("#stage").mousemove(function(e){

		if(!this.leftOffset)
		{
			/* This if section is only run the first time the function is executed. */
			this.leftOffset = $(this).offset().left;
			this.width = $(this).width();
		}

		/* If the mouse is over a button, set dif to 0, which halts the animation */
		if(over) dif=0;
		else
			dif = -5+(10*((e.pageX-this.leftOffset)/this.width));

		/* In the other case calculate the speed according to the X position of the mouse */
	});

	/* Detecting whether the mouse is positioned above a share button: */
	$(".bcontent").hover(
		function(){over=true;dif=0;},
		function(){over=false;}
	);
});</pre>
<p>The main idea here, is that we use <strong>setInterval</strong> to set up a function to be run every 40 milliseconds. This means that it is run 25 times a second, or if you compare it with a movie screen, this is 25 frames per second. In other words more than enough to display a smooth animation (providing that the rendering speed of the browser is fast enough, and that there are no other scripts interfering).</p>
<p>As this is not a computation-heavy animation, it runs pretty smoothly on all browser versions (even as old as <strong>IE6</strong>). However, the smoothest animation I&#8217;ve observed is in <strong>Safari</strong> and <strong>Chrome</strong> (it would run nicely in <strong>Firefox</strong> too, providing that you don&#8217;t have a lot of add-ons or opened tabs).</p>
<p>You can see throughout the code above, that I tend to assign the results of jQuery selectors or other functions to variables. This is done to improve the speed of the script, otherwise all those functions and methods would be calculated on every frame (for a total of 25 times per second) which would ultimately bring down the performance and smoothness of the animation.</p>
<p>With this our animated sharing bar is complete!</p>
<h3>Conclusion</h3>
<p>In this tutorial, we created an animated social sharing bar which encourages visitors to share articles on a number of social networks with an eye-catching animation.</p>
<p>To include new services, in addition to the five currently active, you just have to include a new <strong>btn</strong> element in the stage div with an appropriate script that would generate the button.</p>
<p><strong>What do you think? How would you improve this script?</strong></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=HenglpMKNp0:LpGshKm9ImE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=HenglpMKNp0:LpGshKm9ImE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=HenglpMKNp0:LpGshKm9ImE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=HenglpMKNp0:LpGshKm9ImE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=HenglpMKNp0:LpGshKm9ImE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/HenglpMKNp0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2009/12/animated-share-buttons-jquery-css/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2009/12/animated-share-buttons-jquery-css/</feedburner:origLink></item>
		<item>
		<title>Making a Fresh Content Accordion</title>
		<link>http://feedproxy.google.com/~r/Tutorialzine/~3/5GjSCiHs5Fg/</link>
		<comments>http://tutorialzine.com/2009/12/colorful-content-accordion-css-jquery/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 21:42:46 +0000</pubDate>
		<dc:creator>Martin Angelov</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tutorialzine.com/?p=541</guid>
		<description><![CDATA[Today we are making a simple, yet eye-catching accordion with the help of CSS, jQuery and the easing plug-in for some fancy effects.]]></description>
			<content:encoded><![CDATA[<p>When developing a website, it is a great challenge to be able to organize the content in such a way, that it is both intriguing and eye-catching. Not to mention how important it is to prevent information overloading by exposing your visitors to too much data in a single view.</p>
<p>This is why there are certain techniques that help designers group content and show it only if the user is interested in what you have to offer and interacts with the page.</p>
<p>Today we are making a simple, yet eye-catching accordion with the help of CSS, jQuery and the <a href="http://gsgd.co.uk/sandbox/jquery/easing/" target="_blank">easing plug-in</a> for some fancy effects.</p>
<p>You can go ahead and download the demo files before we continue with step one.</p>
<h3>Step 1 &#8211; XHTML</h3>
<p>As you can see from the demo, the accordion is divided into four sections, each defined by a <strong>LI</strong> element with a class name of <strong>menu</strong>. They are positioned inside the main unordered list (<strong>ul.container</strong>) and share a common XHTML structure:</p>
<h4>demo.html</h4>
<pre class="brush:html">&lt;li class="menu"&gt; &lt;!-- This LI is positioned inside the main UL --&gt;

&lt;ul&gt; &lt;!-- This UL holds the section title and content  --&gt;

&lt;!-- The click-able section title with a unique background: --&gt;
&lt;li class="button"&gt;&lt;a href="#" class="green"&gt;Kiwis &lt;span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;

&lt;!-- The dropdown list, hidden by default and shown on title click: --&gt;
&lt;li class="dropdown"&gt;

&lt;ul&gt; &lt;!-- This list holds the options that are slid down by jQuery --&gt;

&lt;!-- Each option is in its own LI --&gt;
&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Kiwifruit"&gt;Read on Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.flickr.com/search/?w=all&amp;amp;q=kiwi&amp;amp;m=text"&gt;Flickr Stream&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt; &lt;!-- Closing the elements --&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;/li&gt;</pre>
<p>Each menu LI contains another UL, which forms a title area (<strong>li.button</strong>), and a content area (<strong>li.dropdown</strong>).</p>
<p>We then have an anchor element positioned inside  <strong>li.button</strong> (button being the assigned class name) . This hyperlink is later bound to a jQuery event handler, which slides open the section&#8217;s drop down list once the link is clicked.</p>
<p>It is also worth noting that the <strong>dropdown</strong> list elements are hidden by default with the <strong>display:none</strong> CSS property as you will see in a moment.</p>
<div id="attachment_544" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-544" title="The accordion explained" src="http://tutorialzine.com/wp-content/uploads/2009/12/i1.png" alt="The accordion explained" width="600" height="460" /><p class="wp-caption-text">The accordion explained</p></div>
<h3>Step 2 &#8211; CSS</h3>
<p>If styled properly, even the simplest idea can make a big difference in the overall feeling your site gives to the visitors.</p>
<p>It is important to take extra care so that the CSS code you produce is valid and that it works well across different browsers. I&#8217;ve included all the styles used in the demo below:</p>
<h4>demo.css &#8211; part 1</h4>
<pre class="brush:css">body{
	/* Setting default text color, background and a font stack */
	color:#cccccc;
	font-size:13px;
	background: #302b23;
	font-family:Arial, Helvetica, sans-serif;
}

ul{
	margin:0;
	padding:0;
}

ul.container{
	/* The main UL */
	width:240px;
	margin:0 auto;
	padding:50px;
}

li{
	list-style:none;
	text-align:left;
}

li.menu{
	/* The main list elements */
	padding:5px 0;
	width:100%;
}

li.button a{
	/* The section titles */
	display:block;
	font-family:BPreplay,Arial,Helvetica,sans-serif;
	font-size:21px;
	height:34px;
	overflow:hidden;
	padding:10px 20px 0;
	position:relative;
	width:200px;
}</pre>
<p>Here we style the main UL &#8211; <strong>ul.container</strong>, which holds the rest of the elements. Lastly, we define the looks of the hyperlinks that act as section titles (notice that the actual background images are not yet assigned).</p>
<h4>demo.css &#8211; part 2</h4>
<pre class="brush:css">li.button a:hover{
	/* Removing the inherited underline from the titles */
	text-decoration:none;
}

li.button a span{
	/* This span acts as the right part of the section's background */
	height:44px;
	position:absolute;
	right:0;
	top:0;
	width:4px;
	display:block;
}

/* Setting up different styles for each section color */

li.button a.blue{background:url(img/blue.png) repeat-x top left; color:#074384;}
li.button a.blue span{ background:url(img/blue.png) repeat-x top right;}

li.button a.green{background:url(img/green.png) repeat-x top left; color:#436800;}
li.button a.green span{ background:url(img/green.png) repeat-x top right;}

li.button a.orange{background:url(img/orange.png) repeat-x top left; color:#882e02;}
li.button a.orange span{ background:url(img/orange.png) repeat-x top right;}

li.button a.red{background:url(img/red.png) repeat-x top left; color:#641603;}
li.button a.red span{ background:url(img/red.png) repeat-x top right;}

/* The hover effects */

li.button a:hover{ background-position:bottom left;}
li.button a:hover span{ background-position:bottom right;}

.dropdown{
	/* The expandable lists */
	display:none;
	padding-top:5px;
	width:100%;
}

.dropdown li{
	/* Each element in the expandable list */
	background-color:#373128;
	border:1px solid #40392C;
	color:#CCCCCC;
	margin:5px 0;
	padding:4px 18px;
}</pre>
<p>The most interesting part here is how we specify the individual background property for every section color. We also provide a background for the span element, which holds the right part of the background image, as explained in the illustration below.</p>
<div id="attachment_545" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-545" title="The buttons" src="http://tutorialzine.com/wp-content/uploads/2009/12/i3.png" alt="The buttons" width="600" height="230" /><p class="wp-caption-text">The buttons</p></div>
<h3>Step 3 &#8211; jQuery</h3>
<p>After we&#8217;ve laid the layout of the accordion, it is time to make it tick. First we need to include a few scripts in the page (this code is put inside the head section of the document):</p>
<pre class="brush:html">&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.easing.1.3.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;</pre>
<p>We first include the jQuery library from Google&#8217;s CDN, which will help us write compact and compatible code. Later we include the easing plugin and lastly our own script file, which I&#8217;ve covered in detail below.</p>
<p>As you may have noticed from the demo, the slide down effect is not your regular linear movement, but more bounce-like and lively. This is achieved with the help of the <strong>easing</strong> plug-in for jQuery. It provides a number of interesting effects which can put those lovely details in your web design.</p>
<p>Now lets continue with our own scripts file.</p>
<h4>script.js</h4>
<pre class="brush:js">$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	/* Changing thedefault easing effect - will affect the slideUp/slideDown methods: */
	$.easing.def = "easeOutBounce";

	/* Binding a click event handler to the links: */
	$('li.button a').click(function(e){

		/* Finding the drop down list that corresponds to the current section: */
		var dropDown = $(this).parent().next();

		/* Closing all other drop down sections, except the current one */
		$('.dropdown').not(dropDown).slideUp('slow');
		dropDown.slideToggle('slow');

		/* Preventing the default event (which would be to navigate the browser to the link's address) */
		e.preventDefault();
	})

});</pre>
<p>We first set the easing method that is going to be used by the <strong>slideUp</strong>/<strong>slideDown</strong> effects, and then bind a specially crafted function to the click event of the <strong>li.button</strong> hyperlink. When this link is clicked, we get the corresponding <strong>li.dropDown</strong> and show it, hiding all the others.</p>
<p>I am using the <strong>slideToggle</strong> jQuery method, which checks to see if the element is already visible on the page, and decides whether to show or hide it. This way, when you click on an open section of the accordion, it is contracted instead of just staying opened.</p>
<p>After this, we use <strong>e.preventDefault()</strong> to prevent the browser from navigating away from the page (after all we&#8217;ve just clicked a link and this is the normal behavior).</p>
<p>With this our fruity CSS &amp; jQuery accordion is complete!</p>
<div id="attachment_546" class="wp-caption alignnone" style="width: 610px"><img class="size-full wp-image-546" title="A CSS &amp; jQuery accordion" src="http://tutorialzine.com/wp-content/uploads/2009/12/i2.png" alt="A CSS &amp; jQuery accordion" width="600" height="230" /><p class="wp-caption-text">A CSS &amp; jQuery accordion</p></div>
<h3>Conclusion</h3>
<p>This time we created a fresh accordion script that is both easily modifiable end embeddable.</p>
<p>Can you think of any awesome uses of this script?</p>
<p>How would you like to change it?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=5GjSCiHs5Fg:hw2pH5Bd_7o:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=5GjSCiHs5Fg:hw2pH5Bd_7o:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=5GjSCiHs5Fg:hw2pH5Bd_7o:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?i=5GjSCiHs5Fg:hw2pH5Bd_7o:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Tutorialzine?a=5GjSCiHs5Fg:hw2pH5Bd_7o:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Tutorialzine?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Tutorialzine/~4/5GjSCiHs5Fg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tutorialzine.com/2009/12/colorful-content-accordion-css-jquery/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		<feedburner:origLink>http://tutorialzine.com/2009/12/colorful-content-accordion-css-jquery/</feedburner:origLink></item>
	</channel>
</rss>
