重叠格式

时间:2013-02-28 17:36:24

标签: javascript jquery debugging overlap graphical-interaction

我有一个函数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);
   });

您可以找到我尝试制作的节目here

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:0)

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

时,我只是不需要.isOverlap