IE 忽略定位元素上的 Z-Index

发布于 2024-08-23 11:43:40 字数 1260 浏览 12 评论 0原文

事实证明,IE 再次成为我生存的祸根。我正在开发的网站顶部有一个水平菜单,其中的一个项目会触发一个纯 CSS 菜单,该菜单绝对定位在父菜单 DIV 内(相对定位)。这使得菜单可以完美地放置在 IE 和 W3C 兼容浏览器中。

当我在页面下方有更多定位元素时,就会出现问题。它们也是相对定位的,因为它们内部的数据需要绝对定位......同样,这在我测试过的所有浏览器中都能正确显示。

问题是,然后打开顶部菜单,部分内容被页面下方的定位元素遮挡 - 实际上,它位于这些元素下方,即使所有元素都定义了 z-index 属性。 (在 CSS 文件和内联中)。

让 IE 正确显示此内容的唯一方法是将菜单的实际 HTML 放置在页面底部,位于页面其他位置的定位元素下方(用 DOM 术语来说)。我只会将其作为绝对的最后手段。

所有元素都是相同的类型(div)。这是相关的 HTML:

<div id='menu'>
<div id='cat_menu' style='display:none;z-index:10000;'>
<table cellspacing='0' cellpadding='0' class='brmenu' width='100%'>
    [data]
</table>
</div>

<div class='product_new' style='z-index:20;'>[data]</div>
<div class='product_listing' style='background-color:#FFFFFF;'>[data]</div>

以及相关的CSS:

div#menu {
height: 26px;
padding: 0;
position: relative;
}

div#cat_menu {
position: absolute;
top: 25px;
left: 115px;
width: 300px;
z-index: 1000;
}

 div.product_new {
background-image: url("/images/sp_images.png");
background-position: 0 -108px;
background-repeat: no-repeat;
padding 0px;
height: 40px;
font-size: 9pt;
margin-top: 5px; 
position: relative;
z-index: 20;
}

IE yet again is proving to be the bane of my existence. The top of a site I'm working on has a horizontal menu, an item of which triggers a pure-CSS menu that is positioned absolute within the parent menu DIV (positioned relative). This places the menu perfectly in both IE and the W3C compliant browsers.

The problem arises when I have more positioned elements further down on the page. These are also positioned relative, because there is data inside them that needs to be positioned absolute... again, this displays properly in all browsers I've tested it on.

The problem is, that then the top menu is opened, part is obscured by the positioned elements further down the page - in effect, it's positioned BELOW these elements even though there are z-index properties defined on all. (in both the CSS file and inline).

The only way to get IE to display this properly is to place the actual HTML for the menu at the bottom of the page, below (in DOM terms) the positioned elements elsewhere on the page. I would only do this as an absolute last resort.

All Elements are the same type (div). Here is the relevant HTML:

<div id='menu'>
<div id='cat_menu' style='display:none;z-index:10000;'>
<table cellspacing='0' cellpadding='0' class='brmenu' width='100%'>
    [data]
</table>
</div>

<div class='product_new' style='z-index:20;'>[data]</div>
<div class='product_listing' style='background-color:#FFFFFF;'>[data]</div>

And the relevant CSS:

div#menu {
height: 26px;
padding: 0;
position: relative;
}

div#cat_menu {
position: absolute;
top: 25px;
left: 115px;
width: 300px;
z-index: 1000;
}

 div.product_new {
background-image: url("/images/sp_images.png");
background-position: 0 -108px;
background-repeat: no-repeat;
padding 0px;
height: 40px;
font-size: 9pt;
margin-top: 5px; 
position: relative;
z-index: 20;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乙白 2024-08-30 11:43:40

我也有同样的问题。经过一番尝试后,我同意 David 的观点:“IE 只能可靠地呈现同级元素的 z 索引。”

我确实想出了一个简单的替代解决方案。看起来 ie 将非同级元素的 z 索引呈现为零(就好像它们没有 z 索引一样)。我通过将有问题的元素及其父元素设置为负 z 索引,将它们放在 z 索引重置为零的元素下方,解决了这个问题。

I had the same problem. After playing around a bit I agree with David: "IE only reliably renders z-indexes for sibling elements."

I did come up with an easy alternate solution. It seems that ie renders z-index for non sibling elements as zero (as if they didn't have a z-index). I solved the problem by setting the offending elements and their parent elements to have a negative z-index, putting them below the elements with z-index reset to zero by ie.

哀由 2024-08-30 11:43:40

上面的 jQuery 解决方案似乎也适用于 Prototype...我在下面包含了 PrototypeJS 代码:

var zIndexNumber = 1000;
$("div").each(function(e) {
    e.setStyle({ zIndex: zIndexNumber });
    zIndexNumber -= 10;
});

The solution above for jQuery seems to work with Prototype as well... I've included the PrototypeJS code below:

var zIndexNumber = 1000;
$("div").each(function(e) {
    e.setStyle({ zIndex: zIndexNumber });
    zIndexNumber -= 10;
});
心如荒岛 2024-08-30 11:43:40

有几点需要注意:

  • suckerfish 下拉菜单已被证明效果良好。为什么要重新发明轮子?
  • IE 仅可靠地呈现同级元素的 z 索引。因此,最好仅使用“z-index”元素作为 BODY 标记的直接子元素。
  • 如果您不想更改标记,则可能需要在“20”上方的 #menu 元素上设置 z 索引。问题是设置 z-index 会将元素从流中取出(这是不可取的)。
  • style="..." 属性使 css 非常具体(要求您在样式表中使用“!important”。请不要忘记迁移这些样式属性)
  • “position:relative”触发 IE 的“hasLayout”

A couple of notes:

  • suckerfish dropdowns are proven to work well. Why reinvent the wheel?
  • IE only reliably renders z-indexes for sibling elements. Due to this, it's best to only "z-index" elements that are direct children of the BODY tag.
  • If you don't want to change your markup, you will likely need to set a z-index on the #menu element above "20". The problem is that setting a z-index takes the element out of the flow (which is undesirable).
  • the style="..." attribute makes css incredibly specific (requiring you to use "!important" in your stylesheet. Please don't forget to migrate those style attributes)
  • "position: relative" triggers "hasLayout" for IE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文