Today we will create own first jQuery plugins. As one of easy task – we will create own image slider (commonly – of any content, not just images). Our slider will switch between slides using the fade effect.
Firstly – you can download our package and check demo:
[sociallocker]
[/sociallocker]
Lets start coding !
Step 1. HTML
As usual, we start with the HTML. This is source code of our sample:
index.html
01 |
< link rel = "stylesheet" href = "css/main.css" type = "text/css" /> |
02 |
< script type = "text/javascript" src = "js/jquery-1.5.2.min.js" ></ script > |
03 |
< script type = "text/javascript" src = "js/main.js" ></ script > |
05 |
< h3 >< a href = "#" >My Slider example</ a ></ h3 > |
07 |
< li >< img src = "data_images/pic1.jpg" alt = "" /></ li > |
08 |
< li >< img src = "data_images/pic2.jpg" alt = "" /></ li > |
09 |
< li >< img src = "data_images/pic3.jpg" alt = "" /></ li > |
10 |
< li >< img src = "data_images/pic4.jpg" alt = "" /></ li > |
11 |
< li >< img src = "data_images/pic5.jpg" alt = "" /></ li > |
12 |
< li >< img src = "data_images/pic6.jpg" alt = "" /></ li > |
13 |
< li >< img src = "data_images/pic7.jpg" alt = "" /></ li > |
14 |
< li >< img src = "data_images/pic8.jpg" alt = "" /></ li > |
15 |
< li >< img src = "data_images/pic9.jpg" alt = "" /></ li > |
16 |
< li >< img src = "data_images/pic10.jpg" alt = "" /></ li > |
18 |
< div id = "counter" ></ div > |
As you can see – all very easy, here are UL-LI structure with slider tabs inside. In end – additional counter element.
Step 2. CSS
Here are used CSS file with styles of our demo:
css/main.css
01 |
body{ background : #eee ; margin : 0 ; padding : 0 } |
02 |
.example{ background : #FFF ; width : 500px ; border : 1px #000 solid ; margin : 20px auto ; padding : 15px ;-moz-border-radius: 3px ;-webkit-border-radius: 3px } |
09 |
list-style : none outside none ; |
19 |
#my_slider li:first-child { |
Step 3. JS
Here are all JS files:
js/main.js
02 |
$.fn.MySlider = function (interval) { |
09 |
$(slides[i]).fadeOut(1000); |
11 |
if (i >= amount) i = 0; |
12 |
$(slides[i]).fadeIn(1000); |
14 |
cnt.text(i+1+ ' / ' +amount); |
16 |
setTimeout(run, interval); |
18 |
slides = $( '#my_slider' ).children(); |
20 |
amount = slides.length; |
23 |
cnt.text(i+1+ ' / ' +amount); |
24 |
setTimeout(run, interval); |
28 |
jQuery(window).load( function () { |
29 |
$( '.smart_gallery' ).MySlider(3000); |
Firstly, make attention to $.fn.MySlider, so it mean that I registering my function as new jQuery function (expanding its functions). Then I made simple switching mechanism between images. And, using fadeOut-fadeIn – perform ‘switching’. So, as result, we can call ‘MySlider’ function for any UL element which we want to use as slider. Example: $(‘.smart_gallery’).MySlider(3000); (where 3000 – delay between switching).
js/jquery-1.5.2.min.js
This is default jQuery library. Available in our package.
Conclusion
Today I told you how to build new own jQuery plugin. Sure that this will useful for you. Our sample result was very easy, and very small. The whole JS code can even be compressed to 284 bytes 🙂 Good luck!