DRAG - BLOCK & REVERT - #1

by bizamajig

HTML

<div>
    <div class="drop">drop here</div>
</div>
<div>
    <div class="drag">drag me</div>
</div>
<div class="overlay">i want to block the droppable</div>

CSS

.drag {
    display: inline-block;
    width: 100px;
    height: 100px;
    border: 1px solid black;
    background-color: lightblue;
    z-index: 1;
}
.drop {
    display: inline-block;
    border: 1px dotted black;
    width: 200px;
    height: 200px;
}
.drop-hover {
    background-color: grey;
}
.overlay {
    position: absolute;
    width: 300px;
    height: 100px;
    top: 50px;
    background-color: red;
    border: 1px solid black;
}

JavaScript

$(function () {
    $('.drag').draggable();
    $('.drop, .overlay').droppable({
        tolerance: 'touch',
        hoverClass: 'drop-hover',
        accept: '.drag',
        drop: function (event, ui) {
            if ($(this).hasClass('overlay')) {
                ui.draggable.draggable({
                    revert: true
                });
            } else {
                ui.draggable.draggable({
                    revert: "invalid"
                });
            }
        }
    });
});