How to Create your own jQuery Slider Plugin

Date: 19th May 2011 Author: admin 20 Comments
Posted in: jQuery |Tags: , ,

Creating own jQuery plugin - slider

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!

Enjoyed this Post?

   
   
   
If you enjoyed this article, feel free to share our tutorial with your friends.

20 Comments

    • quiet0ne's Gravatar
    • 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!

    • Adrian Lynch's Gravatar
    • Mr_Rue's Gravatar
    • 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?

    • John's Gravatar
    • 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?

    • Juan's Gravatar
    • sidharth's Gravatar
    • Chris's Gravatar
    • 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

    • Ron's Gravatar
    • 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?

    • Ron's Gravatar
    • Custom Software Development Company's Gravatar
    • Ged's Gravatar
    • Ruben's Gravatar

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>