Вы хотите использовать position
или offset
из объекта пользовательского интерфейса.
Примите во внимание следующее:
$(function() {
var draggableRight;
var draggableWidth = $('.draggable').height();
var parentWidth = $('#parent').height();
$('.draggable').draggable({
containment: 'parent',
axis: "y",
drag: function(e, ui) {
var p = {
top: ui.offset.top - $("#parent").offset().top,
bottom: (ui.offset.top - $("#parent").offset().top) + $(this).height()
};
$("#log").html(p.top + ", " + p.bottom);
if (p.top == 0) {
console.log("Hit Top");
}
if (p.bottom == $("#parent").height()) {
console.log("Hit Bottom");
}
}
});
});
#parent {
background: khaki;
width: 400px;
height: 200px;
position: relative;
margin: 20px;
padding: 0px;
}
.draggable {
background: #fff;
width: 400px;
height: 50px;
margin: 0px;
background: red;
}
<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 id="parent">
<div class="draggable"></div>
</div>
<div id="log"></div>