Skip to content

Commit ee21514

Browse files
committed
feat: add new funciton
1 parent af5b8b2 commit ee21514

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/collisionDetection.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function collisionDetection(o1: string | HTMLElement, o2: string | HTMLElement) {
2+
const obj1: HTMLElement = typeof o1 === 'string' ? document.querySelector(o1)! : o1
3+
const obj2: HTMLElement = typeof o2 === 'string' ? document.querySelector(o2)! : o2
4+
if (!obj1 || !obj2)
5+
return
6+
const left1_start = obj1.offsetLeft
7+
const left1_end = obj1.offsetLeft + obj1.offsetWidth
8+
const left2_start = obj2.offsetLeft
9+
const left2_end = obj2.offsetLeft + obj2.offsetWidth
10+
const top1_start = obj1.offsetTop
11+
const top1_end = obj1.offsetTop + obj1.offsetHeight
12+
const top2_start = obj2.offsetTop
13+
const top2_end = obj2.offsetTop + obj2.offsetHeight
14+
// 判断是否碰撞
15+
return !(left1_start > left2_end || left1_end < left2_start || top1_start > top2_end || top1_end < top2_start)
16+
}

0 commit comments

Comments
 (0)