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’.
- For the element our mouse is over we remove the class ‘notactive’
- 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
- Now we increase the width of the current ‘.holder’ div to 380px
- Next we slide the image up to make way for the text.
- We then change the background color of the span header to black to highlight it.
- 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
<div class="main"> <div class="holder notactive"> <img class="image" src="images/4237135170_61a9938781.jpg" height=""/> <span>Image One</span> <div class="text">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,</div> </div> <div class="holder notactive"> <img class="image" src="images/4166210326_06d91ce72a.jpg"/> <span>Image Two</span> <div class="text">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,</div> </div> <div class="holder notactive"> <img class="image" src="images/4060303038_a630262185.jpg"/> <span>Image Three</span> <div class="text">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,</div> </div> <div class="clear"></div> </div>
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; }
The article source:http://papermashup.com/building-a-fancy-jquery-header-feature/
You might also like
Tags
accordion accordion menu animation navigation animation navigation menu carousel checkbox inputs css3 css3 menu css3 navigation date picker dialog drag drop drop down menu drop down navigation menu elastic navigation form form validation gallery glide navigation horizontal navigation menu hover effect image gallery image hover image lightbox image scroller image slideshow multi-level navigation menus rating select dependent select list slide image slider menu stylish form table tabs text effect text scroller tooltips tree menu vertical navigation menu