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

  • Home
  • SEARCH
  • 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 7057189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:57:22+00:00 2026-05-28T03:57:22+00:00

Possible Duplicate: jQuery/Javascript collision detection What would be the best way using preferably using

  • 0

Possible Duplicate:
jQuery/Javascript collision detection

What would be the best way using preferably using $.position to check if a <div> is being touched or overlapped by another <div>?

Thanks!

  • 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-28T03:57:22+00:00Added an answer on May 28, 2026 at 3:57 am

    Here’s a function I wrote a while back that does this:

    var percentCovered = function (dim1, dim2) {
        // The whole thing is covering the whole other thing
        if (
                (dim1.left <= dim2.left) && 
                (dim1.top <= dim2.top) && 
                ((dim1.left + dim1.width) >= (dim2.left + dim2.width)) && 
                ((dim1.top + dim1.height) > (dim2.top + dim2.height))
        ) {
                return 100;
        }
        // Only parts may be covered, calculate percentage
        else {
                dim1.right      = dim1.left + dim1.width;
                dim1.bottom     = dim1.top + dim1.height;
                dim2.right      = dim2.left + dim2.width;
                dim2.bottom     = dim2.top + dim2.height;
    
                var l = Math.max(dim1.left, dim2.left);
                var r = Math.min(dim1.right, dim2.right);
                var t = Math.max(dim1.top, dim2.top);
                var b = Math.min(dim1.bottom, dim2.bottom);
    
                if (b >= t && r >= l) {
                /* $('<div/>').appendTo(document.body).css({
                                background: 'red', 
                                position:   'absolute',
                                left:       l + 'px', 
                                top:        t + 'px', 
                                width:      (r - l) + 'px', 
                                height:     (b - t) + 'px', 
                                zIndex:     100
                        }); */
    
                        var percent = (((r - l) * (b - t)) / (dim2.width * dim2.height)) * 100;
    
                //      alert(percent + '% covered')
    
                        return percent;
                }
        }
    
        // Nothing covered, return 0
        return 0;
    };
    

    It takes as arguments two objects each containing width, height, left and top properties. Like this:

    var el1     = $('#el-1');
    var el2     = $('#el-1');
    var offset1 = el1.offset();
    var offset2 = el2.offset();
    var dim1    = {
        width:  el1.outerWidth(), 
        height: el1.outerHeight(), 
        left:   offset1.left, 
        top:    offset1.top
    };
    var dim2    = {
        width:  el2.outerWidth(), 
        height: el2.outerHeight(), 
        left:   offset2.left, 
        top:    offset2.top
    };
    
    alert(percentCovered(dim1, dim2));
    

    I guess you could move that offset stuff to the function if you don’t mind it being calculated every time you call it.

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

Sidebar

Related Questions

Possible Duplicate: javascript: pause setTimeout(); Im using jQuery and working on a notification system
Possible Duplicate: Kill Ajax requests using JavaScript using jQuery Here is the simple code
Possible Duplicate: How do I check a checkbox with JQuery or Javascript? I'm trying
Possible Duplicate: What is the best way to profile javascript execution? I have a
Possible Duplicate: DOM Mutation event in JQuery or vanilla Javascript I am using backbone.js
Possible Duplicate: Submit a form using jQuery $('#form') Supposed the jQuery Object of the
Possible Duplicate: Client Id for Property (ASP.Net MVC) In my View I'm using jquery
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Is it a good idea to learn JavaScript before learning jQuery? I
Possible Duplicates: Why would a javascript variable start with a dollar sign? JQuery :

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.