find elements that are stacked under (visually) an element in jquery

前端 未结 4 2073
小鲜肉
小鲜肉 2020-11-29 09:37

if i have 2 divs (z index is not assigned), one layered over the over, can i use the reference to the top div to find which div is below it?

as far as the DOM struct

4条回答
  •  死守一世寂寞
    2020-11-29 10:14

    Maybe something along these lines... detach the element under your cursor (in this case box1) and use document.elementFromPoint to fetch the next element underneath that x,y position.. rough example:

        var first_box = $('#box1');// more programmaticaly would be to use document.elementFromPoint to fetch the element under your cursor
    
        var first_box_offset = first_box.offset();
        var x = first_box_offset.left;
        var y= first_box_offset.top + first_box.height();
    
        var reattach_me = first_box.detach();
        var second_box = document.elementFromPoint(x, y);
    
        $('body').append(reattach_me);
    

提交回复
热议问题