$(function() {
function makeDrops(n) {
var t = $(".ui-widget").eq(1);
for (var i = 1; i <= n; i++) {
$("<div>", {
id: "drop-" + i,
class: "will-be-drop ui-widget-content"
}).appendTo(t);
}
}
function inViewport(element, detectPartial) {
element = $(element);
detectPartial = (!!detectPartial); // if null or undefined, default to false
var viewport = $(window),
vpWidth = viewport.width(),
vpHeight = viewport.height(),
vpTop = viewport.scrollTop(),
vpBottom = vpTop + vpHeight,
vpLeft = viewport.scrollLeft(),
vpRight = vpLeft + vpWidth,
elementOffset = element.offset(),
elementTopArea = elementOffset.top + ((detectPartial) ? element.height() : 0),
elementBottomArea = elementOffset.top + ((detectPartial) ? 0 : element.height()),
elementLeftArea = elementOffset.left + ((detectPartial) ? element.width() : 0),
elementRightArea = elementOffset.left + ((detectPartial) ? 0 : element.width());
return ((elementBottomArea <= vpBottom) && (elementTopArea >= vpTop)) && ((elementRightArea <= vpRight) && (elementLeftArea >= vpLeft));
}
function markVisible(c) {
c.each(function(i, el) {
if (inViewport(el, true)) {
$(el).addClass("visible");
}
});
}
makeDrops(3000);
$(".will-be-drop").droppable({
drop: function(event, ui) {
let item = ui.draggable;
console.log("Drag Item " + item.text().trim() + " dropped to " + $(this).attr("id"));
item.detach().appendTo($(this));
},
over: function() {
$(this).addClass("ui-state-highlight");
},
out: function() {
$(this).removeClass("ui-state-highlight");
}
}).droppable("disable");
$('.will-be-drag').draggable({
helper: 'clone',
start: function(e, ui) {
markVisible($(".will-be-drop"));
$(".will-be-drop.visible").droppable("enable");
},
drag: function(e, ui) {
$(".will-be-drop.visible").droppable("disable").removeClass("visible");
markVisible($(".will-be-drop"));
$(".will-be-drop.visible").droppable("enable");
},
stop: function(e, ui) {
$(".will-be-drop").droppable("disable");
$(".will-be-drop.ui-state-highlight").removeClass("ui-state-highlight");
}
});
});
.will-be-drag {
width: 50px;
height: 50px;
padding: 0.25em;
float: left;
margin: 10px 10px 10px 0;
}
.will-be-drop {
width: 100px;
height: 100px;
padding: 0.25em;
float: left;
margin: 10px;
}
<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="ui-widget">
<div class="will-be-drag ui-widget-content">A</div>
<div class="will-be-drag ui-widget-content">B</div>
<div class="will-be-drag ui-widget-content">C</div>
</div>
<div class="ui-widget">
</div>