Search code examples
javascriptjquerytwitter-bootstrapslidertwitter-bootstrap-3

how to disable slider in bootstrap-slider.js?


is there any solution to stop dragging when click on the button.

the documentation do not give proper description

http://www.eyecon.ro/bootstrap-slider/

$("#stopDrag").click(function(){

});

DEMO http://jsfiddle.net/sweetmaanu/npGw2/


Solution

  • Extend the slider plugin with a enable and disable function like:

    <script src="/slider/js/bootstrap-slider.js"></script>
    <script>
    $.fn.slider.Constructor.prototype.disable = function () {
        this.picker.off();
    }
    $.fn.slider.Constructor.prototype.enable = function () {
        if (this.touchCapable) {
            // Touch: Bind touch events:
            this.picker.on({
                touchstart: $.proxy(this.mousedown, this)
            });
        } else {
            this.picker.on({
                mousedown: $.proxy(this.mousedown, this)
            });
        }
    }
    </script>
    

    Demo: http://bootply.com/83445

    Example html:

    <div class="container" style="background-color:darkblue;">
    <br>
    <input id="slide" type="text" class="span2" value="" data-slider-min="-20" data-slider-max="20" data-slider-step="1" data-slider-value="-14" data-slider-orientation="vertical" data-slider-selection="after">
    
    <button id="enable">Enable</button>
    <button id="disable">Disable</button>
    </div>
    

    example javascript:

    $('#slide').slider();
    
    $('#enable').click(function(){ $('#slide').slider('enable') });
    $('#disable').click(function(){ $('#slide').slider('disable') });