Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Slider - Disable click on slider track

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 the track.

slider set up in document.ready() as follows:

$('#navScroller').slider({
      value: 0,
      start: onNavScrollStart,
      slide: onNavScrollSlide,
      animate: slideSpeed,
      max: slideWidth
});

I tried both:

$('#navScroller').unbind('click');

and

$('#navScroller .ui-slider').unbind('click');

neither of which worked to stop the slider from responding to track clicks.

Any ideas on how to accomplish this? Thanks!

EDIT: I just discovered that using:

$('#navScroller').unbind('mousedown');

removes clicks from the whole slider, handle and all, which is closer to what i need but I still need to be able to drag the handle.

like image 812
Ribs Avatar asked Nov 30 '11 18:11

Ribs


3 Answers

There's a non-JS solution to this as well, using only two lines of CSS:

/* Supress pointer events */
#navSlider { pointer-events: none; }
/* Enable pointer events for slider handle only */
#navSlider .ui-slider-handle { pointer-events: auto; }
like image 149
Jaron Avatar answered Oct 21 '22 02:10

Jaron


@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
    });
like image 7
Ryan Tomac Avatar answered Oct 21 '22 02:10

Ryan Tomac


As JQuery already bound your container (slider) div with mouse down event, so even if you unbind your range div from it, it'll still fire on the container div,

here's the solution:

$('#navScroller .ui-slider-range').mousedown(function() {
      return false;
});

Regards,

like image 5
Amr Faisal Avatar answered Oct 21 '22 04:10

Amr Faisal