Create your own jQuery plugin – slider
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
download in package
Lets start coding !
Step 1. HTML
As usual, we start with the HTML. This is source code of our sample:
index.html
<link rel="stylesheet" href="css/main.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<div class="example">
<h3><a href="#">My Slider example</a></h3>
<ul id="my_slider">
<li><img src="data_images/pic1.jpg" alt="" /></li>
<li><img src="data_images/pic2.jpg" alt="" /></li>
<li><img src="data_images/pic3.jpg" alt="" /></li>
<li><img src="data_images/pic4.jpg" alt="" /></li>
<li><img src="data_images/pic5.jpg" alt="" /></li>
<li><img src="data_images/pic6.jpg" alt="" /></li>
<li><img src="data_images/pic7.jpg" alt="" /></li>
<li><img src="data_images/pic8.jpg" alt="" /></li>
<li><img src="data_images/pic9.jpg" alt="" /></li>
<li><img src="data_images/pic10.jpg" alt="" /></li>
</ul>
<div id="counter"></div>
</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
body{background:#eee;margin:0;padding:0}
.example{background:#FFF;width:500px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
/*My slider styles*/
#my_slider {
width:500px;
height:340px;
overflow: hidden;
position:relative;
list-style: none outside none;
padding:0;
margin:0;
}
#my_slider li {
position: absolute;
top: 0px;
left: 0px;
display:none;
}
#my_slider li:first-child {
display:block;
}
#counter {
text-align:right;
font-size:16px;
width:500px;
}
Step 3. JS
Here are all JS files:
js/main.js
(function($){
$.fn.MySlider = function(interval) {
var slides;
var cnt;
var amount;
var i;
function run() {
// hiding previous image and showing next
$(slides[i]).fadeOut(1000);
i++;
if (i >= amount) i = 0;
$(slides[i]).fadeIn(1000);
// updating counter
cnt.text(i+1+' / '+amount);
// loop
setTimeout(run, interval);
}
slides = $('#my_slider').children();
cnt = $('#counter');
amount = slides.length;
i=0;
// updating counter
cnt.text(i+1+' / '+amount);
setTimeout(run, interval);
};
})(jQuery);
// custom initialization
jQuery(window).load(function() {
$('.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.
Live Demo
download in 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!

Hello! I love this script. Could you please do one more tutorial showing how to add in a caption feature, and next/previous image links? Thank you!
Yes, sure, I will try
Do you need to call setTimeout(run, interval); twice? Calling it once on line 30 should be enough.
You can change
setTimeout(run, interval);
to
run(); at line 30.
To get a timer to work in an infinite loop, we need write a function that calls itself.
Loved the tutorial. Simple and as the saying goes “simple is most effective”
I’ve tried this and it will not open up in IE9…
Any suggestions?
Why?
It working without errors in IE9, are you tried to wait a little until images loaded?
The first image appears in IE9 but the rest don’t seem to work – the first image fades to white and no more appear. It works fine in Firefox and Safari. Please help?
Can you tell us version of your IE9? Just because I tried it in my v9.0.8112 right now – and it worked to me. I just waited a little.
Are you got any warnings in IE?
Thanks, awesome tutorial!
How to make the slider stop when hovering mouse over it…
Hi sidharth,
Shortly – you should add MouseOver event handler, and stop our timer in this moment (pause). Or, you can implement alternative solution with adding some new variable, and, in case when we keep mouse over our slider – set this variable to TRUE (as example), and in our cycle (run function) – check for this variable.
How would I make the pictures in the slider a clickable link? I tried using the image as a link but I ended up with a blank and no picture would display
Hello Chris,
You can try to do clickable images in next way:
<li><img src="data_images/pic1.jpg" alt="" /></li>
change to
<li><a href="your_address"><img src="data_images/pic1.jpg" alt="" /></a></li>
after, in our JS code we have to change
slides = $(‘#my_slider’).children();
to
slides = $(‘#my_slider li’);
Great tutorial! Just what I was looking for.. One question though. Is it possible to slow the time it transitions to the next slide? Meaning have the slide last longer?
Nevermind. I probably should have read before asking! LOL
Thanks!
The tutorial is fine, but one small comment is that it isn’t actually a slider. This is more of a “carousel” as the images don’t actually “slide” in and out of view.
Interesting suggestion, I thought that a slider is something (object) where we can see replacing each other images, but anyway yes- possible.
what’s the use of this one?
$(‘.smart_gallery’)
where’d you get it?
Actually, we don’t need it, this selector is not used
Thanks! It works amazing