$(function() {
function topToPerc(t, p, round) {
var result = 0;
if (round == undefined) {
result = parseFloat(((t / p) * 100).toFixed(2));
} else {
result = Math.round((t / p) * 100);
}
return result;
}
$(".box").each(function(i, el) {
if (i != 2) {
$("p", el).html(topToPerc($(el).position().top, $(".page").height()) + "%");
} else {
$("p", el).html(topToPerc($(el).position().top, $(".page").height(), true) + "%");
}
});
$(".box").draggable({
axis: "y",
containment: [0, 0, 350, 200],
drag: function(e, ui) {
if ($(this).index() != 2) {
$("p", this).html(topToPerc(ui.position.top, $(".page").height()) + "%");
} else {
$("p", this).html(topToPerc(ui.position.top, $(".page").height(), true) + "%");
}
}
});
});
body,
.page {
margin: 0;
padding: 0;
}
.page {
position: relative;
height: 200px;
}
.box {
width: 150px;
height: 2em;
position: absolute;
}
.box p {
margin: 3px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<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>
<div class="page">
<div class="box ui-widget-content" style="top: 10px; left: 8px; background: #fcc;">
<p></p>
</div>
<div class="box ui-widget-content" style="top: 175px; left: 160px; background: #cfc;">
<p></p>
</div>
<div class="box ui-widget-content" style="top: 200px; left: 312px; background: #ccf;">
<p></p>
</div>
</div>