jQuery cool hover Slide Effect

Setting up our files

Ok let’s start by opening our text editor and creating a new html document, save this to your desktop as “index.html”. Now create a new css file and save this as “style.css” in a new folder named “css”.


Include our files

Getting back to our index file, we now need to include our newly created css file and the jQuery library into the head of our document.

In this tutorial we are going to link to the google api version of jQuery as the chances are your user has come across this file at some point so it was already be cached on their system.

The mark up

Now we’ve included our necessary files for this effect to work let’s get dow to adding some html to our document. Include this html inside the tag of the index file.

Style the body

Open up the css file we created earlier named “style” and let’s add some style to the page just to make it a little less bland. You can mess around with these colours as much as you like, it’s all down to preference really.

body {
font-family:Arial, Helvetica, sans-serif;
h1 {
a {

Container style

Now the page has a little colour we can add the style to our divs, we’re going to start with the #container div this way we can talk through the reason for having certain styles.

#container {
margin:0 auto;
border:10px solid #414141;
#container:hover {

The container is the div that displays our image, which the two other divs (#hover and #overlay) will slide over.

We set this to relative because we need to set the child divs as absolute so that we position them our of view – this is an important style.

This style basically hides the two child dives which gives the effect of them sliding across the image.

Places the div at the bottom of the pile which gives us the transparent look from the div above.

Overlay style

This is the div which ill give us the semi-transparent black effect that fades in over our image, let’s add the styles again to our css file and go through some of the important styles.

#overlay {

We can change this colour to what ever we would like the effect to be, for example #fff for white.

As the div is an empty one we need to display it as block.

We set this as absolute so that we can position it anywhere we like in our container that’s positioned as relative.

top and left
This will place are div at the very top and very left of our container div, we will change the left position using jQuery later.

We’ve set this as 1 so that it sits on top of our image.

Hover style

Now let’s add the styles for our final div #hover, which contains the content in our sliding effect.

#hover {
#hover p {

  1. Width and height, you may have noticed we’ve reduced our width and height here – reasons being we want to use padding so that our content isn’t squashed in.
  2. position, we’ve positioned this div as absolute again so that we can control where we place in in our container div.

Dummy content

You can place whatever you like in your div but for now I’m just going to place in some dummy content.

Cool hovering slide effect!

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Read more

This goes inside the #hover div.

Let’s get down to the jQuery

Now for the fun stuff, we can start to animate our content using a little bit of jQuery. To do so we need to insert an opening and closing script tag in the head section of our document.

$(document).ready(function () {


What is document ready?

Basically, what we’re doing here is telling our code not to run unless the document has fully loaded and ready to do so. Alternatively we can put our jQuery in our the just above our closing body tag in the footer which will automatically let the page load before running our script, which means we can remove our document.ready function all together.

Set the variables

The reason for setting variables in our code here is really just to save repeating code, which in this case is very useful.

$(document).ready(function () {

var colour = $("#overlay");

var content = $("#hover");


Hiding our divs

If you’ve previewed the code we have written so far you will have noticed that the divs just all sit on top of each other hiding our image which is not good. To get around this we will need to hide these elements using jQuery, let’s put those variables to use.

$(document).ready(function () { 
var colour = $("#overlay"); 
var content = $("#hover"); 

You will now notice that all we see is our image which is great.

Hover in some colour

Now we’re going to put our #overlay div to some use and fade in it and then back out again. Add the following code directly above the tag.

$("#container").hover(function() {  
colour.stop().fadeTo(500, .7) 

,function() { 
colour.stop().fadeTo(500, 0) 

.css() explanation

You can see here we’ve added some css to the #hover div, where we positioned the div as absolute earlier in our stylesheet we can now play around with this in our jQuery. Notice that the position left is minus (-) the full width of our div which will make place the this element just out of the visible portion of our container div, if you’re going to make you divs a different size to the one’s we have used in this tutorial you will need to adjust this part of the jQuery to match your new widths.

Stop our slider going mad

If your move the mouse frantically over the image you will notice that the content just keeps on sliding back and forth uncontrollably, to get over this we need to call in .stop() which will basically stop our code from over running. You may have noticed we already have this in place in the line controlling the colour fade, we’re basically going to do the same thing here.

$("#container").hover(function() {  
content.stop().show().css({ "left" : "-450px" }).animate({left : 0}, 300); 
colour.stop().fadeTo(500, .7) 

,function() { 
content.stop().animate({left : 450}, 300); 
colour.stop().fadeTo(500, 0) 

Download: http://www.htmldrive.net/items/download/685

Be Sociable, Share!

Leave a comment