jQuery Web Page Design Tutorials

jQuery Tutorial Animate Div Layer Left

This is the final jQuery tutorial of animating Div Layer. In this jQuery tutorial, we show how to animate a Div Layer to left 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 Left

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

jQuery animate div left

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

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 Left and Right

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 Left

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

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

jQuery animate div left direction

Case 2: Animating the Div Layer Right (Back)

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

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

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