how to disable slider in bootstrap-slider.js?

16,419

Solution 1

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') });

Solution 2

The functionality to disable sliders has been implemented by setting the data-slider-enabled attribute to true or false.

So you can implement a disabled slider like this:

<input id="slide" type="text" data-slider-min="0" data-slider-max="20" data-slider-step="1" data-slider-value="5" data-slider-enabled="false"/>

Or an enabled slider like this:

<input id="slide" type="text" data-slider-min="0" data-slider-max="20" data-slider-step="1" data-slider-value="5" data-slider-enabled="true"/>

You can also enable and disable your sliders like this with jQuery:

$("#slide").slider();
$("#slide").slider("enable");
$("#slide").slider("disable");

Or like this with pure JavaScript:

var slide = new Slider("#slide");
slide.enable();
slide.disable();

For your implementation you would need to do this:

$("#stopDrag").click(function(){
    $("#slide").slider("disable");
});

Solution 3

Simply have a custom css class on the slider container which controls the mouse events with pointer-events. Then it is just about adding or removing it in your javascript code.

CSS would look like something like this

#container {
    pointer-events:auto;
}
#container.slider-unavailable {
    pointer-events:none;
}

The you only have to work on adding/removing the class on the slider container element. It is particularly nice with angular where your code is simply something like this:

<div class="container" ng-class="{'class1 class2 class3':true, 'slider-unavailable':sliderIsUnavailable}">
    <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"><br>
    <label>Slider is unavailable:
        <input type="checkbox" ng-model="sliderIsUnavailable">
    </label><br>
</div>

Solution 4

You need to bind the mouseenter / mouseleave events to show/hide the tooltip:

$.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),
            mouseenter: $.proxy(this.showTooltip, this),
            mouseleave: $.proxy(this.hideTooltip, this)
        });
    } else {
        this.picker.on({
            mousedown: $.proxy(this.mousedown, this),
            mouseenter: $.proxy(this.showTooltip, this),
            mouseleave: $.proxy(this.hideTooltip, this)
        });
    }
}
Share:
16,419
Mo.
Author by

Mo.

Every nice creation start from imagination followed by dedication. Contacts 👇 Portfolio LinkedIn

Updated on June 24, 2022

Comments

  • Mo.
    Mo. almost 2 years

    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/

  • philippe_b
    philippe_b about 10 years
    Thank you for your comprehensive answer. It definitely deserves more votes! However, I realized that it was not compatible with the tooltip: once the slider is disabled, the tooltip is gone forever. This is because mouseenter and mouseleave events are not handled anymore. I was able to fix this, nothing fancy. I thought you may want to update your answer to take this point into account.
  • DeadlyChambers
    DeadlyChambers about 10 years
    How did you fix that?
  • user1241091
    user1241091 almost 10 years
    I am trying to use the above method to display tooltip for touch devices on 'touchend' on the handle. The problem I am facing is, when I call this method like:--- $('#slide').slider('enable'); ---- another slider instance seems to be created. Is there any other way to call this method in scope?
  • Adam Copley
    Adam Copley about 8 years
    FYI This is now built in!
  • Barry Michael Doyle
    Barry Michael Doyle almost 7 years
    Can you post a new answer demonstrating the built in functionality?