jQuery Web Page Design Tutorials

jQuery Tutorial Animate Div Layer Right

In previous jQuery tutorials, we learned how to animate a Div Layer in down and up direction. In this tutorial, we show how to animate a Div Layer to right direction. Actually the concept is the same. The only difference is also 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 to Right

The following diagram is the starting layout of the Div Layers. The picture Div Layer is simply position and hidden at the left position. When the "Click Here to Animate Picture Left/Right" button is clicked, the picture div layer will animate to the right side.

jQuery animate div right

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: 284px;
width: 300px;
background-image: url(arrow_down.png);
background-repeat: no-repeat;
top: 0px;
left: 317px;
}

And the styles of the frame Div Layer is:

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

 

Animate Div Layer Right and Left

The first thing to do is to get the outer width of the picture div layer. And then position the picture div layer at the left side just beyond the frame layer when the page loads. The jQuery codes are:

// Get the outer width of #picture Div
var widthShift = $("#picture").outerWidth();

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

There are two scenerios when animating the picture Div Layer.

Case 1: Animate the Div Layer to Right

It simply animate the picture div layer to right direction, creating an effect that the arrow image is shotting from the left side. The jQuery codes is:

$("#picture").animate({ "left": "+=" + widthShift + "px" });

jQuery animate div right direction

Case 2: Animating the Div Layer Left (Back)

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

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

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