0

I am trying to create a custom slider with two handles using jQuery UI. The problem is that the right handle is moving out of the container.

I know the problem, the reason being the fact that the handles are positioned using the left CSS property and since the width of the handles is more than one pixel, the right handle moves out of the container.

I don't want to use the default slider UI provided by jQuery and that's why I am fighting with this.

The code is uploaded on JSBin - http://jsbin.com/egoca/edit, copied here for prosperity:

$(document).ready(function(){ 
  var end = parseInt($('#slider_range_slide').attr('minvalue'), 10); 
  var start  = parseInt($('#slider_range_slide').attr('maxvalue'), 10); 

  $('#slider_bar').slider({ 
    min: 0, 
    max: 100, 
    range: true, 
    values: [0, 100], 
    animate: true, 
    slide: function(e, ui) { 
      var left = $('#slider_bar').slider('values', 0); 
      var right = $('#slider_bar').slider('values', 1); 
      $(".slide_dis_l").css("width", left+"%"); 
      $(".slide_dis_r").css("width", (100 - right)+"%"); 
      $("#slider_handle_left").css("left", left+"%"); 
      $("#slider_handle_right").css("right", (100 - right)+"%"); 
      var from = start + (left / 100) * (end - start); 
      var to = (right / 100) * (end - start); 
      $('#price_slider_display').text(from + " to " + to); 
    } 
  }); 
});

Any help in fixing this will be highly appreciated.

1 Answer 1

1

I think you're going to have to live with a simple fix unless you want to redevelop the entire thing with a lot of added (and unnecessary) complexity. Even the jQuery UI sliders overlap the left and right boundaries.

The best way to solve this is to apply a negative left-margin that's half the width of the handles as demonstrated here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.