
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 includes 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 includes a very cool animated background. Here is how the final result will look:

As you can see, in this example I have used the Twitter background clouds, which look really nice.
This tutorial has been inspired by the one called How To Build an Animated Header in jQuery that I definitely recommend you go check out.
Let’s do 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 finished, 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. Since we want to do a simple layout, we’ll only need to create a header and content area. 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 use some CSS and give 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 look 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 include the library. Since Google hosts a version that you can use, 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 has 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>There’s nothing really hard with that code. First, we declare a set of variables to control the animation (image width, scroll speed, etc). Then, we created a function to automatically move the background. The most tricky part is to calculate when we need to reset the position. If you have a better idea about doing it, let me know in a comment! ![]()
Demo & Download
I hope you enjoyed this tutorial and helps you to create very nice websites. If you want, you can see a Demo of this code, or you can Download the source files.




54 Comments + Trackbacks
11.12.2009
Awesome tutorial! Just tweeted it.
11.12.2009
Very nice little jQuery trick.
11.12.2009
Great tutorial Jean, this is just another step towards flash dying.
The image doesnt get added until there is enough space for it so there is a gap every 20 seconds or so. See this screenshot:
http://screencast.com/t/MTZiMjNkO
Firefox 3.5
11.12.2009
Looks very cool! I would just make it move slower, to make it less distracting.
11.12.2009
Brilliant! this looks wicked! thanks for the tut.
11.12.2009
This is sexy, for real.
11.12.2009
Wes – I fixed it with changing var imageWidth = 2247; to var imageWidth = 1700; but I assume you could change the headerWidth also.
Does this have increased CPU usage because I tested it and it revved up my processor pretty good. Maybe it’s just me?
11.12.2009
Nice tip, thanks for sharing on Twitter
11.12.2009
Great, but is there very usefull to load jquery just for this animation?
It’s really easy to use default function and speed up the execution time (while not using jquery somewhere else ofc) of the script by changing line 73 into :
document.getElementById(‘header’).style.backgroundPosition = current+’px 0′;
If you’re using Prototype or Mootools :
$(‘header’).setStyle(“background-position”,current+”px 0″);
11.12.2009
Great tutorial!
Currentry I’m working on the same approach for one of the project – and I’ve met at least two small problems, that was not covered in your tutorial (as for myself I have not found any optimal solution for them yet)…
1. When the cycle ends end animation restarts there is visible jump in image… When we dealing with vertiacal moving that easily could be fixed with the same pixel-set in the beginnig and in the end of animation that equal to the header height, but in the horizontal case it does not work because we don’t now the width of user’s window in this moment and this width is different for different users
2. In your case restart position is calculated assuming that the header width is 800px, but if user has screen resolution more than 800*600 – this put you in a trouble because the user will see the end of the picture moving on the screen before the animation restarts… so there we also meet the question about user’s screen width
probably further discussions will reveal some solutions

anyway thank you a lot for sharing this code
11.12.2009
Thanks for this list
11.12.2009
“var headerWidth = $(‘html’).width();” will work with any screen resolutions, but headerWidth need to be updated inside script when user resize window.
11.13.2009
F*%$ awesome man!!!
I wanna implement it someday :/
i’ll twit it right away
11.13.2009
does NOT work. reaches the images end.
11.13.2009
Can you tell me if we can use the twitter background on our projects?
11.13.2009
By the way, you can determinate the screen resolution with: screen.width
You just need to put it like this: var headerWidth = screen.width;
There you go!
11.15.2009
This needs to use clearInterval, doesn’t it? CPU runs to 100% the longer this runs. Anybody know how to do it?
11.15.2009
That’s some nice jQuery!
11.17.2009
i want to know if we can use the twitter background on our projects?
11.17.2009
@jane : I don’t think you can since the example image I took comes from Twitter website.
11.17.2009
too good! loved it! love jquery!
11.18.2009
Could you please show me I would incorporate this into a wordpress site please?
11.19.2009
hi don’t forget to check another inspiration at my web site click product and choose another design and after that choose my type…i have another animation for background just with jquery..i really like it..and i really like your web site name “catswhocode” ,thankyou..
11.19.2009
Thanks for the tip. I think I’m going to use it for my site.
I’ll modify it and embedded it in the middle of the page.
Thanks.
11.20.2009
Wow. Great one. I’ll definitely use this for my personal website.
11.21.2009
don’t think you can since the example image I took comes from Twitter website.
11.22.2009
This is pretty slick, thank you for sharing it.
To make the animation seamless could you use two exact images that rotate positions?
For example as image1 is halfway through it’s animation, image2 would append to the end of image1.
When image2 moves completely into the background space, image1 would append to the end of image2.
11.22.2009
Well done! That’s a really useful tutorial.
11.23.2009
I dont know much yet in jquery but this tutorial rocks!
11.27.2009
Woww. Thank you so much Jean.
11.28.2009
awesome tuts..thanks for share
11.29.2009
Awesome trick – worked for me too..
any way to do it with a flash file ? maybe a negative z-index or something ?
11.30.2009
Thanks alot for the neat trick. I am planning to do something similar with my personal site with some extra parameters. But this one saved me alot of coding time.
12.3.2009
Wow, first time I saw an effect like this. I thought things like this are exclusive for flash websites only. I’m definitely exploring this trick. I just might incorporate it in some sites of mine. Thanks a lot.
12.6.2009
Hmm, nice start but this needs some work.
At first I would make it go slower, which will make it less distracting. Then second, as mentioned before, it jumps when it reaches the end, so you should get the screen width and work with that value.
Third, some people say it gets their CPU really pumped up and that’s not what we want right.
Nice start!
12.17.2009
Here’s my solution for the time being:
If you open the ‘bg-clouds.png” in Photoshop and tile the clouds to something like the width of 6741pixels and re-save it, it’s only a 100kb image still. Set the “var scrollSpeed = 70;” and you’re all set.
Now, the kick doesn’t come in for a looooong time
12.29.2009
The very cool, thanks !
12.30.2009
what amazing beautiful tutorial…, don’t need more flash for this animation
Thank you so much =)
1.9.2010
Great tute, and nice code. Would love to see this with multiple depth, like parallax scrolling, so, back mid front.
Great blog, and thanks for sharing, Digging now =)
1.21.2010
I just want to say that Cats are really good in writing codes “art of code” after what i read in this website; Gratz
1.24.2010
Here’s a way to get around the end of image problem:
1. Use an image that can wrap horizontally.
2. Add to the background style “repeat-x”.
3. Update this line of code: “var restartPosition = -(imageWidth);”
It’s a cool effect, but still trying to figure out if I should use it or not.