Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If element is over another element?

Tags:

jquery

With jQuery how do i find out if div one is over div two? Not, which z-index is higher but what div is visually over the other div.

<style type='text/css'>
    #one {
    position:absolute; top:0; left:0; width:100px; height:100px; background-color:red; z-index:2;
    }
    #two {
    position:absolute; top:0; left:0; width:100px; height:100px; background-color:green; z-index:1;
    }
    </style>    
    <div id='one'></div>
        <div id='two'></div>
like image 252
user892134 Avatar asked Jan 15 '12 19:01

user892134


People also ask

How do you know if two elements overlap?

To check if two elements overlap, use the getBoundingClientRect() method to get an object containing information about the relative position to the viewport of the elements. Then, compare the boundary edges (top, right, bottom, left) of the two rectangles. Here is the HTML for the examples in this article. Copied!

How do you place an element on top of another element?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

How do you check if an element contains another element?

contains() method checks if an element is inside another, and returns a boolean: true if it is, and false if it's not. Call it on the parent element, and pass the element you want to check for in as an argument. // returns true main.

How do I show a DIV on top of another?

Use CSS position: absolute; followed by top: 0px; left 0px; in the style attribute of each DIV. Replace the pixel values with whatever you want. You can use z-index: x; to set the vertical "order" (which one is "on top").


1 Answers

You can use the offset method (here) to get the space from the document edges. Add the width and height of the elements, and substract the numbers.

Offset and width for div's

like image 131
Lg102 Avatar answered Oct 09 '22 17:10

Lg102