jQuery Web Page Design Tutorials

jQuery Tutorial Animate Div Layer Up

In previous jQuery tutorial, we learned how to animate a Div Layer down. In this tutorial, we show how to animate a Div Layer up. Actually the concept is the same. The only difference is the starting position of the picture Div Layer.

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 bottom position. When the "Click Here to Animate Picture Down/Up" button is clicked, the picture div layer will animate up.

jQuery animate div up

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: 302px;
}

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 Up and Down

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 bottom side just below 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 Up

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

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

jQuery animate div up

Case 2: Animating the Div Layer Down (Back)

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

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

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