# 多标签排列避免重叠算法

0

0

``````//碰撞检测
function collision(dragEl, hitEl){
let x1 = 0;
let y1 = 0;
let x2 = 0;
let y2 = 0;
let distance = [];
let dragRight = dragEl.getBoundingClientRect().right;
let hitLeft = hitEl.getBoundingClientRect().left;
let dragBottom = dragEl.getBoundingClientRect().bottom;
let hitTop = hitEl.getBoundingClientRect().top;
let dragLeft = dragEl.getBoundingClientRect().left;
let hitRight = hitEl.getBoundingClientRect().right;
let dragTop = dragEl.getBoundingClientRect().top;
let hitBottom = hitEl.getBoundingClientRect().bottom;

if(dragRight >= hitLeft){
x1 = dragRight - hitLeft;
distance.push(x1);
}

if(dragBottom >= hitTop){
y1 = dragBottom - hitTop;
distance.push(y1);
}

if(dragLeft <= hitRight){
x2 = hitRight - dragLeft;
distance.push(x2);
}

if(dragTop <= hitBottom){
y2 = hitBottom - dragTop;
distance.push(y2);
}

if(distance.length === 0){
//没碰撞
return false;
}

//判断横向移动
if(distance[0] < distance[2]){
//dragEl 向左移动 distance[0] + 1 的长度
}else{
//dragEl 向右移动 distance[2] + 1 的长度
}

//判断纵向移动
if(distance[1] < distance[3]){
//dragEl 向上移动 distance[1] + 1 的长度
}else{
//dragEl 向下移动 distance[3] + 1 的长度
}
}``````
zanetti | 园豆：128 (初学一级) | 2019-12-20 01:49

@逐影: 别客气，我看到你的问题就想了想。我还没在工作中遇到这么难的问题呢，大神们可能没把代码贡献出来吧。祝你早日攻克难关！

0

zzhenxiang | 园豆：340 (菜鸟二级) | 2019-12-23 17:22

您需要登录以后才能回答，未注册用户请先注册