$(function() {
function calcPerc(el, vp) {
el = $(el), vp = $(vp);
var y1 = el.position().top;
var h = el.height();
return parseFloat(((y1 / h) * 100).toFixed(2));
}
function showData(t, p) {
$(".top").html(t);
$(".perc").html(p);
}
$(".item").draggable({
scroll: false,
axis: "y",
cursor: "move",
containment: [0, -899, 0, 1],
drag: function(e, ui) {
showData(ui.position.top, calcPerc(this, $(".viewport")));
}
});
$(".perc").click(function() {
var offsetVal = parseFloat($(this).text()) * 10;
console.log(offsetVal);
$(".item").css({
top: offsetVal + "%"
});
});
$(".buttons button").click(function() {
$(".item").css("top", $(this).html());
showData($(".item").position().top, calcPerc($(".item"), $(".viewport")));
});
});
.viewport {
position: absolute;
border: 1px dashed #222;
top: 0;
}
.small {
width: 200px;
height: 100px;
}
.tall {
width: 201px;
height: 1000px;
}
.item {
background-color: #ccf;
position: absolute;
top: -299px;
}
.item span {
padding-bottom: 80px;
display: block;
text-align: center;
border-bottom: 1px solid #222;
}
.show {
position: absolute;
border: 1px solid #000;
border-radius: 3px;
width: 3em;
padding: 3px;
}
.show.buttons {
width: 100px;
}
.top {
top: 20px;
left: 220px;
}
.perc {
top: 50px;
left: 220px;
}
.buttons {
top: 80px;
left: 220px;
}
.buttons button {
width: 100%;
}
.ghost {
width: 201px;
background-color: #fff;
opacity: 0.75;
height: 900px;
position: absolute;
}
.after {
top: 101px;
}
<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="container">
<div class="small viewport">
<div class="tall item">
<span>0</span>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
</div>
</div>
<div class="ghost after"></div>
<div class="show top">-299</div>
<div class="show perc">-29.9</div>
<div class="show buttons">
<button>0%</button>
<button>-25%</button>
<button>-500%</button>
<button>-75%</button>
<button>-900%</button>
</div>
</div>