jQuery Web Page Design Tutorials

jQuery Tutorial Animate Div Layer Down

jQuery can animate div layer easily thus allow create interesting web page effect. In this jQuery animate div layer tutorial series, we will animate the div layer in four direction, i.e. down, up, right and left direction. This first tutorial shows how to animate the div layer down.

jQuery Demo

A demo of this jQuery tutorial is available. Click here to View Demo. (Pop up window)

Goal of Animating Div Layer Down

The following diagram is the starting layout of the Div Layers. The picture Div Layer is simply position and hidden at the top position. When the "Click Here to Animate Picture Down/Up" button is clicked, the picture div layer will animate down.

jQuery animate div down

HTML Layout of Div Layers

This is the HTML code of the picture Div Layer:

<div id="frame">
<div id="picture"></div>
</div>

This is the styles of the picture Div Layer:

#picture {
position: absolute;
height: 298px;
width: 282px;
background-image: url(arrow_down.png);
background-repeat: no-repeat;
top: -298px;
}

And the styles of the frame Div Layer is:

#frame {
position: relative;
height: 298px;
width: 282px;
background-color: #99F;
border: 2px solid #999;
overflow: hidden;
margin-bottom: 10px;
}

 

Animate Div Layer Down and Up

The first thing to do is to get the outer height of the picture div layer. And then position the picture div layer at the top just above the frame layer when the page loads. The jQuery codes are:

// Get the outer height of #picture Div
var heightShift = $("#picture").outerHeight();

// Set starting position of #picture Div
$("#picture").css( { top: -heightShift + 'px'} );

There are two scenerios when animating the picture Div Layer.

Case 1: Animate the Div Layer Down

It simply animate the picture div layer down, creating an effect that the arrow image is dropping down from above. The jQuery codes is:

$("#picture").animate({ "top": "+=" + heightShift + "px" });

jQuery animate div down

Case 2: Animating the Div Layer Up (Back)

It simply animate the picture Div Layer up and back to the original position thus creating an effect that the arrow image is hiding back to the top. The jQuery codes is:

$("#picture").animate({ "top": "-=" + heightShift + "px" });

This is the end of jQuery Animate Div Layer Down Tutorial.