How to detect elements overlapping (overlaying) using JavaScript?

前端 未结 3 614
别跟我提以往
别跟我提以往 2020-12-06 07:44

How to detect overlap HTML elements, using JavaScript?

I have an item list (

    ). It slides up and down using JavaScript. When it slides down, de
相关标签:
3条回答
  • 2020-12-06 08:11
    function isObjOnObj(a,b){
        var al = a.left;
        var ar = a.left+a.width;
        var bl = b.left;
        var br = b.left+b.width;
    
        var at = a.top;
        var ab = a.top+a.height;
        var bt = b.top;
        var bb = b.top+b.height;
    
        if(bl>ar || br<al){return false;}//overlap not possible
        if(bt>ab || bb<at){return false;}//overlap not possible
    
        if(bl>al && bl<ar){return true;}
        if(br>al && br<ar){return true;}
    
        if(bt>at && bt<ab){return true;}
        if(bb>at && bb<ab){return true;}
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-06 08:27

    This feels like collision detection, and there's a solution just posted. Instead, I would suggest working out how many list items would be needed for a menu to overlap (i.e. 10), and hide the div when those menus (with 10 li) are selected.

    0 讨论(0)
  • 2020-12-06 08:28

    Why not just raise the z-index of your UL to be above the div at the bottom?

    add:

    #wrap {
        z-index  : 2;
        position : relative;
    }
    
    0 讨论(0)
提交回复
热议问题