1

好的,我有这样的事情:

<div id='father' style='width: 100%; height: 100px; position:absolute; top: 0px; left: 0px;overflow:hidden;'>
    <div id='box' style='width: 50px; height: 50px;'></div>
</div>
<script>
function FrandomBoxPos(elem){
    cuadrante = rand(1,4);
    if (cuadrante == 1){
        elem.style.top = "-"+rand(100, 200)+"px";
        elem.style.left = rand(1, document.body.offsetWidth)+"px";
    } else if (cuadrante == 2){
        elem.style.top = rand(1, document.body.offsetHeight)+"px";
        elem.style.left = rand(-300, (document.body.offsetWidth*-1)-300)+"px";
    } else if (cuadrante == 3){
        elem.style.top = rand(document.body.offsetHeight, document.getElementById('DcontenidoSubmenu').offsetHeight+100)+"px";
        elem.style.left = rand(1, document.body.offsetWidth)+"px";
    } else if (cuadrante == 4){
        elem.style.top = rand(1, document.body.offsetHeight)+"px";
        elem.style.left = rand(document.body.offsetWidth+400, document.body.offsetWidth+600)+"px";
    }
}
FrandomBoxPos(document.getElementById('box'));
</script>

有时,在我运行 FrandomBoxPos 之后,我可以看到他父亲的“盒子”(我不应该)。因此,如果我可以检查“盒子”是否不在“父亲”中,我可以制作如下内容:

while isOutOf('box', 'father') == false:
    FrandomBoxPos(document.getElementById('box'));

这是代码:http : //pastebin.com/bnnWLwdY 我不能粘贴它 jsFiddle 因为在那里不起作用。当您将鼠标放在任何“部分”按钮上并且 BOX 没有移动并且不在适当的位置(进入红色边框)时,就是出现问题了......

4

1 回答 1

1
if((child.posX > dad.posX && child.posX < dad.posX + dad.width) || (child.posX + child.width > dad.posX && child.posX + child.width < dad.posX + dad.width) ) {
   //it is in horizontal boundaries
     if((child.posY > dad.posY && child.posY < dad.posY + dad.width) || (child.posY + child.width > dad.posY && child.posY + child.width < dad.posY + dad.width) ) {
   //child overlaps parent
    }
}

我认为这应该工作..

于 2013-10-01T15:41:21.890 回答