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 4599742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:35:04+00:00 2026-05-21T23:35:04+00:00

OK guys, here’s a tricky one (perhaps): is there any to get a sort

  • 0

OK guys, here’s a tricky one (perhaps): is there any to get a sort of “global z-index” of an element? here is an example of what i’d like to do:

given two element references, how would you determine which one is higher in the z order (given that they may not have assigned z-indices or are in separated containers)

Elaborate hacks welcome! Not-so elaborate methods more than welcome.

  • 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-21T23:35:05+00:00Added an answer on May 21, 2026 at 11:35 pm

    This was an interesting problem. The following code may not be bug free. I just put it together right now and haven’t tested it entirely. Its purpose is to demonstrate an approach.

    It compares two elements and tests them on three criteria:

    • Looking through the ancestor tree of each element, if the first ancestor for element A to have a z-index has a greater z-index than the first ancestor of element B to have a z-index (if one exists for element B at all), then element A is higher in the z-order.

    • Otherwise, if element B is a descendent of element A then element B has a higher z-order.

    • Otherwise, looking at the siblings of the last ancestor that element A and element B had in common, if an ancestor of element A comes first in the array of children of that common ancestor then element B has a higher z order.

    There may be an simpler way of describing of the logic above and the algorithm can be improved. (For example, the second check above could be done first.) However, here’s the code:

    <html>
    <head>
    <script language="javascript">
    
    function getAncestorsAsArray(element){
        var arr = new Array();
        arr.unshift(element);
        while (arr[0].parentNode){
            arr.unshift(arr[0].parentNode);
        }
    
        return arr;
    }
    
    function highestInitialZIndex(elementArr){
        for (var i=0; i<elementArr.length; i++){
            if (elementArr[i].style == undefined) continue;
            var r = elementArr[i].style.zIndex;
            if (!isNaN(r) && r!="") {
                return r;
            }
        }
    
        return undefined;
    }
    
    function findCommonAncestor(elementArr1, elementArr2){
        var commonAncestor;
        for (var i=0; i<elementArr1.length; i++){
            if (elementArr1[i] == elementArr2[i]) {
                commonAncestor = elementArr1[i];
            }
        }
    
        return commonAncestor;
    }
    
    function findHighestAbsoluteIndex(element1, element2){
        var arr1 = getAncestorsAsArray(element1);
        var arr2 = getAncestorsAsArray(element2);
    
        // Does an ancestor of one elment simply have a higher z-index?
        var arr1Z = highestInitialZIndex(arr1);
        var arr2Z = highestInitialZIndex(arr2);
        if (arr1Z > arr2Z || (!isNaN(arr1Z) && isNaN(arr2Z))) return element1;
        if (arr2Z > arr1Z || (!isNaN(arr2Z) && isNaN(arr1Z))) return element2;
    
        // Is one element a descendent of the other?
        var commonAncestor = findCommonAncestor(arr1, arr2);
        if (commonAncestor == element1) return element2;
        if (commonAncestor == element2) return element1;
    
        // OK, which has the oldest common sibling? (Greater index of child node for an element = "older" child)
        var indexOfCommonAncestor;
        for (var i=0; i<arr1.length; i++){
            if (arr1[i] == commonAncestor) {
                indexOfCommonAncestor = i;
                break;
            }
        }
    
        for (var j=commonAncestor.childNodes.length; j>=0; j--){
            if (arr1[indexOfCommonAncestor+1] == commonAncestor.childNodes[j]) return element1;
            if (arr2[indexOfCommonAncestor+1] == commonAncestor.childNodes[j]) return element2;
        }   
    }
    
    function doTest(){
        var e1 = document.getElementById("myDiv1");
        var e2 = document.getElementById("myDiv2");
    
        highest = findHighestAbsoluteIndex(e1, e2);
        alert(highest.id);
    }
    
    </script>
    
    </head>
    <body onload="javascript:doTest();">
    
    <div>
    <div>
    <div style="position:absolute; z-index:20;" id="myDiv1">
    </div>
    </div>
    </div>
    
    <div>
    </div>
    
    <div style="position:absolute; z-index:10;" id="myDiv2">
    </div>
    
    </body>
    </html>
    

    Play around with the nested divs in the body to test it working.

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

Sidebar

Related Questions

Hey everyone, You guys here are awesome, I'm getting so many questions answered, its
Im fairly new to Asp.Net arena here guys,so please bear with me if say
Hey guys, im trying to insert some checkboxes into a database and im pretty
Which Non-relational database should I choose(I know the question is a subjective one and
I got a string looking like that: EVENTS: RAID Volume Set Information Volume Set
a few days ago I posted this question on the ANTLR mailinglist, but didn't
Alright, I am so close to finishing this I can taste it. Over the
I am using jQuery to check username availability when registering on my web application.
I have a string similar to belows: a_b_c_d_1 x_y_z_12 Now, you can see that
Today I was designing a transparent PNG background that would only sit in the

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.