What is default z-Index of
element in HTML, and how to get it using JavaScript?

前端 未结 6 2020
面向向阳花
面向向阳花 2021-02-01 15:13

So normally we can get z-Index value of a div element using, e.g:

var zindex = document.getElementById(\'id\').style.zIndex;

I am using this

6条回答
  •  醉话见心
    2021-02-01 15:55

    Have a look at this question: Getting the z-index of a DIV in JavaScript?:


    Since z index is mentioned in the CSS part you won't be able to get it directly through the code that you have mentioned. You can use the following example.

    function getStyle(el,styleProp)
    {
        var x = document.getElementById(el);
    
        if (window.getComputedStyle)
        {
            var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); 
        }  
        else if (x.currentStyle)
        {
            var y = x.currentStyle[styleProp];
        }                     
    
        return y;
    }
    

    pass your element id and style attribute to get to the function.

    Eg:

    var zInd = getStyle ( "normaldiv1" , "zIndex" );
    alert ( zInd );
    

    For firefox you have to pass z-index instead of zIndex

    var zInd = getStyle ( "normaldiv1" , "z-index" );
     alert ( zInd );
    

    Reference


    You must use z-index for Chrome as well.

提交回复
热议问题