可能重复:
How to detect if two divs touch with jquery?检测两个div是否重叠
我花了很多时间试图找出如何检测,如果两个div重叠。
我试过gamequery插件,并用它这样的:
$("#" + checkform).collision("#" + checkform + "w").each(function(){
alert("Collision");
});
你可以使用gamequery插件和/或你会怎么做呢?
可能重复:
How to detect if two divs touch with jquery?检测两个div是否重叠
我花了很多时间试图找出如何检测,如果两个div重叠。
我试过gamequery插件,并用它这样的:
$("#" + checkform).collision("#" + checkform + "w").each(function(){
alert("Collision");
});
你可以使用gamequery插件和/或你会怎么做呢?
已经有这里有一个答案查询:How to detect if two divs touch with jquery?
仅供参考我在这里复制的代码:
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;
}
每个元素上使用getBoundingClientRect()
,看看它们重叠的方式。不需要jQuery。
我希望你把这个问题标记为另一个的重复。 –
是的,所以.... – Amar