I need to use jQuery UI to restrict the containment area for draggable object with some additional restriction. I need prevent the draggable element from overlapping with other elements within the same container. I need to allow movement in "moveInHere" area but not "butNotHere" area. Is it possible?
<div id="moveInHere">
<div id="dragMe"> </div>
<div id="butNotHere"> </div>
</div>
<script type="text/javascript">
$("#dragMe").draggable({
containment: "#moveInHere"
});
</script>
This took me quite a bit of fiddling. Basically I created a
droppable
on the element you want to avoid, then set a boolean when the drag is over it. I then use that in an undocumented revert function override to cancel the drop. It only works if#dragMe
is fully over#butNotHere
:Check out the working demo and feel free to play with it: http://jsfiddle.net/H59Nb/31/
Edit: New Solution
I found a plugin for this called JQuery UI Draggable Collision. Using this, implementing your desired functionality becomes trivial. See the following jsFiddle example: http://jsfiddle.net/q3x8w03y/1/ (This uses version 1.0.2 of JQuery UI Draggable Collision, along with JQuery 1.7.2 and JQueryUI 1.1.18.)
Here is the code:
Old Solution
The following should work. It has a glitch, though. Once the divs collide, you have to "regrab" the div you are dragging, which can be a little annoying. Perhaps someone else will know how to fix this this. You can see my jsFiddle example here: http://jsfiddle.net/MrAdE/8/