我写的是非常非常基础的根据其中一个 obj 页面元素的上下左右,与另一个 obj 页面元素的上下左右比较之后判断是否碰撞,具体代码如下。觉得这么写不是特别的高效,判断太多了,有没有好一点的方式?
function getRect(obj){
return obj.getBoundingClientRect();
}
function crash(obj1,obj2){
var first_Rect = getRect(obj1);
var second_rect = getRect(obj2);
var firstLeft = getRect(obj1).left;
var firstTop = getRect(obj1).top;
var firstRight = getRect(obj1).right;
var firstBottom = getRect(obj1).bottom;
var secondLeft = getRect(obj2).left;
var secondTop = getRect(obj2).top;
var secondRight = getRect(obj2).right;
var secondBottom = getRect(obj2).bottom;
if(firstLeft > secondRight||firstRight < secondLeft||firstTop > secondBottom||firstBottom < secondTop){
return false;
}else{
return true;
}
}