opacity - CSS3 "Lifted Corners"带不透明度的投影

标签 opacity css

我一直在玩一些 CSS3 投影效果。我非常喜欢“抬 Angular ”效果,但在尝试向元素添加不透明度时遇到了问题。我的问题是:有没有办法在具有不透明度的元素上创建“抬 Angular ”效果?

http://jsfiddle.net/WAvZu/

.drop-shadow{
    position:relative;
    float:left;
    width:40%;
    padding:1em;
    margin:2em 10px 4em;
    background:#fff;
    -webkit-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
    -moz-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
    box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
}
.drop-shadow:before,
.drop-shadow:after{
    content:"";
    position:absolute;
    z-index:-2;
}
.lifted{
    -moz-border-radius:4px;
    border-radius:4px;
}
.lifted:before,
.lifted:after{
    bottom:15px;
    left:10px;
    width:50%;
    height:20%;
    max-width:300px;
    max-height:100px;
    -webkit-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7);
    -moz-box-shadow:0 15px 10px rgba(0, 0, 0, 0.7);
    box-shadow:0 15px 10px rgba(0, 0, 0, 0.7);
    -webkit-transform:rotate(-3deg);
    -moz-transform:rotate(-3deg);
    -ms-transform:rotate(-3deg);
    -o-transform:rotate(-3deg);
    transform:rotate(-3deg);
}
.lifted:after{
    right:10px;
    left:auto;
    -webkit-transform:rotate(3deg);
    -moz-transform:rotate(3deg);
    -ms-transform:rotate(3deg);
    -o-transform:rotate(3deg);
    transform:rotate(3deg);
}

最佳答案

问题是理解stacking contexts以及它们在浏览器中的呈现方式。

  • the root element (HTML),
  • positioned (absolutely or relatively) with a z-index value other than "auto",
  • elements with an opacity value less than 1.
  • on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto"

9.9.1 Specifying the stack level: the 'z-index' property

  1. the background and borders of the element forming the stacking context.
  2. the child stacking contexts with negative stack levels (most negative first).
  3. the in-flow, non-inline-level, non-positioned descendants.
  4. the non-positioned floats.
  5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  7. the child stacking contexts with positive stack levels (least positive first).

#test 的背景首先被渲染,因为这是应用了不透明度的元素。之后,阴影位于顶部,因为它们处于新的堆叠上下文中(position: absolute)。最后,div 的文本。

一个简单的解决方案:将 div 包裹在另一个 div 中并将不透明度应用于 that div 而不是 #test

http://jsfiddle.net/WAvZu/3/

另一个好读物:What No One Told You About Z-Index

关于opacity - CSS3 "Lifted Corners"带不透明度的投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067109/

相关文章:

css - 适用于所有浏览器的不透明度 CSS?

javascript - 如何在鼠标悬停在第三个 div 上时显示两个 div

css - 这些双破折号前缀的 CSS 属性有什么作用?

html - CSS 垂直对齐

jquery - 从兄弟元素的第一个元素创建 Accordion

html - Chrome 打印一些带有灰色填充的文本框

css - 当不透明度降低时,图像出现在导航菜单上方

java - 更改 HSB 颜色模式中的不透明度

c++ - 是否可以设置 qt 小部件的不透明度?

jquery - 为什么 jQuery .animate({opacity :0}) changes the z-index too