javascript, css, z-index, div 堆叠

标签 javascript css html z-index

我想创建一种仅作用于页面内单个 DIV 的浅/粗框。 当我触发第一个 div(phantom_back) 时,它会按照我的要求运行,但第二个 div,phantom_top 将其自身设置在 phantom_back 之后,而不管其 z-index 和定位如何。

我做错了什么?

这是我目前所拥有的:

<html>
<head>
    <script type="text/javascript">
    <!--//
        function phantom_back(image)
        {
            document.getElementById('phantom_back').style.zIndex = 100;
            document.getElementById('phantom_back').style.height = '100%';
            document.getElementById('phantom_back').style.width = '100%';
            phantom_top();
        }

        function phantom_top(image)
        {
            document.getElementById('phantom_top').style.zIndex = 102;
            document.getElementById('phantom_top').style.height = 600;
            document.getElementById('phantom_top').style.width = 600;
            document.getElementById('phantom_top').style.top = 0;
            document.getElementById('phantom_top').style.left = 0;
        }
    //-->
    </script>
</head>
<body>
    <a href="#" onclick="phantom_back()">Change</a>

    <div style="height: 700px; width: 700px; border: 2px black solid; margin:0 auto; background-color: red;" id="overlord">
        <div style="height: 10px; width: 10px; position: relative; z-index: -1; background-color: #000000; filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5;" id="phantom_back"></div>

        <div style="height: 10px; width: 10px; position: relative; z-index: -3; margin: 0 auto; background-color: green;" id="phantom_top">asdf</div>
    </div>

</body>
</html>

我在徘徊,为什么我能找到的教程都没有提供这样的内容。

最佳答案

所以,我明白了。我在 phantom_back 上设置了一个绝对定位,而不是试图重新堆叠它们,我只是设置了可见性。当我尝试设置 z-index 时,它会崩溃。

<html>
<head>
    <script type="text/javascript">
    <!--//
        function phantom_back(image)
            {
                document.getElementById('phantom_back').style.height = 700;
                document.getElementById('phantom_back').style.width = 700;
                document.getElementById('phantom_back').style.zIndex = 50;
                phantom_top();
            }

        function phantom_top()
            {
                document.getElementById('phantom_top').style.height = 600;
                document.getElementById('phantom_top').style.width = 600;
                document.getElementById('phantom_top').style.visibility = "visible";
            }
    //-->
    </script>
</head>
<body>
    <a href="#" onclick="phantom_back()">Change</a>

    <div style="height: 700px; width: 700px; border: 2px black solid; margin:0 auto; background-color: red;" id="overlord">
        <div style="height: 10px; width: 10px; position: absolute; z-index: -1; background-color: #000000; filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5;" id="phantom_back"></div>

        <div style="margin: 0 auto; text-align: center; height: 10px; width: 10px;  position: relative; z-index: 102; top: 10px; background-color: white; visibility: hidden;" id="phantom_top"><br /><br /><img src="load.gif"></div>
    </div>

</body>
</html>

关于javascript, css, z-index, div 堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/529407/

相关文章:

javascript - 第二次使用输入文件不再触发 onchange

javascript - 将数据分成 n 组,按某些数字属性分布

html - 导航未与父 Div 垂直对齐

javascript - 如何修复多张图片的拖放问题?

javascript - 如何在单击缩略图上以大图像添加事件类

javascript - 监控来自 IE BHO 的 ajax 调用

javascript - 如何检查谷歌电子表格中的单元格类型?

html - 如何消除网格项底部的多余空白?

css - 在运行时突出显示行

javascript - 如何为我的 javascript 代码中的每个 div 分配一个唯一的 ID?