I didn't know how to formulate the question, so suggestions about the name of the question is advised :D.
So basically I have a grid(the main droppable container), consisting from elements 20x20px and in order to snap the draggable in the conrainer according to this 20x20 elements I need to use the SnapTolerance to correct their positioning inside the grid.
for(var i=1; i<=15; i++){
$('#box-' + i).draggable({
revert: "invalid",
stop: function(){
$(this).draggable('option', 'revert', 'invalid');
},
drag: function( event, ui ) {
var snapTolerance = $(this).draggable('option', 'snapTolerance');
var topRemainder = ui.position.top % 20;
var leftRemainder =ui.position.left % 20;
if (topRemainder <= snapTolerance) {
ui.position.top = ui.position.top - topRemainder;
}
if (leftRemainder <= snapTolerance) {
ui.position.left = ui.position.left - leftRemainder;
}
}
});
};
This works just fine, but on the other hand I don't want the draggables to overlap, so I use them also as droppable with a variable set to invalid, if they overlap.
$('#box-' + i).droppable({
greedy: true,
tolerance: 'touch',
drop: function(event,ui){
ui.draggable.draggable('option','revert',true);
}
})
So far so good, it all works fine for me, but sometimes the SnapTolerance is playing a joke with me and allows something like this.
Which I of course don't want to happen. And till we're still on this subject, I also don't know how to avoid another nasty "bug". When a draggable is dragged to the edge of the container it doesn't isntantly take position. Sometimes it returns (as invalid) up to 10 times, and I have to be really insistent about it.
Here is the jsfiddle for the whole thing.
http://jsfiddle.net/rb4qeq8k/1/
You can play around with it for a little while to see where it "bugs".
Thanks in advance.