Here is a perfect example of free movement while snapping to a grid or other element. Hope it helps!
https://jqueryui.com/draggable/#snap-to
As you can see from the source the drag gable element can move freely, but we also give it the option to snap to certain elements using 'snap'.
$( function() {
$( "#draggable" ).draggable({ snap: true });
$( "#draggable2" ).draggable({ snap: ".ui-widget-header" });
$( "#draggable3" ).draggable({ snap: ".ui-widget-header", snapMode: "outer" });
$( "#draggable4" ).draggable({ grid: [ 20, 20 ] });
$( "#draggable5" ).draggable({ grid: [ 80, 80 ] });
} );
Also here is an example of reverting the element back to it's original position if it isn't snapped to another element.
$(function() {
$("#draggable").draggable({
revert : function(event, ui) {
// on older version of jQuery use "draggable"
// $(this).data("draggable")
// on 2.x versions of jQuery use "ui-draggable"
// $(this).data("ui-draggable")
$(this).data("uiDraggable").originalPosition = {
top : 0,
left : 0
};
// return boolean
return !event;
// that evaluate like this:
// return event !== false ? false : true;
}
});
$("#droppable").droppable();
});
Revert a jQuery draggable object back to its original container on out event of droppable