简体   繁体   English

如何在bootstrap-slider.js中禁用滑块?

[英]how to disable slider in bootstrap-slider.js?

is there any solution to stop dragging when click on the button . 是否有任何解决方案,以便在单击button时停止拖动。

the documentation do not give proper description 文档没有给出正确的描述

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

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

});

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

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 演示: http//bootply.com/83445

Example html : 示例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: 示例javascript:

$('#slide').slider();

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

The functionality to disable sliders has been implemented by setting the data-slider-enabled attribute to true or false . 通过将data-slider-enabled属性设置为truefalse可以实现禁用滑块的功能。

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: 您还可以使用jQuery启用和禁用这样的滑块:

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

Or like this with pure JavaScript: 或者像纯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");
});

You need to bind the mouseenter / mouseleave events to show/hide the tooltip: 您需要绑定mouseenter / mouseleave事件以显示/隐藏工具提示:

$.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)
        });
    }
}

Simply have a custom css class on the slider container which controls the mouse events with pointer-events. 只需在滑块容器上有一个自定义css类,它使用指针事件控制鼠标事件。 Then it is just about adding or removing it in your javascript code. 然后它只是在您的JavaScript代码中添加或删除它。

CSS would look like something like this CSS看起来像这样

#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: 使用angular特别好看,你的代码就像这样:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM