drag elements in container should not be overlap how can we restrict. please help
Asked
Active
Viewed 3,018 times
1 Answers
0
Sorry that this was not answered sooner. I believe you have to manually check the position of the elements' top, bottom, left, and right edges, so here is what I did:
//Call this function from within your .on('dragmove') method.
//It should replace your translations.
function noOverlap(event, overlapElements){
var dx = event.dx;
var dy = event.dy;
//just for flagging when the target would overlap another element
var overlap = false;
var targetDims = event.target.getBoundingClientRect();
for(i = 0; i < overlapElements.length; i++){
var overlapElementDims =
overlapElements[i].getBoundingClientRect();
//make sure the element doesn't look at itself..
if(overlapElements[i] != event.target){
//checks if the target "doesn't" overlap
if(((targetDims.right + dx) < (overlapElementDims.left + 1))
||((targetDims.left + 1 + dx) > (overlapElementDims.right))
||((targetDims.top + 1 + dy) > (overlapElementDims.bottom))
||((targetDims.bottom + dy) < (overlapElementDims.top + 1))){
//Basically, the target element doesn't overlap the current
//element in the HTMLCollection, do nothing and go to the
//next iterate
}
else{
//This is if the target element would overlap the current element
//set overlap to true and break out of the for loop to conserve time.
overlap = true;
break;
}
}
};
if(overlap === false){
//if there's no overlap, do your normal stuff, like:
event.target.x += dx;
event.target.y += dy;
event.target.style.webkitTransform =
event.target.style.transform =
'translate(' + event.target.x + 'px, ' + event.target.y + 'px)';
//then reset dx and dy
dy = 0;
dx = 0;
}
else{
if(event.interaction.pointers[event.interaction.pointers.length - 1].type
=== "pointerup"){
//check if the target "is" in the restriction zone
var restriction =
interact(event.target).options.drag.restrict.restriction;
var restrictionDims = restriction.getBoundingClientRect();
if((targetDims.right > restrictionDims.right) ||
(targetDims.left < restrictionDims.left) ||
(targetDims.bottom > restrictionDims.bottom) ||
(targetDims.top < restrictionDims.top)){
event.target.style.webkitTransform =
event.target.style.transform =
'translate(0px, 0px)';
//then reset dx and dy
dy = 0;
dx = 0;
//then reset x and y
event.target.x = 0;
event.target.y = 0;
}
}
}
}

kaarto
- 747
- 1
- 8
- 18

KirklandBrown373
- 85
- 8
-
Also, if anyone ends up reading this, please let me know what all I could do to improve the efficiency. – KirklandBrown373 Dec 18 '16 at 18:46
-
I have stopped dragging by mot updating the positions of the dragged element, but any idea how I can stop the mouse pointer from moving in that case? – not2savvy May 02 '17 at 15:13
-
stopping the mouse pointer from moving is a completely different subject from interact js. I would look at something like this: http://www.smartjava.org/content/html5-use-pointer-lock-api-restrict-mouse-movement-element – KirklandBrown373 May 03 '17 at 18:03
-
1Kirkland hi. Has this been folded into interact.js? It seems a v useful option and this is a bit too clever for me. I would much rather have a snap to non overlap. Seen "bricks" or something do that. Just wondering :-) – BeNice Feb 04 '20 at 22:14