Animated background image with jQuery

If you’re used to work with the jQuery library, there’s no doubt that you know how powerful it is. In this tutorial, we are going to create a basic web page layout which include a super cool animated background image, using jQuery.

Getting ready

In this tutorial, we are going to create a simple layout for a website, which include a very cool animated background. Here is how the final result will look:

animated background

This tutorial have been inspired by the one called How To Build an Animated Header in jQuery that I definitely recommend you to check out.

Let’s doing it

1. The first step of this tutorial is to download our background image. I have used this one from Twitter, but of course feel free to use any other image you’d like.

2. Once done, let’s create a a file called index.html. In order to get started with the basic html structure, paste the following code in your file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<title>Animated background Image</title>
	</head>

	<body>
	</body>
</html>

3. Now, let’s define our document structure. As we want to do a simple layout, we’ll only need to create a header and a content. Paste the following within the <body> and </body> tags:

<div id="container">
	<div id="header">
		<h1>Animated Background Image</h1>
	</div><!-- #header -->

	<div id="content">
		<!-- Your content will go here -->
	</div><!-- #content -->
</div><!-- #container -->

4. Well done! We already have our XHTML. Now, what we have to do is obviously to use some CSS and give some style to our document.
To do so, copy the code below and paste it within the <head> and <head> tags of the index.html file.

<style type="text/css" media="screen">
	body{
		background-color: #C0DEED;
		margin:0;
		padding:0;
	}

	#header{
		height:180px;
		background: #8EC1DA url(bg-clouds.png) repeat-y scroll left top;
		text-align:center;
		margin-top:-30px;
	}

	#header h1{
		padding-top:35px;
		font-family: "Myriad Pro", Helvetica, Arial, sans-serif;
		color:white;
		font-size:45px;
	}

	#content{
		background-color:#fff;
		height:500px;
		width:980px;
		margin:25px auto 0 auto;
		-moz-border-radius:10px;
		-webkit-border-radius:10px;
	}
</style>

5. At this point, you can save your work and take a look at the index.html page using your web browser. If everything is ok, your index.html page should looks like the screenshot above, with fixed clouds, of course.

6. Now, it’s time to give life to the layout by using the power of jQuery. As you probably guessed it, what we have to do right now is of course, to include the library. As Google host a version that you can use it, there’s definitely no need to download a copy. Just use the one from Google.

To do so, paste the following line of code in your index.html file, after the closing </body> tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>

7. Now that jQuery have been loaded, we can code a function to animate the background. Copy the code below and paste it on your index.html file, just after the line where you imported jQuery into the file.

<script type="text/javascript" charset="utf-8">
	var scrollSpeed = 70;
	var step = 1;
	var current = 0;
	var imageWidth = 2247;
	var headerWidth = 800;		

	var restartPosition = -(imageWidth - headerWidth);

	function scrollBg(){
		current -= step;
		if (current == restartPosition){
			current = 0;
		}

		$(’#header’).css("background-position",current+"px 0");
	}

	var init = setInterval("scrollBg()", scrollSpeed);
</script>

Animated background image with jQuery

Related

recycling,green living,reusing, save the earth,the greener me custom applications, application development, software development, agile development methodology, Microsoft platform, mobile applications,DotNetNuke,DNN job, jobs, recruitment, employment, jobs search engine, employment search, job opportunity, job opportunities, job opening, job openings, careerjet, search all jobsemployment search engine The Advantages Of Mezzanine Flooring in Your Home | Do It Yourself Home Projects Detailed Columbia Spring Homes Search with 30+ criteria. Instant access to all currently listed Real Estate, Foreclosed Homes, REO, Bank Owned, and Short Sales. linux web hosting,freebsd port,best seo services,sem tool,website templates,free, web design templates, flash templates web meeting,best web hosting,search marketing,seo forum,pay per click placement,top search engine marketing,create a website, Free website builder ,Free Flash templates, make a free website, free website, Flash website, web design, free web design PSD to HTML / XHTML linux hosting uk,macintosh,mac hosting,mac web hosting,linux hosting,freebsd unix,dns SpyderMate,Free SEO Analysis & SEO Tools Best Budget Hosting,Best Blog Hosting,Best Forum Hosting,Best Unix Hosting,Best Windows Hosting,Best PHP Hosting,Best Email Hosting,Best Ecommerce Hosting,Best Multi-Domain Hosting,Best VPS Hosting,Best Reseller Hosting,Best Dedicated Hosting,Top 25 Web Hosts,web hosting,top web hosts,internet hosting, dedicated servers,unix hosting