在 IE7 及更早版本中,如何将子元素放置在其父元素后面

发布于 2024-09-27 04:11:11 字数 1168 浏览 7 评论 0原文

我正在尝试在 Internet Explorer 中创建模糊的投影(类似于 CSS3 框阴影)。最合乎逻辑的方法似乎是在 IE 中找到所有需要阴影的元素,并使用 jQuery 在其中注入一个新的 div。这个新的 div 将填充与其父级完全相同的空间,然后可以使用 IE 的模糊滤镜对其进行模糊处理。 html 看起来像这样:

<div id="parent">
    <div class="ie_shadow"></div>
    All visible content goes here.
</div>

CSS:

#parent {
    position: relative;
    height: 200px;
    width: 200px;
}
.ie_shadow {
    background: #111;
    position: absolute;
    height: 100%;
    width: 100%;
    z-index: -1; /* has to be at least one less than its parent */
    filter:progid:DXImageTransform.Microsoft.blur(pixelradius=3, MakeShadow='true', ShadowOpacity='0.60');
}

这将完美地工作(稍微调整以确保不同区域的重叠正常工作),除了 IE7 和更早版本似乎不允许您放置子元素无论您如何设置“位置”和“z-index”,都位于其父级后面。

另一种选择是在需要阴影的框之前或之后插入 .ie_shadow div,但这有一些问题。具体来说,没有好的方法来设置阴影的宽度(和位置),除非由需要阴影的 div 显式设置。如果用户调整页面大小,此方法似乎也会失效,因为 .ie_shadow 的高度和宽度是显式设置的并且不会自动重新计算。

非常感谢任何帮助。谢谢!

编辑:有关实例,请参见此处:http://martinsmucker.com/ demo/ie_shadow.html 对于我们这些不再使用 IE7 的人来说,设置 IE8 以 IE7 标准模式显示页面可以忠实地重现该问题。

I'm trying to create a blurred drop shadow (similar to the CSS3 box-shadow) in Internet Explorer. The most logical way to do this seems to be to find all elements that need a shadow in IE, and inject a new div inside of them using jQuery. This new div will fill the exact same space as its parent, and it can then be blurred using IE's blur filter. The html would look something like this:

<div id="parent">
    <div class="ie_shadow"></div>
    All visible content goes here.
</div>

And the CSS:

#parent {
    position: relative;
    height: 200px;
    width: 200px;
}
.ie_shadow {
    background: #111;
    position: absolute;
    height: 100%;
    width: 100%;
    z-index: -1; /* has to be at least one less than its parent */
    filter:progid:DXImageTransform.Microsoft.blur(pixelradius=3, MakeShadow='true', ShadowOpacity='0.60');
}

This would work perfectly (with a little tweaking to make sure overlapping of different regions works correctly) except for the fact that IE7 and earlier don't seem to let you place a child element behind its parent, regardless of how you set "position" and "z-index".

The other option would be to insert the .ie_shadow div either before or after the box that needs a shadow, but this has some problems of its own. Specifically, there's no good way to set the width (and position) of the shadow unless it is explicitly set by the div that needs a shadow. This method also seems to fall apart if the user resizes the page, as the height and width of .ie_shadow is set explicitly and not recalculated automatically.

Any help is much appreciated. Thanks!

Edit: for a live example, see here: http://martinsmucker.com/demo/ie_shadow.html For those of us no longer blessed with IE7, setting IE8 to display the page in IE7 Standards mode faithfully reproduces the problem.

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

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

发布评论

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

评论(1

话少心凉 2024-10-04 04:11:11

最好的答案可能是:“IE6 和 IE7 的一些问题非常严重,无法轻松修复。这似乎就是其中之一。”

显然这不是一个很好的答案,所以这就是我可以尝试的。我将使用 jQuery 的 wrap() 函数包装该元素,而不是将阴影作为该元素的绝对定位子元素插入。

$(document).ready(function(){
    $('.has_shadow').wrap('<div class="ie_shadow"></div>');
    $('.ie_shadow').each(function(){
        var childwidth = $(this).children('.has_shadow').css('width');
        $(this).css('width', childwidth)
    });
});

现在唯一的挑战是使父元素与其子元素具有相同的宽度。浮动它确实有效,但它有可能完全破坏页面预先存在的设计。可能可以使用 javascript 找到子级的宽度并将父级设置为相同的值,但这只有在子级实际具有定义的值时才真正有效。我仍在考虑这个问题,但看起来这是我能得到的最佳答案。 ;)

The best answer would probably be: "There are some things about IE6 and IE7 that are pretty broken with no easy fixes. This appears to be one of them."

Obviously that isn't a great answer, so here's what I might try. Instead of inserting the shadow as an absolutely-positioned child of the element, I'll wrap the element using jQuery's wrap() function.

$(document).ready(function(){
    $('.has_shadow').wrap('<div class="ie_shadow"></div>');
    $('.ie_shadow').each(function(){
        var childwidth = $(this).children('.has_shadow').css('width');
        $(this).css('width', childwidth)
    });
});

Now the only challenge is making the parent element have the same width as its child. Floating it left works, but it has the potential to completely destroy the pre-existing design of the page. It might be possible to find the child's width using javascript and set the parent to the same value, but this only really works if the child actual has a defined value. I'm still playing around with this, but it looks like this is the best answer I'm going to get. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文