问答详情
源自:8-1 z-index相关实践分享

js如何获取body下子元素的最大z-idnex值?

听了老师的课,百度找了下js如何获取body下子元素的最大z-idnex值,找不到,哪位大神知道的,求分享,十分感谢。

提问者:雨中的鱼L 2016-12-05 23:57

个回答

  • 月下风物语
    2016-12-12 15:31:10
    已采纳

    技术有限,我的方法比较死板。代码如下:

    var DOMs = document.all;//获取所有DOM
    var maxIndex = 0;//最大z-index
    var curIndex = 0;//当前z-index
    for(var i = 0;i < DOMs.length;i++){
        curIndex = document.defaultView.getComputedStyle(DOMs[i],null)["z-index"];//获取z-index的值
        curIndex = curIndex == "auto" ? 0 : 1;
        maxIndex = curIndex > maxIndex ? curIndex : maxIndex;
    }
    console.log(maxIndex);


  • KeithTt
    2019-12-07 13:41:07

    let arr = [...document.all].map(ele => +window.getComputedStyle(ele).zIndex || 0);

    let maxZ = arr.length ? Math.max(...arr) : 0;

    console.log(maxZ);