How to Create your own jQuery Slider Plugin

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:

Live Demo

[sociallocker]

download in package

[/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>
04 <div class="example">
05     <h3><a href="#">My Slider example</a></h3>
06     <ul id="my_slider">
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>
17     </ul>
18     <div id="counter"></div>
19 </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}
03 /*My slider styles*/
04 #my_slider {
05     width:500px;
06     height:340px;
07     overflowhidden;
08     position:relative;
09     list-stylenone outside none;
10     padding:0;
11     margin:0;
12 }
13 #my_slider li {
14     positionabsolute;
15     top0px;
16     left0px;
17     display:none;
18 }
19 #my_slider li:first-child {
20     display:block;
21 }
22 #counter {
23     text-align:right;
24     font-size:16px;
25     width:500px;
26 }

Step 3. JS

Here are all JS files:

js/main.js

01 (function($){
02     $.fn.MySlider = function(interval) {
03         var slides;
04         var cnt;
05         var amount;
06         var i;
07         function run() {
08             // hiding previous image and showing next
09             $(slides[i]).fadeOut(1000);
10             i++;
11             if (i >= amount) i = 0;
12             $(slides[i]).fadeIn(1000);
13             // updating counter
14             cnt.text(i+1+' / '+amount);
15             // loop
16             setTimeout(run, interval);
17         }
18         slides = $('#my_slider').children();
19         cnt = $('#counter');
20         amount = slides.length;
21         i=0;
22         // updating counter
23         cnt.text(i+1+' / '+amount);
24         setTimeout(run, interval);
25     };
26 })(jQuery);
27 // custom initialization
28 jQuery(window).load(function() {
29     $('.smart_gallery').MySlider(3000);
30 });

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.


Live Demo

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!