jQuery Moving Tab and Sliding Content Tutorial
Introduction
We learn how to create a moving tab slide content in this tutorial.
Basically, the tab section simulate Lava Lamp Effect, and the content is
using animate method to slide it. I have two tutorials that focus on
that two effects. Click on the following links if you're interested:
Right, like usual, I always like to separate the code into 3 sections
- HTML, CSS and Javascript. For best practise, it's good to separate
css, javascript from html code.
1. HTML
In the container #moving_tab, we have two sections in the HTML code -
tabs class and content class. Tabs class contains 2 tabs, and content
class contains 2 ul lists.
The following image illustrate the structure of this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < div id = "moving_tab" >
< div class = "tabs" >
< div class = "lava" ></ div >
< span class = "item" >Tab 01</ span >
< span class = "item" >Tab 02</ span >
</ div >
< div class = "content" >
< div class = "panel" >
< ul >
< li >< a href = '#' >Panel 01 Item 01</ a ></ li >
< li >< a href = '#' >Panel 01 Item 02</ a ></ li >
< li >< a href = '#' >Panel 01 Item 03</ a ></ li >
< li >< a href = '#' >Panel 01 Item 04</ a ></ li >
< li >< a href = '#' >Panel 01 Item 05</ a ></ li >
</ ul >
< ul >
< li >< a href = '#' >Panel 02 Item 01</ a ></ li >
< li >< a href = '#' >Panel 02 Item 02</ a ></ li >
< li >< a href = '#' >Panel 02 Item 03</ a ></ li >
< li >< a href = '#' >Panel 02 Item 04</ a ></ li >
< li >< a href = '#' >Panel 02 Item 05</ a ></ li >
</ ul >
</ div >
</ div >
</ div >
|
2. CSS
I have put comment in every important css style. Bear in mind that,
if you change the width of #moving_tab, you might have to change the
width value in .item, .lava, .panel, ul.
The rest of it like position:absolute and position:relative are
important as well to set the layout, for the animation and layering. For
more tips and tricks for CSS, check out the following posts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | body {
font-family : arial ;
font-size : 12px ;
}
a {
color : #333 ;
text-decoration : none ;
display : block ;
}
a:hover {
color : #888 ;
text-decoration : none ;
}
#moving_tab {
overflow : hidden ;
width : 300px ;
position : relative
border : 1px solid #ccc ;
margin : 0 auto ;
}
#moving_tab .tabs {
position : relative ;
height : 30px ;
padding-top : 5px ;
cursor : default ;
}
#moving_tab .tabs .item {
position : relative ;
z-index : 10 ;
float : left ;
display : block ;
width : 150px ;
text-align : center ;
font-size : 14px ;
font-weight : 700 ;
}
#moving_tab .tabs .lava {
position : absolute ;
top : 0 ; left : 0 ;
z-index : 0 ;
width : 150px ;
height : 30px ;
background : #abe3eb ;
}
#moving_tab .content {
position : relative ;
overflow : hidden ;
background : #abe3eb ;
border-top : 1px solid #d9fafa ;
}
#moving_tab .panel {
position : relative ;
width : 600px ;
}
#moving_tab .panel ul {
float : left ;
width : 300px ;
padding : 0 ;
margin : 0 ;
list-style : none ;
}
#moving_tab .panel ul li {
padding : 5px 0 5px 10px ;
border-bottom : 1px dotted #fff ;
}
|
3. Javascript
I learnt new trick in jQuery scripting - position() method. I have
been using offset() method to calculate the position of elements, it's
really hassle because the position:relative of elements can influence
the value of offset.
This is the code I used to use:
- $('#moving_tab').offset().left - $(this).offset().left + $(this).width()
and it can be replaced by:
- $(this).position()['left']
Don't know why I couldn't find this method. But hey! it's something
new and I'm excited. Anyway, the jQuery script is quite simple, we only
have to animate lava class and panel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$(document).ready( function () {
$( '.lava' ).css({left:$( 'span.item:first' ).position()[ 'left' ]});
$( '.item' ).mouseover( function () {
$( '.lava' ).stop().animate({left:$( this ).position()[ 'left' ]}, {duration:200});
$( '.panel' ).stop().animate({left:$( this ).position()[ 'left' ] * (-2)}, {duration:200});
});
});
|
Conclusion
Like this tutorials? You can express your gratitude by visiting my
sponsors on the sidebar, buy me a drink in the bottom of the page or,
just bookmark it and help me to spread this tutorial to the rest of the
people who you think they are interested! :) Thanks!
|