Here we’re going to create some sliding boxes effects built with jQuery and some CSS. Throughout this tutorial I’ll explain every line of used code with details, Hope you will enjoy this understand it easily.
If you want to follow along with the source files, you can download them by clicking here (*.zip archive). You can also take a look at the demo here.
Your Designer Toolbox
Unlimited Downloads: 500,000+ Web Templates, Icon Sets, Themes & Design Assets
Table of Contents
DOWNLOAD NOW
Step 1: Preparing our HTML, CSS & JS files
- Create a root folder and choose a name for it, mine is SlidingBoxes.
- In the root folder create a HTML file and name index.html, a css file and a js file.
- Than create a folder and name it images (this folder will contain the images needed).
Now we are ready for step 2.
Step 2: Preparing the html file structure
First we need to include the files we created in the previous step, into the header part of our HTML.
Let’s begin with the CSS file :
<link rel="stylesheet" type="text/css" href="style.css" />
Now we’ll attach the latest version of jQuery from Google’s AJAX Libraries repository:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
And now the last thing is our JavaScript file :
<script type="text/javascript" src="script.js"></script>
Now our header should look this:
<strong> </strong> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script type="text/javascript" src="script.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> </head>
Now we will start with our body elements:
We’re going to create 6 div’s because we will apply 6 different animations.
<div id="top-left" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div> <div id="top-center" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div> <div id="bottom-center" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div> <div id="left" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div> <div id="right" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div> <div id="top-right" class="wrap"> <img src="img/back.jpg" /> <img src="img/front.jpg" class="front" /> </div>
Explanation:
I used 6 div’s with different id’s (each id refers to where the animation starts ) and with the same class (wrap class) so we can add some styles to each one with css. Each div contains 2 images : the front and back images. Here is the images I used :
Front.jpg and back.jpg.
Step 3: Let’s add some CSS !
Now head over to your css file, we will begin with wrap class :
.wrap { float:left; position:relative; width:300px; height:150px; margin:20px; overflow:hidden; }
To have our images positioned one next the other we have to use the float left technique. I used the a margin of 20px to have some space between our images, next I set a width of 300px and a height of 150px . Also you have to set the position of this element to relative and a hidden overflow so anything that goes below or higher than the height that we put will be hidden.
CSS for Images :
img { top:0; left:0; position:absolute; }
We set our images to absolute and their top and left positions to 0.
Step 4: the jQuery
We’re going to start with the jQuery’s main function:
$(document).ready(function(){ // animation code will be written here });
Now we are going to add the animation for the first div ( #top-left ) :
$(document).ready(function(){ $('#top-left').hover(function(){ $(this).children('.front').stop().animate({'top' : '150px', 'left' : '300px'}, 500); },function(){ $(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); });
Explanation:
When the top-left div is hovered ( in this case $(this) refers to #top-left ) we are going to seek for the front class situated in this div and we’re going to animate it ( We have to set the top to 150px and the left to 300px so our front image can move with a nice effect), the 500 refers to duration. And when the div is not hovered we’re going to set back the top and left positions to 0.
To obtain the other effects you have to change the left and top positions.
Here is all the effects I used :
$(document).ready(function(){ $('#top-left').hover(function(){ $(this).children('.front').stop().animate({'top' : '150px', 'left' : '300px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); $('#top-center').hover(function(){ $(this).children('.front').stop().animate({'top' : '150px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); $('#bottom-center').hover(function(){ $(this).children('.front').stop().animate({'top' : '-150px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); $('#left').hover(function(){ $(this).children('.front').stop().animate({'left' : '300px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); $('#right').hover(function(){ $(this).children('.front').stop().animate({'left' : '-300px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); $('#top-right').hover(function(){ $(this).children('.front').stop().animate({'top' : '150px','left' : '-300px'}, 500); }, function(){$(this).children('.front').stop().animate({'top' : '0px', 'left' : '0px'}, 500);}); });
That’s it !
Thanks for following this tutorial. I hope you liked it and could follow it step by step. If you’ve done everything correctly, you should have ended up with something like this.
This post may contain affiliate links. See our disclosure about affiliate links here.