0

我有一个函数 isOverlap 告诉两个对象是否重叠,这里是源代码:

    function isOverlap(idOne,idTwo){
    var objOne=$("#"+idOne),
        objTwo=$("#"+idTwo),
        offsetOne = objOne.offset(),
        offsetTwo = objTwo.offset(),
        topOne=offsetOne.top,
        topTwo=offsetTwo.top,
        leftOne=offsetOne.left,
        leftTwo=offsetTwo.left,
        widthOne = objOne.width(),
        widthTwo = objTwo.width(),
        heightOne = objOne.height(),
        heightTwo = objTwo.height();
    var leftTop = leftTwo > leftOne && leftTwo < leftOne+widthOne 
            && topTwo > topOne && topTwo < topOne+heightOne,
        rightTop = leftTwo+widthTwo > leftOne && leftTwo+widthTwo < leftOne+widthOne 
            && topTwo > topOne && topTwo < topOne+heightOne,
        leftBottom = leftTwo > leftOne && leftTwo < leftOne+widthOne 
            && topTwo+heightTwo > topOne && topTwo+heightTwo < topOne+heightOne,
        rightBottom = leftTwo+widthTwo > leftOne && leftTwo+widthTwo < leftOne+widthOne 
            && topTwo+heightTwo > topOne && topTwo+heightTwo < topOne+heightOne;
    return leftTop || rightTop || leftBottom || rightBottom;
}

我需要的主要是如何格式化调用函数,有什么帮助吗?这是我这样做的失败尝试:

   if($(document).isOverlap("#mario", ".block")) {
       $(".block").hide("explode", { pieces: 16 }, 100);
   });

你可以在这里找到我正在尝试制作的程序

4

2 回答 2

2
isOverlap("#mario", ".block")

您传递给此函数的字符串是完整的 jQuery 选择器。

因此,"#"+idOne函数内部变成"##mario",这显然是错误的。

此外,如果这些选择器中的任何一个匹配多个元素,您的代码将无法正常工作。

于 2013-03-01T04:16:29.307 回答
0

我发现问题在于,)当我尝试将.click线路更改为.isOverlap

于 2013-03-04T17:27:35.367 回答