12

How do I add tick marks to the jQuery slider? Say I have values from 1 to 10, how I can I add a tick at each value?

I've seen similar posts on S.O. but they all suggest plug-ins, and I would like to hard code it due to a lot of interactivity with other elements.

Thanks!

0

5 Answers 5

17

Thanks Code-Toad. I modified your code to work with percents instead of pixels, so now it's immune to window-resize:

Javascript:

function setSliderTicks(){
    var $slider =  $('#slider');
    var max =  $slider.slider("option", "max");    
    var spacing =  100 / (max -1);

    $slider.find('.ui-slider-tick-mark').remove();
    for (var i = 0; i < max ; i++) {
        $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) +  '%').appendTo($slider); 
     }
}

CSS:

.ui-slider-tick-mark{
    display:inline-block;
    width:2px;
    background:black;
    height:16px;
    position:absolute;
    top:-4px;
}
1
  • .ui-slider-tick-mark { display: inline-block; width: 2px; background: #fcf8e3; height: 28px; position: absolute; top: 0px; } If you want the styles to appear at the bottom. This is a mod of your code. Thanks.
    – blackmambo
    Dec 4, 2015 at 7:50
10

Thanks Arie Livshin and CodeToad. The previous code assume that the min value is always 1. I edited the code to work with min values too.

$("#users-slider").slider(
  {
    range: "min",
    value: 3,
    min: 3,
    max: 6,
    create: function( event, ui ) {
      setSliderTicks(event.target);
    },
  }
);

function setSliderTicks(el) {
    var $slider =  $(el);
    var max =  $slider.slider("option", "max");    
    var min =  $slider.slider("option", "min");    
    var spacing =  100 / (max - min);

    $slider.find('.ui-slider-tick-mark').remove();
    for (var i = 0; i < max-min ; i++) {
        $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) +  '%').appendTo($slider); 
     }
}
2
  • you may want to change the loop to start from i=1. this makes ui look better in my opinion
    – harshit
    Jul 31, 2016 at 3:19
  • Use i <= max-min to show last tick mark. Jun 25, 2018 at 10:10
4

here is a simple solution assuming that slider interval = 1.

function setSliderTicks(){
    var $slider =  $('#slider');
    var max =  $slider.slider("option", "max");    
    var spacing =  $slider.width() / (max -1);

    $slider.find('.ui-slider-tick-mark').remove();
        for (var i = 0; i < max ; i++) {
            $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) + 'px').appendTo($slider);                    
        }
}


.ui-slider-tick-mark{
display:inline-block;
width:2px;
background:black;
height:16px;
position:absolute;
top:-4px;
}
0
3

Similar to this SO Question. The answer from Mortimer might help.

Although there is no official way of doing it yet, it'd be nice to have it baked in the jQuery UI slider.

1
  • 2
    @DonnyP: Mando was specifically calling your attention to Mortimer's Answer, not Bijan's Mar 25, 2012 at 23:48
0

You could use a background image that has the marks in the right places - this would probably be the simplest way.

1
  • The range of the slider is dynamic, so using a background image won't work. Thanks though. I suspect there are CSS solutions
    – Don P
    Dec 27, 2011 at 20:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.