jQuery UI Slider - Disable click on slider track

后端 未结 13 1299
南方客
南方客 2020-12-31 08:57

I\'m trying to find a way to disable the click on my jQuery UI Slider\'s track. Clicking on the handle is ok but I do not want the handle to respond if the user clicks on t

13条回答
  •  失恋的感觉
    2020-12-31 09:07

    @btate has the right idea (abandoning mousedown/touchstart that don't originate in the slider handle), but there is an easier way to get there. jQuery's event object will tell you which element initiated the event.

    var sliderMouseDown = function (e) { // disable clicks on track
        var sliderHandle = $('#navScroller').find('.ui-slider-handle');
        if (e.target != sliderHandle[0]) {
            e.stopImmediatePropagation();
        }
    };
    
    $('#navScroller')
        .on('mousedown', sliderMouseDown)
        .on('touchstart', sliderMouseDown)
        .slider({
              value: 0,
              start: onNavScrollStart,
              slide: onNavScrollSlide,
              animate: slideSpeed,
              max: slideWidth
        });
    

提交回复
热议问题