jQuery/JavaScript collision detection

前端 未结 7 2117
情话喂你
情话喂你 2020-11-22 04:49

How to detect if two

elements have collided?

The two divs are simple coloured boxes travelling perpendicular to each other, so no complicate

相关标签:
7条回答
  • 2020-11-22 05:38

    You can do this using getBoundingClientRect()

    function isOverlapping(div1, div2){
        const div1 = div1.getBoundingClientRect();
        const div2 = div2.getBoundingClientRect();
        return (div1.right > div2.left && 
                div1.left < div2.right && 
                div1.bottom > div2.top && 
                div1.top < div2.bottom)
    }
    
    0 讨论(0)
提交回复
热议问题