<?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>youlove.us &#187; animation</title>
	<atom:link href="http://youlove.us/blog/tag/animation/feed" rel="self" type="application/rss+xml" />
	<link>http://youlove.us/blog</link>
	<description>gorgeous design and beautiful code</description>
	<lastBuildDate>Mon, 16 Nov 2009 02:16:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The youlove.us scrolling background effect explained</title>
		<link>http://youlove.us/blog/the-youloveus-scrolling-background-effect-explained</link>
		<comments>http://youlove.us/blog/the-youloveus-scrolling-background-effect-explained#comments</comments>
		<pubDate>Tue, 16 Jun 2009 16:40:12 +0000</pubDate>
		<dc:creator>Chris Wheeler</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://youlove.us/blog/?p=291</guid>
		<description><![CDATA[<p><img class="alignright size-medium wp-image-295" title="3586379781_8df45c258d_o" src="http://youlove.us/blog/wp-content/uploads/3586379781_8df45c258d_o-300x236.jpg" alt="3586379781_8df45c258d_o" width="300" height="236" />We&#8217;ve had a lot of questions regarding the background effect on our new site &#8211; so I thought I&#8217;d explain how it works, to save you the trouble of picking through the source code. Feel free to base your own&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-medium wp-image-295" title="3586379781_8df45c258d_o" src="http://youlove.us/blog/wp-content/uploads/3586379781_8df45c258d_o-300x236.jpg" alt="3586379781_8df45c258d_o" width="300" height="236" />We&#8217;ve had a lot of questions regarding the background effect on our new site &#8211; so I thought I&#8217;d explain how it works, to save you the trouble of picking through the source code. Feel free to base your own designs on this technique, but don&#8217;t just straight up copy our code and images &#8211; we&#8217;ve spent a good amount of time on this.</p>
<p>If you&#8217;re feeling nice give us a mention on your web site/blog/twitter/facebook/bebo/myspace or to your mates down the pub.</p>
<h2>How it works</h2>
<p>The technique is actually a lot simpler than it looks, it&#8217;s based around scrolling a very tall gradient image behind some transparent PNG images. The header image (with our logo, strapline and the laptop) has a transparent background and solid text, while the main page is actually an image with the heading text as transparent cut outs.</p>
<h3>The images</h3>
<p>We&#8217;re using four images for the whole effect, but you could make it worth with just the background and header if you wanted. We have:</p>
<ul>
<li><a href="/template/theme/youloveus/img/bg.jpg" target="_blank">The background</a></li>
<li><a href="/template/theme/youloveus/img/bg-header.png" target="_blank">The header</a></li>
<li><a href="/template/theme/youloveus/img/bg-home.png" target="_blank">The content</a></li>
<li><a href="/template/theme/youloveus/img/bg-footer.png" target="_blank">The footer</a></li>
</ul>
<h3>The CSS</h3>
<p>To keep the search engines, screen readers and pre-historic browsers happy we&#8217;ve also overlaid all content and headings on the top of the images, and hidden them via CSS.</p>
<h3>The JavaScript</h3>
<p>The JavaScript is where the real magic happens. We&#8217;ve made use of the jQuery library <span style="text-decoration: line-through;">and Alexander Farkas backgroundposition.js script</span> and CSS to help make the background move.</p>
<p>The code below makes the whole thing work. The comments should explain what each section does.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// ***</span>
	<span style="color: #666666; font-style: italic;">// Scrolling background</span>
	<span style="color: #666666; font-style: italic;">// ***</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// height of background image in pixels</span>
	var backgroundheight <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4000</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// get the current minute/hour of the day</span>
	var now <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	var hour <span style="color: #339933;">=</span> now.<span style="color: #006633;">getHours</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	var minute <span style="color: #339933;">=</span> now.<span style="color: #006633;">getMinutes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// work out how far through the day we are as a percentage - e.g. 6pm = 75%</span>
	var hourpercent <span style="color: #339933;">=</span> hour <span style="color: #339933;">/</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
	var minutepercent <span style="color: #339933;">=</span> minute <span style="color: #339933;">/</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
	var percentofday <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">round</span><span style="color: #009900;">&#40;</span>hourpercent <span style="color: #339933;">+</span> minutepercent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// calculate which pixel row to start graphic from based</span>
	<span style="color: #666666; font-style: italic;">// on how far through the day we are</span>
	var offset <span style="color: #339933;">=</span> backgroundheight <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">*</span> percentofday<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// graphic starts at approx 6am, so adjust offset by 1/4</span>
	var offset <span style="color: #339933;">=</span> offset <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>backgroundheight <span style="color: #339933;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	function scrollbackground<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// decrease the offset by 1, or if its less than 1 increase it by</span>
		<span style="color: #666666; font-style: italic;">// the background height minus 1</span>
   		offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>offset <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> offset <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>backgroundheight <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> offset <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// apply the background position</span>
   		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">css</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;background-position&quot;</span>, <span style="color: #0000ff;">&quot;50% &quot;</span> <span style="color: #339933;">+</span> offset <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   		<span style="color: #666666; font-style: italic;">// call self to continue animation</span>
   		setTimeout<span style="color: #009900;">&#40;</span>function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			scrollbackground<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>, <span style="color: #cc66cc;">100</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Start the animation</span>
	scrollbackground<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The really clever bit&#8230;</h3>
<p>You may have noticed from reading the code above, but if you come and visit us later in the day you&#8217;ll see the background position start from a different place. If you visit us at night, you&#8217;ll see a night sky, and if you visit us in the morning you&#8217;ll see a sunrise!</p>
<p>If you have any comments, questions or improvements to the script &#8211; drop us an <a href="/contact/">email</a> or leave a comment here.</p>
<h2>Updated 2009-07-30</h2>
<p>After a few comments regarding the processor usage under FireFox 3.5 i&#8217;ve updated the script to no longer use backgroundposition.js or jQuery&#8217;s animate() method &#8211; it now simply changes the body&#8217;s CSS background-position value by 1 pixel on each loop iteration. CPU usage on an older machine running FireFox 3.5 went from around 65% to 30%. All other browsers still appear not to struggle with it. Feedback welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://youlove.us/blog/the-youloveus-scrolling-background-effect-explained/feed</wfw:commentRss>
		<slash:comments>167</slash:comments>
		</item>
	</channel>
</rss>
