Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:14:09+00:00 2026-05-25T22:14:09+00:00

How do I detect if two divs are overlapping? Not taking into consideration the

  • 0

How do I detect if two divs are overlapping?

Not taking into consideration the width of a div, it is basically a vertical line segment. The (top,left) point is point A while the bottom (top + height) is point B and so forth. I’d then compare each div to each to the other divs in the divs array and then create an array of colliding divs. However, I’m stuck on how to do this programmatically.

This is my array of divs:

var divs = [
    {class:'A', top:0,   left:0,   height:'60px'},
    {class:'B', top:50,  left:60,  height:'60px'},
    {class:'C', top:30,  left:10,  height:'60px'},
    {class:'D', top:100, left:180, height:'60px'},
    {class:'E', top:80,  left:50,  height:'60px'},
    {class:'F', top:110, left:200, height:'60px'},
    {class:'G', top:55,  left:80,  height:'60px'}
];

Here’s the function I had started:

    this.collide = function( divs )
{
    var collidingDivs = [], z = events.length;

    for(i; i<z; i++)
    {
        if
        (
           // Begin pseudocode
           ( divsB.top >= divsA.top ) && 
           ( (divsB.top + divsB.height) <= (divsA.top + divsA.height) ) 
        )
        {
            collidingDivs.push(divs[i].class);
        }
    }
    console.log(collidingDivs); // Array of divs that overlap (collide)
};

I’m just utterly stuck at this point. How do I iterate over each div and check if it collides with any of the other divs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:14:09+00:00Added an answer on May 25, 2026 at 10:14 pm

    You need to loop through each div, and then compare with every other div in a nested loop. Then use logic like what you’ve written to compare each combination. Here is an example that simply prints out the overlapping divs to the output (note also that I changed the height element to have a numerical value rather than text so that its value could be used in calculations):

    var divs = [
        {class:'A', top:0,   left:0,   height:60},
        {class:'B', top:50,  left:60,  height:60},
        {class:'C', top:30,  left:10,  height:60},
        {class:'D', top:100, left:180, height:60},
        {class:'E', top:80,  left:50,  height:60},
        {class:'F', top:110, left:200, height:60},
        {class:'G', top:55,  left:80,  height:60}
    ];
    
    for (var i=0; i < divs.length - 1; i++)
        for (var j=i+1; j < divs.length; j++)
        {
            var I=divs[i];
            var J=divs[j];
    
            if ( (I.top <= J.top && (I.top + I.height) >= J.top) ||
                 (J.top <= I.top && (J.top + J.height) >= I.top) )
                document.writeln(
                    I.class + " collides with " + J.class + "<br />");
        }
    

    Output:

    A collides with B
    A collides with C
    A collides with G
    B collides with C
    B collides with D
    B collides with E
    B collides with F
    B collides with G
    C collides with E
    C collides with G
    D collides with E
    D collides with F
    D collides with G
    E collides with F
    E collides with G
    F collides with G
    

    Sample working code: http://jsfiddle.net/QUrWM/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I programmatically detect whether or not two triangles touch each other, given
I write two js code. I want first detect the width of the screen,
How could I detect, not only if the frames of two images intersect, but
Using OpenGL /GLUT how would I detect if two keys, say 'a' and 'j'
I'm looking for an algorithm to detect if two rectangles intersect (one at an
How do I detect two fingers tap on the iPhone?
I'd like to detect two buttons being pressed together with the Arduino LCD shield.
Is there a built-in function to detect collision between two circles? I have used
I am trying to detect the collision from two objects. This collision has more
I have two div's in a container div, one floating left, one floating right.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.