浏览器中的不透明度和 z-index 哪个优先级更高?

发布于 2024-09-01 18:36:53 字数 574 浏览 11 评论 0原文

我正在用 JavaScript 编写一个“弹出窗口”,我遇到了一件有趣的事情:

Cropped Screenshot 展示奇怪的堆叠行为

弹出窗口下方的海军蓝方块是可见的,尽管我希望它被隐藏。弹出窗口是在方块之后添加的,因此它应该位于顶部。

海军蓝方块的 CSS opacity 属性为 0.3。根据我的尝试,似乎区间 (0,1) 中的每个数字都会产生相同的结果。如果我将其更改为 1,那么它的行为将按预期进行(即弹出窗口下方的正方形部分被隐藏)。

我尝试将正方形的 z-index 属性设置为 10 ,将弹出窗口的 100 设置为 100 ,但它没有改变任何东西。

我缺少什么?为什么显示部分正方形?

测试的浏览器:

  • Firefox 3.6.x
  • Chrome 4

I'm coding a "popup window" in JavaScript and I've come across an interesting thing:

Cropped screenshot demonstrating strange stacking behavior

The navy square under the popup window is visible even though I would expect it to be hidden. The popup was added after the square, so it should be on the top.

CSS opacity property of the navy square is 0.3. From what I've tried, it seems that every number from the interval (0,1) would yield the same result. If I change it to 1, then it behaves as expected (i.e. the part of the square under the popup is hidden).

I've tried to set the z-index property to 10 for the square and 100 for the popup, but it doesn't change anything.

What am I missing? Why is part of square displayed?

Tested browsers:

  • Firefox 3.6.x
  • Chrome 4

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

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

发布评论

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

评论(8

向地狱狂奔 2024-09-08 18:36:54

可能需要示例代码来调试此问题。

您可以将 overflow:hidden 和可能的 position:relative 放在包围所有编辑器对象的 DIV 中,以尝试强制元素仅在在该 DIV 内绘制,例如:

<div style="overflow: hidden; position: relative">
    (Editor object buttons go here)
</div>

作为最后的手段,您还可以尝试在两个元素之间使用 iframe 来阻止它们渗透。

Example code might be needed to debug this problem.

You might put overflow: hidden and possibly position: relative in a DIV which surrounds all the editor objects to try to force the elements to only be drawn within that DIV, e.g:

<div style="overflow: hidden; position: relative">
    (Editor object buttons go here)
</div>

As a last resort, you could also try a iframe in between the two elements to try to stop them seeping through.

-残月青衣踏尘吟 2024-09-08 18:36:54

您可以尝试使用 !important 设置弹出窗口的 DIV,这样样式在应用新样式或类时不会改变:

background-color: white !important;
z-index: 100 !important;
opacity: 1.0 !important;

然后,创建新的 CSS 类:

.PopupElement
{
 z-index: inherited;
 opacity: inherited;
}

并将类添加到窗口,例如:

<input value="posx" class="some_class PopupElement"/>

我的猜测是,这会起作用,因为应用 CSS 属性没有优先级......据我所知。 =)

You might try to set the popup window's DIV like this using !important so the style doesn't change on applying new style or class:

background-color: white !important;
z-index: 100 !important;
opacity: 1.0 !important;

Then, make new CSS class:

.PopupElement
{
 z-index: inherited;
 opacity: inherited;
}

And add class to all elements in the window, like this for example:

<input value="posx" class="some_class PopupElement"/>

My guess is that this would work, since there is no priority in applying CSS attributes... as far as I know. =)

善良天后 2024-09-08 18:36:54

我有同样的问题。使用 rgba 而不是颜色/不透明度解决了我的问题。使用 LESS(在 Bootstrap 框架中),fade() 函数为我完成了转换。

I had the same issue. Using rgba instead of color/opacity solved my problem. Working with LESS (in the Bootstrap framework), the fade() function did the conversion for me.

对你再特殊 2024-09-08 18:36:54

虽然 @Guillaume Esquevin 已经给出了一个很好的答案,但我会尝试扩展它,以防有人忽略堆叠上下文是什么(就像我所做的那样)。

正如您可以在此处阅读的那样,是一种称为堆栈上下文的东西,它指的是一组共享父元素并在堆栈中一起移动的元素。一个例子是 div 及其所有子项。

有三种方法可以创建堆叠上下文:在文档的根目录(html 元素)中,通过定位父元素,以及将父元素的不透明度更改为低于 1 的值。

然后,如果您有一个 div如果不透明度低于 1,并且您希望该 div 的某个同级元素出现在其(及其子级)后面,则可以通过将其位置设置为相对位置或更改其不透明度来在此类同级元素上创建新的堆叠上下文。

Although @Guillaume Esquevin already gave a great answer, I will try to expand on it in case someone ignores what a stacking context is (like I did).

As you can read here, there is something called stacking context, which refers to a group of elements sharing a parent that move together in the stack. An example could be a div and all its children.

There are three ways to create a stacking context: in the root of the document (the html element), by positioning the parent element, and by changing the opacity of the parent to something lower than 1.

Then, if you have a div with opacity lower than 1 and you want some sibling element of this div to appear behind it (and its children), you can create a new stacking context on such sibling by setting its position to relative or by changing its opacity as well.

清醇 2024-09-08 18:36:53

这不是一个错误,实际上它应该如何工作。这有点令人困惑,因为堆叠上下文的详细描述没有提及任何相关内容。但是,视觉格式化模块链接到颜色模块,可以在其中找到这个特定的问题(强调我的):

由于不透明度小于 1 的元素是由单个元素合成的
屏幕外图像,其外部的内容无法按 z 顺序分层
其内部的内容片段之间。出于同样的原因,
实现必须为任何元素创建一个新的堆栈上下文
不透明度小于 1。如果不透明度小于 1 的元素是
未定位,实现必须绘制它创建的层,
在其父堆栈上下文中,以相同的堆栈顺序
如果它是带有 'z-index: 0' 的定位元素并且
'opacity: 1'.
如果定位了不透明度小于 1 的元素,
'z-index' 属性按照 [CSS21] 中的描述应用,除了
'auto' 被视为 '0',因为新的堆栈上下文总是
创建的。有关更多信息,请参阅 [CSS21] 的第 9.9 节和附录 E
有关堆叠上下文的信息。本段中的规则不
适用于 SVG 元素,因为 SVG 有自己的渲染模型([SVG11],
第 3 章)。

This is not a bug and is actually how it's supposed to work. It's a bit confusing as the elaborate description of Stacking Contexts doesn't mention anything about it. However, the visual formatting module links to the color module where this particular gotcha can be found (emphasis mine):

Since an element with opacity less than 1 is composited from a single
offscreen image, content outside of it cannot be layered in z-order
between pieces of content inside of it. For the same reason,
implementations must create a new stacking context for any element
with opacity less than 1. If an element with opacity less than 1 is
not positioned, implementations must paint the layer it creates,
within its parent stacking context, at the same stacking order that
would be used if it were a positioned element with ‘z-index: 0’ and
‘opacity: 1’.
If an element with opacity less than 1 is positioned,
the ‘z-index’ property applies as described in [CSS21], except that
‘auto’ is treated as ‘0’ since a new stacking context is always
created. See section 9.9 and Appendix E of [CSS21] for more
information on stacking contexts. The rules in this paragraph do not
apply to SVG elements, since SVG has its own rendering model ([SVG11],
Chapter 3).

空宴 2024-09-08 18:36:53

这不是 opacityz-index 更重要的问题,而不是 z-index 相对于其堆叠上下文的问题(请参阅z-index)。

换句话说,z-index 仅在定位祖先的上下文中才有意义(无论是相对、绝对还是固定)。要解决问题,您需要做的是将 position:relative; 添加到包含弹出窗口和海军方块的元素,并可能添加它 z-index: 1; 。看到你的屏幕截图,它可能是一个顶部元素,例如包装 div。

It's not a problem of opacity being more important than z-index, rather than z-index being relative to their stacking context (see z-index in the CSS2 specification).

In other words, z-index are only significant within the context of a positioned ancestor (whether its relative, absolute or fixed). What you need to do to fix your problem is add a position: relative; to the element that contain both your popup and your navy square, and probably add it a z-index: 1; . Seeing your screenshot it will probably be a top element such as a wrapper div.

并安 2024-09-08 18:36:53

两个元素(例如 div)的解决方法:向顶部元素添加 0.99 不透明度,然后重新建立两个元素的顺序。

opacity: 0.99;

Workaround for two elements, like divs: add a 0.99 opacity to your top element, and the order of both is reestablished.

opacity: 0.99;
救星 2024-09-08 18:36:53

使用不透明度的另一种方法是使用透明颜色(带有 alpha 值),

因此,

{
    background: gray;
    opacity: 0.5;
}

您可以尝试

{
    background: rgba(128,128,128,0.5);
}

使用透明颜色,它并不相同,但我遇到了与您遇到的相同问题,上面的方法修复了它。

An alternative to using opacity, is to use a transparent colour (with an alpha value)

So, rather than using

{
    background: gray;
    opacity: 0.5;
}

You could try

{
    background: rgba(128,128,128,0.5);
}

It isn't identical, but I was encountering the same issue you were having, and the above fixed it.

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