I have a UISlider I implemented with JQuery UI
. I want to add a legend under the slider showing the numbers. I followed this answer which showed how to implement that, placing then numbers with percentage values. This way when the sliders size changes, (ex. browser gets resized,) the numbers are still at the correct places.
That works fine and well, but if the browsers window is small, or if there are a lot of numbers for the slider, then the legend number get too close to each other.
Question:
How can I delete a number if they are 'x' amount too close.
Here is what I tried doing:
if (i !== 0) {
var prevLegendNum_offsetLeft = $('#legendNum' + i).closest('.sliderLegend').find('.sliderLegendNum').offset().left
var distance = $('#legendNum' + i).offset().left - prevLegendNum_offsetLeft;
console.log(distance); // Logs: 0
if (distance <= 35) {
$('#legendNum' + i).remove();
}
}
The reason why I had to do the whole thing with prevLegendNum_offsetLeft
, and not just $('#legendNum' + (i - 1))
is because, if there are 3 numbers that are less than the distance of 35 away from each other, so 2 numbers behind i
will be deleted. It will then try to access $('#legendNum' + (i - 1)
, but that won't be there, so it will return an error.
When I run the code above, it deletes all numbers besides for the first one.
JSFiddle
$("#slider").slider({
value: 4,
min: 1,
max: 15,
step: 1
})
.each(function() {
var opt = $(this).data().uiSlider.options;
var vals = opt.max - opt.min;
for (var i = 0; i <= vals; i++) {
var el = $('<div class="sliderLegend"><div class="sliderLegendMark">|</div><div class="sliderLegendNum" id="legendNum' + i + '">' + (i + 2) + '</div></div>').css('left', (i / vals * 100) + '%');
$('#slider').append(el);
if (i !== 0) {
var prevLegendNum_offsetLeft = $('#legendNum' + i).closest('.sliderLegend').find('.sliderLegendNum').offset().left
var distance = $('#legendNum' + i).offset().left - prevLegendNum_offsetLeft;
console.log(distance);
if (distance <= 35) {
$('#legendNum' + i).remove();
}
}
}
});
#slider > div {
position: absolute;
width: 20px;
margin-left: -10px;
text-align: center;
margin-top: 20px;
}
/* below is not necessary, just for style */
#slider {
width: 50%;
margin: 2em auto;
}
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<div id="slider"></div>