Alright, let's get into the tutorial
This time, we are going to make a custom content slider. I implemented this in my recent project and I'm pretty happy about it. My colleague spiced it up a little bit with some nice buttons. The way it works is quite simple, we will have a UL list with 3 buttons and each of the button has a link with a reference to the right panel. How it scroll to the correct panel? It uses jQuery scroll-To plugin. Basically you just have to pass the ID of the panel to the scroll-To plugin, and it will sroll the content to the correct position.
For more information about the plugin, please visit the scrollTo plugin website
1. HTML
HTML is a little bit long this time. Basically, it has two sections:- UL List: This is the list of buttons, you can add as many as you want, or style the layout the way you want.
- Slider: Slider is where we put all the content for the panels, in this case, we have 3 panels with unique ID panel-1, panel-2 and panel-3. We hide the panel with overflow hidden set in the Mask Div.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < div id = "hero-slider" > < ul > < li >< a href = "#" rel = "#panel-1" class = "active" >Item 1 < li >< a href = "#" rel = "#panel-2" >Item 2 < li >< a href = "#" rel = "#panel-3" >Item 3
< div class = "mask" > < div class = "slider-body" > < div class = "panel" id = "panel-1" > < h2 >Title 1 < p >Paragraph
< div class = "panel" id = "panel-2" > < h2 >Title 2 < p >Paragraph
< div class = "panel" id = "panel-3" > < h2 >Title 3 < p >Paragraph
< div class = "clear" >
|
2. CSS
The only tricky part of the CSS would be the mask. You have to make sure it has overflow set to hidden to hide panels. Also, the dimension of the panel and mask must be the same.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 | #hero-slider { text-align : left ; background-color : #efefef ; border : 1px solid #ccc ; width : 450px ; -moz-border-radius: 10px ; -webkit-border-radius: 10px ; margin : 0 auto ; font-family : arial ; } #hero-slider .mask { float : left ; width : 300px ; height : 280px ; margin : 15px 0 0 10px ; overflow : hidden ; } #hero-slider .panel { width : 300px ; height : 280px ; text-align : left ; } #hero-slider ul { margin : 0 ; padding : 15px 15px 0 15px ; list-style : none ; float : left ; border-right : 1px solid #dedede ; height : 285px ; } #hero-slider ul li { margin : 10px 0 ; } #hero-slider ul a { outline : none ; text-decoration : underline ; display : block ; width : 75px ; height : 74px ; text-indent : -999em ; } #hero-slider a { background : url (button.png) no-repeat 0 0 ; } #hero-slider ul a.active { background-position : -75px ; } .panel h 2 { padding : 15px 0 0 0 ; color : #0058a9 ; } .panel p { color : #666 ; } .clear { clear : both } |
3. Javascript
Javascript is short and simple, I have added comments to explain every line of the javascript1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //append click event to the UL list anchor tag $( '#hero-slider ul a' ).click( function () { //reset all the items $( '#hero-slider ul a' ).removeClass( 'active' ); //set current item as active $( this ).addClass( 'active' ); //scroll it to the right position $( '.mask' ).scrollTo($( this ).attr( 'rel' ), 300); //disable click event return false ; }); |
No comments:
Post a Comment