Search code examples
javascriptjqueryuislider

Show numbered intervals above jQuery slider


Hallo,

I've added a jQuery slider and now I want to have numbers above the slider which denotes the intervals. Almost like a everyday ruler. Does anyone have a quick method to accomplish this?


Solution

  • Have a look at this jQuery UI plugin: http://www.filamentgroup.com/lab/update_jquery_ui_slider_from_a_select_element_now_with_aria_support/

    It's actually a more sophisticated version of the jQuery UI slider created by the same people that sponsors jQuery UI. However this uses the select HTML element instead of lists, so you may want to strip out the tic adding function from the plugin and use that instead (not easy, but it's better than writing your own I hope!).

    This is what the plugin uses:

    var scale = sliderComponent.append('<ol class="ui-slider-scale ui-helper-reset" role="presentation"></ol>').find('.ui-slider-scale:eq(0)');
    jQuery(selectOptions).each(function(i){
        var style = (i == selectOptions.length-1 || i == 0) ? 'style="display: none;"' : '' ;
        var labelText = (options.labelSrc == 'text') ? this.text : this.value;
        scale.append('<li style="left:'+ leftVal(i) +'"><span class="ui-slider-label">'+ labelText +'</span><span class="ui-slider-tic ui-widget-content"'+ style +'></span></li>');
    });
    

    For every option element it adds a tic, with a label added by another block of code further down. What it's doing is basically injecting a list of spans into the slider element, with the text taken directly from the element's value attribute. You'll also need quite a bit of CSS to style that properly into tics.