I've been attempting to create range sliders with boxes that display the slider's current value, but I'm having trouble getting them positioned correctly when the window size changes. Despite trying various solutions found online, I resorted to manually moving a box alongside the slider thumb. However, as my window shrinks or expands, the boxes don't stay in place. Does anyone have any suggestions on how I can keep them positioned correctly, regardless of the window size?
Below is the code I'm working with (Note: this is just for testing purposes and not a finalized version):
<html>
<head>
<title>Home</title>
<!-- CSS -->
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div id="login-box">
<div id="logo"><img border="0" src="logo.png"></div>
<div id="text_boost">Boost your account now!</div>
<div id="slider1"><input id="range1" type="range" min="10" max="100" value="0" step="1" onchange="change(1)"></div>
<div id="slider2"><input id="range2" type="range" min="0.5" max="4" value="0" step="0.5" onchange="change(2)"/></div>
</div>
<div id="sliderInfo1" class= "ui-widget-content" style="background-color: #d9d9d9; border-radius:10px; width: 59px;
height: 22px; text-align:center; display: table">
<span style="line-height: 22px"></span>
<div style="display: table-cell; vertical-align: middle; text-align: center; font-size:12px;"><span id="slider_1">10.000</span></div>
</div>
<div id="sliderInfo2" class= "ui-widget-content" style="background-color: #d9d9d9; border-radius:10px; width: 59px;
height: 22px; text-align:center; display: table">
<span style="line-height: 22px"></span>
<div style="display: table-cell; vertical-align: middle; text-align: center; font-size:12px;"><span id="slider_2">500</span></div>
</div>
<script type="text/javascript">
var newPoint, newPlace, offset;
var savedOffset_1 = document.getElementById("sliderInfo1").getBoundingClientRect().top -
document.getElementById("range1").getBoundingClientRect().top;
change(1);
var savedOffset_2 = document.getElementById("sliderInfo2").getBoundingClientRect().top -
document.getElementById("range2").getBoundingClientRect().top;
change(2);
function change (id) {
var el = $("#range" + id);
var top = el.position().top;
var left = el.position().left;
var bodyRect = document.getElementById("range" + id).getBoundingClientRect(),
elemRect = document.getElementById("sliderInfo" + id).getBoundingClientRect(),
offset_2 = elemRect.left - bodyRect.left;
if(id == 1) {
offset_1 = savedOffset_1;
} else if(id == 2) {
offset_1 = savedOffset_2;
}
// Measure width of range input
var width = 430;
// Determine placement percentage between left and right of input
newPoint = (el.val() - el.attr("min")) / (el.attr("max") - el.attr("min"));
offset = -1;
// Calculate new box position
if (newPoint < 0) {
newPlace = 0;
} else if (newPoint > 1) {
newPlace = (bodyRect.left - width);
} else {
newPlace = width * newPoint + left + width + (59 / 2);
offset -= newPoint;
}
// Move box
$("#sliderInfo" + id).css({ position: "fixed", left: newPlace, marginLeft: offset + "%", top: top - offset_1 - 5 + "px",
width: 59 + "px", height: 22 + "px", display: "table", fontSize: 12 + "px",
backgroundColor: "#d9d9d9", borderRadius: 10 + "px", verticalAlign: "middle", textAlign: "center",
lineHeight: 22 + "px"})
.text(numberWithCommas(el.val() * 1000));
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
</script>
</body>
My CSS: http://pastebin.com/rASpFWjN