我正在开发一个简单的气球游戏,使用了两个
元素。问题在于当这两个
元素相互接触时,我无法触发一个函数。
我正在开发一个简单的气球游戏,使用了两个
function collision($div1, $div2) {
var x1 = $div1.offset().left;
var y1 = $div1.offset().top;
var h1 = $div1.outerHeight(true);
var w1 = $div1.outerWidth(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $div2.offset().left;
var y2 = $div2.offset().top;
var h2 = $div2.outerHeight(true);
var w2 = $div2.outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;
if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;
return true;
}
第一个允许这样做:
var hit_list = $("#collider").collision(".obstacle");
这是所有与“#collider”重叠的“.obstacle”列表。
第二个允许:
$("#collider").draggable( { obstacle: ".obstacle" } );
其中之一是提供了“collision”事件可以绑定:
$("#collider").bind( "collision", function(event,ui){...} );
而且你甚至可以设置:
$("#collider").draggable( { obstacle: ".obstacle", preventCollision: true } );
在拖动时,防止“#collider”与任何“.obstacle”重叠。
在原生的Javascript中
var is_colliding = function(div1, div2) {
var d1_height = div1.offsetHeight;
var d1_width = div1.offsetWidth;
var d1_distance_from_top = div1.offsetTop + d1_height;
var d1_distance_from_left = div1.offsetLeft + d1_width;
var d2_height = div2.offsetHeight;
var d2_width = div2.offsetWidth;
var d2_distance_from_top = div2.offsetTop + d2_height;
var d2_distance_from_left = div2.offsetLeft + d2_width;
var not_colliding =
d1_distance_from_top < div2.offsetTop ||
div1.offsetTop > d2_distance_from_top ||
d1_distance_from_left < div2.offsetTop ||
div1.offsetLeft > d2_distance_from_left;
return !not_colliding;
};
Victor S的回答很有用,但我需要检查两个div是否完全重叠,即被遮挡(但这很难在谷歌上搜索,因为结果中充斥着对Eclipse IDE的引用)。
分享我修改后的版本,以防有人觉得有用:
$(function() {
/**
* Function to determine whether the first element completely
* eclipses the second element
*/
function collision($div2, $div1) {
var x1 = $div1.offset().left;
var y1 = $div1.offset().top;
var h1 = $div1.outerHeight(true);
var w1 = $div1.outerWidth(true);
var b1 = y1 + h1;
var r1 = x1 + w1;
var x2 = $div2.offset().left;
var y2 = $div2.offset().top;
var h2 = $div2.outerHeight(true);
var w2 = $div2.outerWidth(true);
var b2 = y2 + h2;
var r2 = x2 + w2;
return x1 > x2 && y1 > y2 && b1 < b2 && r1 < r2
? true
: false
}
// draggable jQuery listener required jQuery UI
// Just used to illustrate the above
$('#div1,#div2').draggable({
drag: function() {
if (collision($('#div1'), $('#div2'))) {
$('#div2').text('Eclipsing').css({'background-color': 'grey'})
} else {
$('#div2').text('Not eclipsing').css({'background-color': 'green'})
}
$('#result').text(collision($('#div1'), $('#div2')));
}
});
});
.box {
display: table-cell;
vertical-align: middle;
text-align: center;
}
#div1 {
width: 200px;
height: 200px;
background-color: pink;
}
#div2 {
width: 75px;
height: 75px;
background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<strong>Drag divs around.</strong>
<p>Eclipsing? <span id="result">false</span>
<div id="wrapper">
<div id="div1" class="box">
Div1
</div><br/>
<div id="div2" class="box">
Not eclipsing
</div>
</div>