Submit your widget

Animated Header in jQuery

Created 13 years ago   Views 16111   downloads 2198    Author n/a
Animated Header in jQuery
View DemoDownload
Share |

We'll show you how to animate your header’s background image using jQuery to give your website that little extra something.We are going to build a header that animates it’s background.  We will also encase the header in shadow for the little extra dramatic effect.

The first thing we’re going to need is the HTML in place.  It’s pretty straight forward.  We have a wrapper div to center the website, a header div for the animation, another div within the header for the shadow overlay, and finally divs for the navigation and body copy.  It’s a lot of divs, but what do you expect, it’s the framework ;)

Here is the HTML:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Animated Header</title>

    <div id="wrapper">
        <div id="header">
            <!-- Div is for Shadow Overlay-->
                <!-- Title -->
                <h1>Animated Header Background</h1>
        <div id="nav">
            <!-- Navigation Goes HERE -->
        <div id="body">
            <!-- Body Content Goes HERE -->

<!-- Import jQuery-->
<script src="" type="text/javascript" charset="utf-8"></script> 




We aren’t going to do anything too difficult here. Here is a list of what was done:

  • Center the website
  • Give the header a height and a background image
  • Style and position the text within the header
  • Create a shadow overlay
  • Give basic style to the header and navigation bar.

Here is the CSS:


    background-color: #000;

/* Center the website */
    margin:0 auto;

/* Give the header a height and a background image */
    background: #000 url(background.jpg) repeat-y scroll left top;

/* Create a Shadow Overlay */
#header div{
    background: transparent url(overlay.png) no-repeat scroll left top;

/* Vertically position header text and style it*/
#header h1{
    font-family: Arial, "MS Trebuchet", sans-serif;

/* Give basic styles to the body and the navigation */
    background-color: #111;



Just a note, the png transparency won’t work in IE6.  If you choose to optimize for IE6 you will need to add this piece of code to use the overlay.


<!--[if lte IE 6]>
    <style type="text/css" media="screen">
        #header div{
            background-image: none;
            filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='overlay.png', sizingMethod='scale');


The jQuery

There are a couple ways of animating the background with jQuery.  You can use the jQuery plugin that allows you to animate the CSS background-position attribute.

It’s a little uglier to look at but the performance is a lot better.

Here is the jQuery:

var scrollSpeed = 70;        // Speed in milliseconds
var step = 1;               // How many pixels to move per step
var current = 0;            // The current pixel row
var imageHeight = 4300;     // Background image height
var headerHeight = 300;     // How tall the header is.

//The pixel row where to start a new loop
var restartPosition = -(imageHeight - headerHeight);

function scrollBg(){
    //Go to next pixel row.
    current -= step;
    //If at the end of the image, then go to the top.
    if (current == restartPosition){
        current = 0;
    //Set the CSS of the header.
    $('#header').css("background-position","0 "+current+"px");

//Calls the scrolling function repeatedly
var init = setInterval("scrollBg()", scrollSpeed);