ery slide images header feature with jQuery

This might not be the best way to structure the JavaScript and I’m sure you hard core coders will pick up on this however one of the purposes of this tutorial is to show newcomers to jQuery exactly how flexible and easy it is to create something that on the outside looks quick complex. The code used in this tutorial is relatively simple to understand.

 

we start with out document ready function, you should be familiar with this by now. If you’re not you can read about it here. We now have an event handler, in our case its a ‘hover’ event meaning that when our cursor hovers over the div ‘.holder’ it will execute the function. When our mouse exits the div ‘.holder’ it will run the handler out function, which in our case will revert the div back to how it was originally.

You can see that we’re using ‘(this)’ to reference our element. this is used to specify the element that the mouse is hovered over at that moment as we’re specifying the class holder, if we didn’t use ‘(this)’ regardless of which element we hovered over all of them would animate, so ‘(this)’ specifys that specific element to animate.

Lets look at what happens when we hover over one of the divs ‘.holder’.

  1. For the element our mouse is over we remove the class ‘notactive’
  2. We then animate the width of the divs with a class of ‘notactive’ this is to compensate for the expanding width of the selected element as you’ll see in the next step
  3. Now we increase the width of the current ‘.holder’ div to 380px
  4. Next we slide the image up to make way for the text.
  5. We then change the background color of the span header to black to highlight it.
  6. And finally over 300 milliseconds fade in the ‘.text’ div.

Because the animations aren’t chained they happen all at the same time.

When you’re mouse leaves the div the function below it is run to revert the contents back to it’s original position.

$(document).ready(function(){

$('.holder').hover(
function () {
$(this).removeClass('.notactive');    
$('.notactive').stop().animate({'width':'290px'},400);
$(this).find('img').stop().animate({'top':'-165px'},400);
$(this).stop().animate({'width':'380px'},400);
$(this).find('span').css({'background-color':'#000'});
$(this).find('.text').fadeIn(300);
},
function () {
$('.notactive').stop().animate({'width':'320px'},400);
$(this).addClass('.notactive');    
$(this).find('.text').hide();
$(this).find('img').stop().animate({'top':'0px'},500);
$(this).stop().animate({'width':'320px'},400);
$(this).find('span').css({'background-color':'#333', 'color':'#dedede'});

});

});

The HTML

Image One

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,

Image Two

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,

Image Three

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,

The CSS

.holder {
overflow:hidden;
width:320px;
height:333px;
float:left;
position:relative;
background-color:#000;
margin-right:1px;
}
.holder .image {
position:absolute;
left:-100px;
}
.holder span {
background-color:#333;
font-size:27px;
font-family:Arial, Helvetica, sans-serif;
color:#dedede;
font-weight:700;
padding:4px;
position:absolute;
top:120px;
left:4px;
}
.holder .text {
padding:20px;
display:none;
font-family:Arial, Helvetica, sans-serif;
line-height:26px;
position:absolute;
top:180px;
font-size:16px;
color:#fff;
width:340px;
}
.clear {
clear:both;
}
.main {
width:1000px;
height:333px;
margin:0 auto;
overflow:hidden;
}
.credit {
font-size:12px;
margin-top:25px;
}

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

Leave a comment