javascript - How to add onclick functionality for bootstrap slider? - Stack Overflow

I have bootstrap slider. It works perfectly, Currently my slider event works when I change the value by

I have bootstrap slider. It works perfectly, Currently my slider event works when I change the value by dragging action. But How to add event for when I click on the slider bar?

myHtml:

<div class="slider warning " style="padding-right: 20px;">
        <label style="font-family: 'Open Sans';padding-top: 10px;">Variants</label><div style="text-align: center" id='res1'>0</div><br>
        <input type="text"  id="variantslider" class="slider-element form-control" value=""  data-slider-value="20" data-slider-step="1" data-slider-max="20" data-slider-min="1" data-slider-orientation="vertical" data-slider-selection="before" data-slider-tooltip="hide" >
</div>

myJs:

$('#variantslider').slider().on('slideStart', function(ev){
    originalVal = $('#variantslider').data('slider').getValue();
});

//Catch slider stop event and capture value
$('#variantslider').slider().on('slideStop', function(ev){
    $('#variantslider').slider().on('slideStop', function(ev){
        var newVal = $('#variantslider').data('slider').getValue();
        //detect if slider value has been changed
        if(originalVal != newVal) {
            //my ops
        }
    });
});

I have bootstrap slider. It works perfectly, Currently my slider event works when I change the value by dragging action. But How to add event for when I click on the slider bar?

myHtml:

<div class="slider warning " style="padding-right: 20px;">
        <label style="font-family: 'Open Sans';padding-top: 10px;">Variants</label><div style="text-align: center" id='res1'>0</div><br>
        <input type="text"  id="variantslider" class="slider-element form-control" value=""  data-slider-value="20" data-slider-step="1" data-slider-max="20" data-slider-min="1" data-slider-orientation="vertical" data-slider-selection="before" data-slider-tooltip="hide" >
</div>

myJs:

$('#variantslider').slider().on('slideStart', function(ev){
    originalVal = $('#variantslider').data('slider').getValue();
});

//Catch slider stop event and capture value
$('#variantslider').slider().on('slideStop', function(ev){
    $('#variantslider').slider().on('slideStop', function(ev){
        var newVal = $('#variantslider').data('slider').getValue();
        //detect if slider value has been changed
        if(originalVal != newVal) {
            //my ops
        }
    });
});
Share Improve this question edited Jul 16, 2018 at 6:53 Simon.Lay 2582 silver badges10 bronze badges asked Jul 16, 2018 at 5:48 Mohamed Thasin ahMohamed Thasin ah 11.2k11 gold badges64 silver badges119 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You should check the documentation of the slider on the link below if you didn't yet.

https://github./seiyria/bootstrap-slider

You could see all events you need to check if the value is changed.

For click events over the slider you can use "change" or "slideStop" event.

When "change" event is used "event.value" returns an object with 2 properties: "oldValue" and "newValue";

Here you are the examples:

$("#variantslider").slider();

$("#variantslider").on("slideStop", function(event){
   console.log('slider value: ', event.value);
});

$("#variantslider").on("change", function(event){
   console.log('slider value: ', event.value.oldValue, event.value.newValue);
});

It's the same with mon Jquery doing.

$("#variantslider").on("click", function(){
    alert(" I'm clicked! ");
});

I try it, and it works well. Here is my example :

https://jsfiddle/453krf90/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744690330a4588173.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信