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

前端 未结 6 2019
面向向阳花
面向向阳花 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 16:09

    You misunderstand the meaning of elements "style" property. By referring element.style what you actually retrieving is elements inline style. For example:

    var el = document.getElementById('myEl');
    
    console.log(el.style.zIndex); //you are asking for 

    and to get the value of your css style(it could be a code goes here tag or any external stylesheet) you have to check is standard method (window.getComputedStyle) supported or not, if not you should check for internet explorer's specific version. You could do so by referring to el.currentStyle.

    Summary:

    to get inline style: var zIndex = el.style.zIndex
    
    to get stylesheet value or style tag value:
    
        standard method: var zIndex = window.getComputedStyle(el,null).getPropertyValue('z-index');
    
        ie method: var zIndex = el.currentStyle['z-index'];
    

提交回复
热议问题