I just wrote a js file to use =) hope this helps you ... it took me a while until I finished it
You can download it from this link: solid-dotheyoverlap.js
just include this js file and call the doTheyOverlap function with two divs that you want to find out if they overlap or not, and what it is!
here is a simple and simple example i made for you:
<html> <head> <title>Test</title> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript" src="solid-dotheyoverlap.js"></script> <script type="text/javascript"> $(function() { alert(doTheyOverlap($('#div0'),$('#div1'))); }); </script> </head> <body> <div id="div0" style="position: absolute; width : 200px; height : 200px; background-color : red">div 0</div> <div id="div1" style="position: absolute; width : 200px; height : 200px; background-color : yellow; top:100px; left:100px">div 1</div> </body> </html>
and the code for solid-doTheyOverlap.js that I wrote is more clear and more efficient than the solution offered by @artwl. it looks like this:
function doTheyOverlap(div0, div1){return (yInstersection(div0, div1) && xInstersection(div0, div1));} function findSmallestY(div0, div1){ return (div0.offset().top < div1.offset().top)? div0 : div1; } function yInstersection(div0, div1){ var divY0 = findSmallestY(div0, div1); var divY1 = (div0 != divY0)? div0 : div1; return (divY0.offset().top + divY0.height()) - divY1.offset().top > 0; } function findSmallestX(div0, div1){ return (div0.offset().left < div1.offset().left)? div0 : div1; } function xInstersection(div0, div1){ var divX0 = findSmallestX(div0, div1); var divX1 = (div0 != divX0)? div0 : div1; return (divX0.offset().left + divX0.width()) - divX1.offset().left > 0; }
Put it in simple words, I realized that βfiguring out whether 2 squares overlap is as easy as figuring out if their segments (depending on the x-axis and y-axis) are excessive,β in javascript it would be like this β doTheyOverlap(div0, div1){return (yInstersection(div0, div1) && xInstersection(div0, div1));} ", from there it is as simple as a pair of permutations ( (divY0.offset().top + divY0.height()) - divY1.offset().top > 0 , (divX0.offset().left + divX0.width()) - divX1.offset().left > 0 ) to find out if these segments overlap.
source share