自然CSS的解决方案是:
- 确保,即 “对话框” 获得一个堆叠内容。这可以通过
- 设置
z-index
比auto
别的东西来完成,
- ,另外
position
要么relative
,absolute
或fixed
。
- 然后将您的弹出窗口添加为“dialogbox”。如果还没有,你可以随时在DOM中移动它。
在这种情况下,您的弹出窗口根本不需要z-index
。这完全避免了“z-index地狱”。
例子:
<!doctype html>
<html>
<head>
<style type="text/css">
#dialogbox {
width: 400px; height: 300px;
top: 0; left: 0;
background-color: red;
}
#popup {
width: 500px; height: 200px;
top: 0; left: 0;
background-color: green;
}
</style>
</head>
<body>
<div id="dialogbox" style="z-index: 100; position: absolute;">
<div>
<div id="widgetThatCausesThePopup" >
<button>Show popup</button>
</div>
</div>
<div id="popup" style="position: absolute;">
<!-- Empty divs cause really weird problems.
Always make sure, that your divs aren't empty! -->
</div>
</div>
</body>
</html>
的堆叠内容甚至可以让你使用Z-指标相对于背景下,如果需要的话(注意,孩子顺序并不重要,和z索引不一定要大于100):
<div id="dialogbox" style="z-index: 100; position: absolute;">
<div id="popup" style="position: absolute; z-index: 2;">
<!-- Empty divs cause really weird problems.
Always make sure, that your divs aren't empty! -->
</div>
<div>
<div id="widgetThatCausesThePopup" style="position: absolute; z-index: 1;">
<button>Show popup</button>
</div>
</div>
</div>
如何给它一个更高的z-index? :D – mingos 2011-04-11 21:03:22
当然,我喜欢做'z-index:{z-index-of-parent + 1}',但由于父母没有z-index集合,我不知道如何找到要拍的索引! – 2011-04-11 21:04:45
每次添加弹出窗口时,都可以增加一个变量。然后,使用JavaScript将最近弹出的z-index更改为100 + inc。我不知道是否有CSS解决方案,尽管... – Azmisov 2011-04-11 22:14:18