javascript - jQuery/JavaScript 碰撞检测

标签 javascript jquery html collision-detection

如何检测两个 <div>元素发生了碰撞?

这两个 div 是相互垂直的简单彩色框,因此没有复杂的形状或 Angular 。

最佳答案

var overlaps = (function () {
    function getPositions( elem ) {
        var pos, width, height;
        pos = $( elem ).position();
        width = $( elem ).width();
        height = $( elem ).height();
        return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
    }

    function comparePositions( p1, p2 ) {
        var r1, r2;
        r1 = p1[0] < p2[0] ? p1 : p2;
        r2 = p1[0] < p2[0] ? p2 : p1;
        return r1[1] > r2[0] || r1[0] === r2[0];
    }

    return function ( a, b ) {
        var pos1 = getPositions( a ),
            pos2 = getPositions( b );
        return comparePositions( pos1[0], pos2[0] ) && comparePositions( pos1[1], pos2[1] );
    };
})();

$(function () {
    var area = $( '#area' )[0],
        box = $( '#box0' )[0],
        html;
    
    html = $( area ).children().not( box ).map( function ( i ) {
        return '<p>Red box + Box ' + ( i + 1 ) + ' = ' + overlaps( box, this ) + '</p>';
    }).get().join( '' );

    $( 'body' ).append( html );
});
body {
    padding: 30px;
    color: #444;
    font-family: Arial, sans-serif;
}

h1 {
    font-size: 24px;
    margin-bottom: 20px;
}

#area {
    border: 2px solid gray;
    width: 500px;
    height: 400px;
    position: relative;
}

#area > div {
    background-color: rgba(122, 122, 122, 0.3);
    position: absolute;
    text-align: center;
    font-size: 50px;
    width: 60px;
    height: 60px;
}

#box0 {
    background-color: rgba(255, 0, 0, 0.5) !important;
    top: 150px;
    left: 150px;
}

#box1 {
    top: 260px;
    left: 50px;
}

#box2 {
    top: 110px;
    left: 160px;
}

#box3 {
    top: 200px;
    left: 200px;
}

#box4 {
    top: 50px;
    left: 400px;
}

p {
    margin: 5px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<h1>Detect overlapping with JavaScript</h1>
<div id="area">
    <div id="box0"></div>
    <div id="box1">1</div>
    <div id="box2">2</div>
    <div id="box3">3</div>
    <div id="box4">4</div>
</div>

总体思路 - 您获取框的偏移量和尺寸并检查它们是否重叠。

如果你想让它更新,你可以使用setInterval:

function detectOverlapping() {
    // code that detects if the box overlaps with a moving box
    setInterval(detectOverlapping, 25);
}

detectOverlapping();  

另请注意,您可以针对您的特定示例优化函数。

  • 您不必重复阅读框尺寸(就像我在代码中所做的那样),因为它们是固定的。您可以在页面加载时读取它们(放入变量中),然后只读取变量

  • 小框的水平位置不会改变(除非用户调整窗口大小)。车箱的垂直位置没有改变。因此,这些值也不必重复读取,还可以存储到变量中。

  • 您不必一直测试小盒子是否与所有汽车盒子重叠。您可以 - 基于其垂直位置 - 找出盒子当前在哪条车道上,并仅测试该车道上的特定汽车盒子。

关于javascript - jQuery/JavaScript 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4230029/

相关文章:

javascript - Firebase - Vue 数据库集成不起作用

jquery - Vimeo 全屏不适用于 jquery colorbox

html - Bootstrap 两列布局

javascript - 如何在 JavaScript 中添加点击事件上的圆圈?

javascript - 使用 Arc 的 HTML5 Canvas Clip 剪辑错误

来自url查询字符串中变量的javascript表单字段填充

javascript - 从相机胶卷 React Native 解码 QR 码图像

javascript - 在 javascript 中模拟点击(也在窗口外......)

javascript - 使用 javascript/jquery 进行元动态

jquery - 使用 ASP.NET MVC 和 AJAX 的分页表