Clear background Blurry background
  • dev / 

    JavaScript Slider

    This script is no longer under development! Please follow this link for an updated, open-source version of this script, called Dragdealer: ovidiu.ch/dragdealer-js.

    Since the comment slider-button drew some attention among the visitors, I figured I'd begin my technical input by presenting (and sharing) the slider script; which turned out to be quite a component. It can be used in many ways – be my guest.

    Here is a demo page that exhibits some potential scenarios. Below you have all the info you need in order to use it: its definition, options and methods. Also, of course, the files to download.

    Constructor

    /*
    HTML code
    <div id="my-slider" class="slider">
        <div class="handle">drag me</div>
    </div>
    Initializing the Slider
    */
    new Slider('my-slider', {
        callback: function(value) {
            alert(value);
        }
    });
    

    Slider Slider(Mixed wrapper [, Object options ])

    • Mixed wrapper - This can be the Slider's HTML id, or the actual element itself if you have it "at hand"
    • Object options - Supported options (below)

    Options

    /*
    Setting options
    */
    new Slider('my-slider', {
        value: 1,
        steps: 5,
        snapping: true,
        callback: function(value) {
           // do something
        }
    });
    
    • Boolean disabled - Initial state; default: false
    • Number value - Initial value [0,1]; default: 0
    • Number steps - Amount of steps to limit to (less than 2 means no steps); default: 0
    • Boolean snapping - Snapping to steps (if set); default: false
    • Function callback - Callback function (on release); default: null
    • Function animation_callback - Animation callback function (on move); default: null
    • Number speed - Sliding speed of the handle; default: 5
    • Number pleft, pright - Left and/or right padding for the rail *; default: 0

    * If you want the padding to be equal in both left and right side, you can set it from CSS.

    Methods

    /*
    Using methods
    */
    var slider = new Slider('my-slider');
    slider.disable();
    slider.enable();
    slider.setValue(0.2);
    slider.setValue(1, true);
    
    • enable() - Enable user interaction
    • disable() - Disable user interaction
    • setValue(Number value [, Boolean snap = false ]) - Set the specified value, with the ability to slide or to snap directly

    In case you're wondering, yes, you can easily change the shape and look of the slider, with modest CSS skills. Just look through the CSS file from the archive and you'll know what I mean.

    Let me know if you stumbled across any problem with the script, or, even better, if you have a cool implementation of it to share. Looking forward.

  • Beautiful scripts.. I only dont like that worldwide scripts are so complex.. I want just a left to right Slider like a scrollbar..

    good work.

    +Reply

    Posted December 2012, by Peter Geoy

  • Hello, I have a question.
    I'm trying to Create a min max option vor this script. min:-100, max:100; I have read that its not in the option list, Now I'm looking trough the code, but I cant seem to find the [0,1] Values. So I can change it to [0,2], To simulate a [-100,0,100], which are also 3 steps.

    Hoping for a answer.

    +Reply

    Posted April 2012, by Exza

  • What about a slider that works on the iPad?

    +Reply

    Posted September 2011, by Marlene

    It does work. Just check the link at the top of the article for a newer version.

    +Reply

    Posted September 2011, by Ovidiu Cherecheș

  • I really like this script. However, my images will not slide in IE7. Works fine in other browsers, including IE 8, but when I enable Compatibility View or IE 7 mode via Developer Tools, it stays stuck on the first image. Not sure what is going on.

    +Reply

    Posted June 2011, by websitedesigningsingapore

    I don't remember it having a problem on IE 6 or 7. But please, use the Dragdealer ( ovidiu.ch/dragdealer-js ) script, this is an outdated version. Thanks!

    +Reply

    Posted June 2011, by Ovidiu Cherecheș

  • need 2/3 sliders next to each other? I tried to get 2 next to each other but as expected only one slider will work. I hope you can enlighten me a bit on this.

    +Reply

    Posted June 2011, by Ranjan

    Your question is too vague. Normally there shouldn't be a problem, I've done it many times.

    +Reply

    Posted June 2011, by Ovidiu Cherecheș

  • I would like to make a slide to unlock look a like.I can't manage to let it drag and on release return to position. when drag to right it has to snap and go to url...

    new Slider('my-slider', {
        steps: 2,
        speed: 5,
        callback: function(value) {	
        }
    });

    +Reply

    Posted October 2010, by Milcoi

    I'm not sure I understand what you want to do. Drop me an email if you want and I'll see if I can help you. Also please use the newer "Dragdealer JS" script.

    +Reply

    Posted October 2010, by Ovidiu Cherecheș

  • I'm really impressed with slider and it works fine with the sample.htm. However when I tried to integrate with an openlayers app to control opacity, while it generally worked, ie the opacity changed with the slider , the slide icon would not drag, it just jumped and
    Firebug reported an error in slider.js:

    document.selection.empty is not a function

    This appears to be in the function preventDefaults (line 396).

    It works fine in IE 8, fails in FF 3.6.8. both as localhost. It's probably something i'm doing wrong so if you could give me a clue ....

    (PS Obviously your site is OK since I could post this)

    +Reply

    Posted July 2010, by mikew

    As I told to Patrick, please try to newer Dragdealer JS script first. My bad, I should add a big message at the beginning of this page linking to it.

    +Reply

    Posted August 2010, by Ovidiu Cherecheș

  • Thanks a lot for the slider! I Found it very easy to use and customize to my needs. Great Work!!

    +Reply

    Posted July 2010, by Kamil P.

  • This works great on every device I've seen so far (iPad, iPhone, Nexus One, etc). I was trying to implement this on a when I ran into an issue when using another jQuery library. Have you had any problems with that or know of why it might be causing errors?

    +Reply

    Posted June 2010, by Patrick

    This is an outdated version of the script. Try the Dragdealer JS (newer post) and let me know if the problems persist. If so, please provide more details about that particular jQuery library.

    +Reply

    Posted June 2010, by Ovidiu Cherecheș

  • Hey Ovidiu, thanks for the great script. It's really just what I was looking for.

    I just wanted to let you know how I altered your script for my use:

    I personally wanted the "handle" to show the current value as it is being dragged. The callback function only works on mouseup, so I added a method to the prototype, called showValue(), which I am calling in the slide() method. As long as snapping is set to true, I call showValue() which updates the handle's innerHTML with the current value.

    It seems to work great. Thanks again for the great script!

    +Reply

    Posted May 2010, by Rob

  • @Ovidiu: awsome job, well done. I'd like to know if you can share the version working with safari mobile for iPhone. Thankyou

    +Reply

    Posted May 2010, by simone

    Thanks! I have been working on a fully updated script with lots of added features and functionalities, including of course the mobile touch support. It will come out very soon – Stay tunned!

    +Reply

    Posted May 2010, by Ovidiu Cherecheș

  • This is cool. I modified this to support mobile browsers (iPhone and Android) via touch events (since mouse move events don't work on touchscreens). Shoot me an email if you want the version. Thanks again.

    +Reply

    Posted April 2010, by Pratik

    Sweet! I just noticed that on iPhone and Android phones and was wondering how should I approach it. Thanks, I'll get back to you.

    +Reply

    Posted April 2010, by Ovidiu Cherecheș

  • How about a simple carousel? :)

    +Reply

    Posted April 2010, by Eddie

    So many carousels out there and there still isn't one that just works? Bummer. I'll consider it.

    +Reply

    Posted April 2010, by Ovidiu Cherecheș

What do you think?

And you are?

...