File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments