Search code examples
javascriptjqueryrangeslider

step size change


I am using the excellent rangeslider.js by Andre Ruffert

I have one question. I have the slider set up so my numbers increase in increments of 6 as I move the slider along like so: 6,12,18,24,30,36,42,48,54,60

However I want the slider to increase by increments of 12 when it reaches 36 thus skipping out '42' and '54' so that when I move the slider it can be pulled along to the following: 6,12,18,24,30,36,48,60

is there a way that i can do this in js or html?

This is the code that should set up the slider values but for some reason this doesn't work:

$(function() {
        var valMap = [6, 12, 18, 24, 36, 48, 60];
        $("#term").rangeslider({
            min: 6,
            max: valMap.length - 1,
            value: 24,
            slide: function(event, ui) {                        
                $("#amount").val(valMap[ui.value]);                
            }       
        });

I can't get the JavaScript functions to overwrite the HTML code for the slider which reads like this:

<input type="range" name="term" min="6" max="60" step="6" value="36" data-rangeslider>

Solution

  • You could check the value on onSlide event and change the step property of the slider once its reached 36:

    var $r = $('input[type=range]');
    onSlide: function(position, value) {
        if (v>=36) {      
          $r.data().plugin_rangeslider.step = 12;
        }
        else {
          $r.data().plugin_rangeslider.step = 6;
        }
    }
    

    Check this codepen

    EDIT:

    The values: min, max and value you should put as attributes on the html element and not while initializing. Also there is no slide function. Look at this example (codepen)

    $("#term").rangeslider({
        polyfill: false,
        onInit: function() {
            $ruler[0].innerHTML = getRulerRange(this.min, this.max, rulerStep);
            this.$range.prepend($ruler);},
        onSlide: function(p,v) {
            if (!$("#term").data().plugin_rangeslider)
              return;
            if (v>=36) {      
              $("#term").data().plugin_rangeslider.step = 12;
            }
            else {
              $("#term").data().plugin_rangeslider.step = 6;
            }
        },
        onSlideEnd: function(p, v) {
            $('#val').attr('value', v)
        }    
    });