4

I've been able to style it flawlessly but getting it to stay within it's container has been a challenge. The margin-left fix doesn't seem to work for me.

Here's a jsfiddle: http://jsfiddle.net/Erugp/

Brooklyn Nicholson
  • 646
  • 10
  • 25

3 Answers3

9

The solution to this is: You do not need to wrap anything, just set the left-margin of the handle to the negative value of 1/2 of its width. As described in: http://bugs.jqueryui.com/ticket/3893

Khalid D.
  • 99
  • 1
  • 2
3

Fix it using javascript:

var $Slide = jQuery('#StatusSlide');
$Slide.slider({
  slide: function(event, ui) {
    /* Fix handler to be inside of slider borders */
    var $Handle = $Slide.find('.ui-slider-handle');
    $Handle.css('margin-left', -1 * $Handle.width() * ($Slide.slider('value') / $Slide.slider('option', 'max')));
  }
});
/* Fix handler to be inside of slider borders */
var $Handle = $Slide.find('.ui-slider-handle');
$Handle.css('margin-left', -1 * $Handle.width() * ($Slide.slider('value') / $Slide.slider('option', 'max')));

And here is your fiddle fixed: http://jsfiddle.net/he04hqg3/

brubla
  • 61
  • 2
3

The issue is that jquery UI slider uses the left CSS property with relative values to animate the slider. This means that it will slide the left edge of the handle from the left edge of the slider all the way to the other side's edge, overflowing. One solution is to make the slider div smaller and wrap it with another div which holds the styling. The outer div width = slider width + handle div.

After a little fiddling with the css sizes and margins it worked for me.

Mihai Oprea
  • 2,051
  • 3
  • 21
  • 39
  • The only thing that I noticed is that if you click on the rightmost side of the slider, it won't slide over automatically. This is because you are clicking on the wrapper, not on the slider div. But it might not be that much of an impact, you can still slide over to that side. – Mihai Oprea Dec 18 '11 at 11:30
  • On top of what Mihai said, I added padding left to the wrapping div and that moved the handle to the center. – user1786317 Dec 13 '12 at 22:27