<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Bennett - User Interface Developer &#187; Flip</title>
	<atom:link href="http://pmbennett.net/tag/flip/feed/" rel="self" type="application/rss+xml" />
	<link>http://pmbennett.net</link>
	<description></description>
	<lastBuildDate>Thu, 27 Oct 2011 15:10:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery Picture Board</title>
		<link>http://pmbennett.net/2010/04/13/jquery-picture-board/</link>
		<comments>http://pmbennett.net/2010/04/13/jquery-picture-board/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 21:25:22 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Flip]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pmbennett.net/?p=299</guid>
		<description><![CDATA[<p>I'm very much looking forward to the World Cup this year, which reminded me of the fun I had during the last one four years ago. I remember organising a Question of Sport style quiz for my fellow colleagues, which gave me a great chance to try out some JavaScript that I had learned. One round involved the creation of a picture board, which is divided up into squares, and users can click on the numbers to reveal a portion of an image underneath, similar to the Picture board on Question of Sport, but more like a game of Catchphrase. It got me thinking: what would I do now I am armoured with all this jQuery goodness?</p>
]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://webmuch.com/image-flip-using-jquery/">Aayush Shastri&#8217;s image flip</a>, I decided to re-create the picture board using some cool animation. My approach was to use a simple list of links, which would each take the form of a square on top of the background image. The HTML and CSS are very straight forard, so I will not list that here. Instead, I will walk through my solution using jQuery.</p>
<p>Before I begin, it is worth highlighting that I am using an ordered list of links, and the hidden image is a background image on the <code>&lt;ol&gt;</code> element. I will take the following approach:</p>
<ol>
<li>Store the url of the hidden image</li>
<li>Add an element to each list item, which will be used to display the hidden portion of the image</li>
<li>then add the click events to the numbered square and the reveal element</li>
</ol>
<p>So the first few lines set a few variables, allowing us to cache some jQuery objects for later use.</p>
<pre><code>$gridWrap = $('#reveal_grid');
$revealImg = $gridWrap.css('background-image');
$gridWrap.css('background', 'transparent')
$sqrs = $('#reveal_grid li');</code></pre>
<p>We then loop through the links in the list and add the <code><span></code> elements, add some CSS rules to setup the hide/reveal.</p>
<pre><code>$('#reveal_grid li a').each(function() {
	$num = $(this);
	$bgPos = $num.css('backgroundPosition');
	$num.parent('li').append(
		$('<span class="reveal"></span>').css({
			backgroundImage: $revealImg,
			backgroundPosition: $bgPos,
			height:'0',
			marginTop: ($num.height()/2) + 'px',
			opacity:'0.2'
		})
	);
});</code></pre>
<p>Next we need to add the click events, which just adds the flip animation that was based upon the <a href="http://webmuch.com/image-flip-using-jquery/">original by Aayush Shastri</a>:</p>
<pre><code>$sqrs.delegate('a', 'click', function() {
	$sqr = $(this);
	$height = $sqr.height();
	$width = $sqr.width();
	$sqr.stop()
	.animate({
		width: $width + 'px',
		height: '0',
		marginTop: ($height/2) + 'px',
		opacity: '0.2'
	}, 500, function() {
	$sqr.next('.reveal')
		.stop()
		.animate({
			width: $width + 'px',
			height: $height + 'px',
			marginTop: '0',
			opacity: '1'
		}, 500);
	});
	return false;
});
$sqrs.delegate('span', 'click', function() {
	$sqr = $(this);
	$height = $sqr.height();
	$width = $sqr.width();
	$sqr.stop()
	.animate({
		width: $width + 'px',
		height: '0',
		marginTop: ($height/2) + 'px',
		opacity: '0.2'
	}, 500, function() {
		$sqr.prev('a')
		.stop()
		.animate({
			width: $width + 'px',
			height: $height + 'px',
			marginTop: '0',
			opacity: '1'
		}, 500);
	});
	return false;
});</code></pre>
<p>I am aware that this code could be streamlined somewhat, but it made sense to structure it like this for demonstration purposes. In terms of performance, it seems fine, so I will leave as is for the time being.</p>
]]></content:encoded>
			<wfw:commentRss>http://pmbennett.net/2010/04/13/jquery-picture-board/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

