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:
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>
Related
- 45 Fresh Useful JavaScript and jQuery Techniques and Tools
- 10 Useful jQuery Techniques to Improve Your Code
- 10+ JQuery tutorials for working with HTML forms
- 10 jQuery snippets for efficient developers
- 10 incredible JQuery navigation menus
- 35+ Create Amazing Image Effects and Sliders with These Awesome jQuery Plugins
- 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets
- jQuery Tools: The missing UI library for the Web
- 20+ Irresistible jQuery Tips to Power Up Your JavaScript Skills













