js如何检测两个对象是否碰撞,方法如下,
1 function isBump(obj1,obj2){ //碰撞检测 2 var L1 = obj1.offsetLeft; 3 var R1 = obj1.offsetLeft + obj1.offsetWidth; 4 var T1 = obj1.offsetTop; 5 var B1 = obj1.offsetTop + obj1.offsetHeight; 6 var L2 = obj2.offsetLeft+obj2.parentNode.offsetLeft; 7 var R2 = obj2.offsetLeft + obj2.offsetWidth + obj2.parentNode.offsetLeft; 8 var T2 = obj2.offsetTop + obj2.parentNode.offsetTop;; 9 var B2 = obj2.offsetTop + obj2.offsetHeight + obj2.parentNode.offsetTop; 10 if( R1<L2 || L1>R2 || T1>B2 || B1<T2 ){ 11 return false; 12 } 13 else{ 14 return true; 15 } 16 }
这个方法就是检测obj1是否与obj2碰撞了,若是真就是返回true。