Today we are going to develop a nice content slider with jCarousel. It is possible that you have already seen similar sliders before, and were looking forward to repeat it. Our slider consists of next elements: initial intro block, control buttons to navigate through the slides, slides itself, and carousel with images (thumbnails) in the bottom. Plus, we can see all the slides one after the other (in alternative layout). This slider works very well in all the most popular browsers (Chrome, Firefox, IE7-9 etc).
[sociallocker]
[/sociallocker]
Ok, let’s download the source files and start coding !
Step 1. HTML
In the beginning we have to add all the necessary scripts and styles in header section:
2 | < link href = "css/main.css" rel = "stylesheet" type = "text/css" /> |
5 | < script src = "js/jquery.jcarousel.min.js" ></ script > |
6 | < script src = "js/main.js" ></ script > |
There are three javascript files: jQuery library, jCarousel library and our own custom script. As I mentioned before, we are going to use intro block, here is it:
3 | < img class = "intro-img" alt = "Intro" src = "images/1.jpg" /> |
6 | < a href = "javascript:void(0)" class = "sl-start" >Look all the photos</ a > |
Basically, this is simple DIV block with an image and text inside. For our slider I selected nice photo gallery by Little_Debbie (from flickr). After the intro section, there is main section:
002 | < div class = "sl-main" style = "display:none" > |
004 | < div class = "sl-controls" > |
006 | < div id = "count" ></ div > |
008 | < a class = "back-home" ></ a > |
011 | < div class = "sl-content" > |
012 | < div class = "sl-slide" > |
013 | < h2 class = "sl-title" >Just Hidden</ h2 > |
014 | < p >< img src = "images/1.jpg" /> 'Just Hidden' image description</ p > |
016 | < div class = "sl-slide" > |
017 | < h2 class = "sl-title" >Come sit awhile...</ h2 > |
018 | < p >< img src = "images/2.jpg" /> 'Come sit awhile... image description</ p > |
020 | < div class = "sl-slide" > |
021 | < h2 class = "sl-title" >escapando</ h2 > |
022 | < p >< img src = "images/3.jpg" /> 'escapando' image description</ p > |
024 | < div class = "sl-slide" > |
025 | < h2 class = "sl-title" >Beelitz Heilstatten</ h2 > |
026 | < p >< img src = "images/4.jpg" /> 'Beelitz Heilstatten' image description</ p > |
028 | < div class = "sl-slide" > |
029 | < h2 class = "sl-title" >I'm just a little guy...</ h2 > |
030 | < p >< img src = "images/5.jpg" /> 'I'm just a little guy...' image description</ p > |
032 | < div class = "sl-slide" > |
033 | < h2 class = "sl-title" >Nothing says I love you...</ h2 > |
034 | < p >< img src = "images/6.jpg" /> 'Nothing says I love you...' image description</ p > |
036 | < div class = "sl-slide" > |
037 | < h2 class = "sl-title" >The birds</ h2 > |
038 | < p >< img src = "images/7.jpg" /> 'The birds' image description</ p > |
040 | < div class = "sl-slide" > |
041 | < h2 class = "sl-title" >Have a Seat</ h2 > |
042 | < p >< img src = "images/8.jpg" /> 'Have a Seat' image description</ p > |
044 | < div class = "sl-slide" > |
045 | < h2 class = "sl-title" >Jess and the birds</ h2 > |
046 | < p >< img src = "images/9.jpg" /> 'Jess and the birds' image description</ p > |
048 | < div class = "sl-slide" > |
049 | < h2 class = "sl-title" >Lost Angel</ h2 > |
050 | < p >< img src = "images/10.jpg" /> 'Lost Angel' image description</ p > |
052 | < div class = "sl-slide" > |
053 | < h2 class = "sl-title" >Here I come...</ h2 > |
054 | < p >< img src = "images/11.jpg" /> 'Here I come... image description</ p > |
056 | < div class = "sl-slide" > |
057 | < h2 class = "sl-title" >Inle Lake Reflections</ h2 > |
058 | < p >< img src = "images/12.jpg" /> 'Inle Lake Reflections' image description</ p > |
060 | < div class = "sl-slide" > |
061 | < h2 class = "sl-title" >Evening Excursion</ h2 > |
062 | < p >< img src = "images/13.jpg" /> 'Evening Excursion' image description</ p > |
064 | < div class = "sl-slide" > |
065 | < h2 class = "sl-title" >In the woods</ h2 > |
066 | < p >< img src = "images/14.jpg" /> 'In the woods' image description</ p > |
068 | < div class = "sl-slide" > |
069 | < h2 class = "sl-title" >In the room</ h2 > |
070 | < p >< img src = "images/15.jpg" /> 'In the room' image description</ p > |
072 | < div class = "sl-slide" > |
073 | < h2 class = "sl-title" >batsto</ h2 > |
074 | < p >< img src = "images/16.jpg" /> 'batsto' image description</ p > |
076 | < div class = "sl-slide" > |
077 | < h2 class = "sl-title" >A boy playing with his...</ h2 > |
078 | < p >< img src = "images/17.jpg" /> 'A boy playing with his...' image description</ p > |
083 | < li >< img src = "images/1t.jpg" /></ li > |
084 | < li >< img src = "images/2t.jpg" /></ li > |
085 | < li >< img src = "images/3t.jpg" /></ li > |
086 | < li >< img src = "images/4t.jpg" /></ li > |
087 | < li >< img src = "images/5t.jpg" /></ li > |
088 | < li >< img src = "images/6t.jpg" /></ li > |
089 | < li >< img src = "images/7t.jpg" /></ li > |
090 | < li >< img src = "images/8t.jpg" /></ li > |
091 | < li >< img src = "images/9t.jpg" /></ li > |
092 | < li >< img src = "images/10t.jpg" /></ li > |
093 | < li >< img src = "images/11t.jpg" /></ li > |
094 | < li >< img src = "images/12t.jpg" /></ li > |
095 | < li >< img src = "images/13t.jpg" /></ li > |
096 | < li >< img src = "images/14t.jpg" /></ li > |
097 | < li >< img src = "images/15t.jpg" /></ li > |
098 | < li >< img src = "images/16t.jpg" /></ li > |
099 | < li >< img src = "images/17t.jpg" /></ li > |
102 | < div class = "sl-view-all" >View as one page</ div > |
In the beginning of this main section you can see control buttons, after, the content section with slides and the carousel (UL-LI based)
Step 2. CSS
css/main.css
To adjust our intro section I used next styles:
03 | background-color : #BBBBBB ; |
04 | border : 1px solid #000 ; |
10 | -webkit-border-radius: 3px ; |
11 | -moz-border-radius: 3px ; |
13 | -webkit-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
14 | -moz-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 ,. 5 ); |
15 | box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
19 | margin : 0 10px 10px 0 ; |
It is the basic rounded box with image at the left and text at the right. Now, styles for navigation control buttons:
07 | background : transparent url (sprite.png) no-repeat -100px 0 ; |
14 | .sl-controls .next:hover { |
15 | background-position : -100px -100px |
18 | background : transparent url (sprite.png) no-repeat -50px 0 ; |
26 | .sl-controls .prev:hover { |
27 | background-position : -50px -100px ; |
29 | .sl-controls .back-home { |
30 | background : transparent url (sprite.png) no-repeat 0 0 ; |
38 | .sl-controls .back-home:hover { |
39 | background-position : 0 -100px ; |
I hope that everything is clean with them, next styles are for slides and carousel object:
013 | margin : 0 0 10px 10px ; |
021 | #sl-thumbs li.selected { |
022 | background-color : #FFFFFF ; |
030 | .jcarousel-container { |
035 | background-color : #497e96 ; |
036 | border-left : 20px solid #497e96 ; |
037 | border-right : 20px solid #497e96 ; |
041 | -webkit-border-radius: 3px ; |
042 | -moz-border-radius: 3px ; |
044 | -webkit-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
045 | -moz-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 ,. 5 ); |
046 | box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
048 | .jcarousel-prev-horizontal, .jcarousel-next-horizontal { |
049 | border : 1px solid white ; |
057 | -webkit-border-radius: 3px ; |
058 | -moz-border-radius: 3px ; |
060 | -webkit-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
061 | -moz-box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 ,. 5 ); |
062 | box-shadow: 0 1px 2px 0 rgba( 119 , 119 , 119 , . 5 ); |
064 | .jcarousel-prev-horizontal { |
066 | background : url (sprite.png) no-repeat -165px 3px , -moz-linear-gradient( top , white 0 , #EDEDED 100% ); |
067 | background : url (sprite.png) no-repeat -165px 3px , -webkit-linear-gradient( top , white 0 , #EDEDED 100% ); |
068 | background : url (sprite.png) no-repeat -165px 3px #EDEDED ; |
070 | .jcarousel-next-horizontal { |
072 | background : url (sprite.png) no-repeat -215px 3px ,-moz-linear-gradient( top , white 0 , #EDEDED 100% ); |
073 | background : url (sprite.png) no-repeat -215px 3px ,-webkit-linear-gradient( top , white 0 , #EDEDED 100% ); |
074 | background : url (sprite.png) no-repeat -215px 3px #EDEDED ; |
076 | .jcarousel-prev-horizontal:hover, |
077 | .jcarousel-prev-horizontal:focus { |
078 | background : url (sprite.png) no-repeat -165px -97px ,-moz-linear-gradient( top , #EDEDED 0 , white 100% ); |
079 | background : url (sprite.png) no-repeat -165px -97px ,-webkit-linear-gradient( top , #EDEDED 0 , white 100% ); |
080 | background : url (sprite.png) no-repeat -165px -97px #EDEDED ; |
082 | .jcarousel-prev-horizontal:active { |
083 | background : url (sprite.png) no-repeat -165px 3px ,-moz-linear-gradient( top , white 0 , #EDEDED 100% ); |
084 | background : url (sprite.png) no-repeat -165px 3px ,-webkit-linear-gradient( top , white 0 , #EDEDED 100% ); |
085 | background : url (sprite.png) no-repeat -165px 3px #EDEDED ; |
087 | .jcarousel-prev-disabled-horizontal, |
088 | .jcarousel-prev-disabled-horizontal:hover, |
089 | .jcarousel-prev-disabled-horizontal:focus, |
090 | .jcarousel-prev-disabled-horizontal:active { |
092 | background : url (sprite.png) no-repeat -165px -197px ,-moz-linear-gradient( top ,rgba( 255 , 255 , 255 , 0.5 ) 0 ,rgba( 237 , 237 , 237 , 0.5 ) 100% ); |
093 | background : url (sprite.png) no-repeat -165px -197px ,-webkit-linear-gradient( top ,rgba( 255 , 255 , 255 , 0.5 ) 0 ,rgba( 237 , 237 , 237 , 0.5 ) 100% ); |
094 | background : url (sprite.png) no-repeat -165px -197px #EDEDED ; |
096 | .jcarousel-next-horizontal:hover, |
097 | .jcarousel-next-horizontal:focus { |
098 | background : url (sprite.png) no-repeat -215px -97px ,-moz-linear-gradient( top , #EDEDED 0 , white 100% ); |
099 | background : url (sprite.png) no-repeat -215px -97px ,-webkit-linear-gradient( top , #EDEDED 0 , white 100% ); |
100 | background : url (sprite.png) no-repeat -215px -97px #EDEDED ; |
102 | .jcarousel-next-horizontal:active { |
103 | background : url (sprite.png) no-repeat -215px 3px ,-moz-linear-gradient( top , white 0 , #EDEDED 100% ); |
104 | background : url (sprite.png) no-repeat -215px 3px ,-webkit-linear-gradient( top , white 0 , #EDEDED 100% ); |
105 | background : url (sprite.png) no-repeat -215px 3px #EDEDED ; |
107 | .jcarousel-next-disabled-horizontal, |
108 | .jcarousel-next-disabled-horizontal:hover, |
109 | .jcarousel-next-disabled-horizontal:focus, |
110 | .jcarousel-next-disabled-horizontal:active { |
112 | background : url (sprite.png) no-repeat -215px -197px , -moz-linear-gradient( top ,rgba( 255 , 255 , 255 , 0.5 ) 0 ,rgba( 237 , 237 , 237 , 0.5 ) 100% ); |
113 | background : url (sprite.png) no-repeat -215px -197px , -webkit-linear-gradient( top ,rgba( 255 , 255 , 255 , 0.5 ) 0 ,rgba( 237 , 237 , 237 , 0.5 ) 100% ); |
114 | background : url (sprite.png) no-repeat -215px -197px #EDEDED ; |
117 | .sl-view- all { cursor : pointer ; text-align : right ; } |
Step 3. JS
Finally, our javascript:
js/main.js
002 | function setActiveSlide(i) { |
004 | $( '#count' ).html( (i + 1) + '/' + $( '.sl-slide' ).length ); |
006 | $( 'ul li' ).removeClass( 'selected' ); |
008 | var li = $( 'ul li' ).eq(i); |
009 | li.addClass( 'selected' ); |
011 | $( '.sl-slide' ).removeClass( 'visible' ); |
013 | $( '.sl-slide' ).eq(i).addClass( 'visible' ); |
015 | window.location.hash = $( '.visible .sl-title' ).text().replace(/ /g, '-' ); |
018 | function randomString(len) { |
019 | charSet = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z' ; |
020 | var randomString = '' ; |
021 | for ( var i = 0; i < len; i++) { |
022 | var randomPoz = Math.floor(Math.random() * charSet.length); |
023 | randomString += charSet.substring(randomPoz,randomPoz+1); |
028 | $(document).ready( function (){ |
030 | var bViewAllMode = false ; |
031 | $( '.sl-slide p' ).each( function () { |
032 | $( this ).append( '<br />' + randomString(500) ); |
035 | $( '#sl-thumbs' ).jcarousel(); |
039 | $( '#sl-thumbs li' ).click( function () { |
040 | setActiveSlide($( this ).index()); |
043 | $( '.sl-slide img' ).click( function (){ |
044 | if (! bViewAllMode) { |
046 | var iCur = $( 'ul li.selected' ).index(); |
047 | var iMax = $( 'ul li' ).length - 1; |
048 | var iNext = (iCur + 1 > iMax) ? 0 : (iCur + 1); |
050 | setActiveSlide(iNext); |
055 | $( '.next' ).click( function (){ |
057 | var iCur = $( 'ul li.selected' ).index(); |
058 | var iMax = $( 'ul li' ).length - 1; |
059 | var iNext = (iCur + 1) > iMax ? 0 : (iCur + 1); |
061 | setActiveSlide(iNext); |
065 | $( '.prev' ).click( function (){ |
067 | var iCur = $( 'ul li.selected' ).index(); |
068 | var iMax = $( 'ul li' ).length - 1; |
069 | var iPrev = (iCur - 1 > iMax) ? 0 : (iCur - 1); |
070 | iPrev = (iPrev == -1) ? iMax : iPrev; |
072 | setActiveSlide(iPrev); |
074 | window.location.hash = $( '.visible .sl-title' ).text().replace(/ /g, '-' ); |
078 | $( '.sl-view-all' ).click( function () { |
082 | $( '.sl-controls, .sl-view-all, .jcarousel-container' ).hide(); |
084 | $( '.sl-intro' ).show(); |
085 | $( '.sl-slide' ).addClass( 'visible' ); |
088 | $( '.sl-start' ).click( function () { |
090 | bViewAllMode = false ; |
092 | $( '.sl-intro' ).hide(); |
094 | $( '.sl-controls, .sl-main, .sl-view-all, .jcarousel-container' ).show(); |
100 | $( '.back-home' ).click( function () { |
102 | $( '.sl-main' ).hide(); |
104 | $( '.sl-intro' ).show(); |
In the beginning I defined ‘setActiveSlide’ function to set active slide. Then, in order to fill our slides with dummy text I created ‘randomString’ function. We can see that random (dummy) text on our slides. After, when DOM is ready – we initialize jcarousel object, and add all the necessary event handlers (all of them – onclick events).
Conclusion
That is all for today. We have just developed really good slider for custom content (which can be images, text, media objects etc). I’m sure that this material will be very useful for you. Good luck and welcome back.