4

我希望减少外部依赖 - 特别是尽可能用 CSS 效果替换图像,例如在这种情况下:

有角度的阴影示例

HTML 和 CSS(如这个小提琴所示)如下。

HTML

<div id="one">
  <div></div>
</div>

CSS

#one{
  width: 940px;
  height: 350px;
  padding: 0 10px;
  margin: 10px 0;
  position: relative;
}

#one{
  background-image: url("http://webhost.ischool.uw.edu/~joatwood/portfolio/images/slider-shadow.png");
}

#one > div{
  background-color: purple;
  width: 100%;
  height: 100%;
}

我想要实现的是获得这三件事:

  1. 仅在左右边缘上从元素上脱落的倾斜阴影(否则无法隐藏溢出,该元素内的其他伪元素必须超出其边缘)
  2. 阴影应该从黑色渐变为白色,因为它垂直远离中间
  3. 阴影不应该使用直边 - 它应该稍微模糊,就像你应用阴影一样box-shadow: 5px 5px 5px

到目前为止,我已经尝试了两种方法,但都没有完全成功:

  1. 我使用transform: skewX了两个具有渐变背景的容器大小的伪元素——它在#1 和#2 上完全成功,但我想不出用它实现#3 的方法。
  2. 我使用transform: rotate了上述伪元素并尝试应用一些框阴影来模拟模糊 - 这种尝试仅在 #3 上成功,就 #1 和 #2 而言根本不起作用。

我可以使用 CSS3,只要它适用于当前的主流浏览器(Firefox、Chrome),但我不能将任何伪元素应用于内部<div>,因为它们被用于页面上的另一个设计元素。

4

2 回答 2

1

真的不会解释太多,因为你已经知道它是如何工作的。对于那些不这样做的人——基本上,我linear-gradient在父母身上添加了背景。然后我在每一侧覆盖了 CSS 三角形(通过:before/添加:after)以获得所需的外观。

jsFiddle 示例- 看起来与我非常相似。

它适用于所有主流浏览器。虽然它在 Chrome 中看起来确实更好,因为 FF 在三角形上生成锯齿状边缘。

HTML

<div id="two">
  <div></div>
</div>

CSS

#two {
    width: 940px;
    height: 350px;
    padding: 0;
    margin-left: 10px;
    position: relative;
    background: rgb(255,255,255);
    background: -moz-linear-gradient(top,  rgba(255,255,255,1) 2%, rgba(0,0,0,1) 40%, rgba(0,0,0,1) 60%, rgba(255,255,255,1) 98%);
    background: -webkit-linear-gradient(top,  rgba(255,255,255,1) 2%,rgba(0,0,0,1) 40%,rgba(0,0,0,1) 60%,rgba(255,255,255,1) 98%);
    background: linear-gradient(to bottom,  rgba(255,255,255,1) 2%,rgba(0,0,0,1) 40%,rgba(0,0,0,1) 60%,rgba(255,255,255,1) 98%);
}

#two > div {
    background-color: purple;
    width: 920px;
    height: 100%;
    margin: 0px auto;
}

#two:before, #two:after {
    content: "";
    width: 10px;
    height: 0px;
    border-top: 175px solid transparent;
    border-bottom: 175px solid transparent;
    position: absolute;
    top: 0;
}

#two:before {
    left: 0px;
    border-left: 10px solid white;
}

#two:after {
    right: 0px;
    border-right: 10px solid white;
}

让我知道你的想法——据我所知,它们看起来一样。可以稍微调整渐变以使它们相同。

于 2013-11-04T04:24:43.310 回答
0
<style type="text/css">
#container {
    width: 350px;
    height: 250px;
    position: relative;
}
#box {
    height: 100%;
    width: 100%;
    background-color: #909;
    position: absolute;
}
.shadow {
    width: 10px;
    height: 50%;
    position: absolute;
    background: -moz-linear-gradient(top,  rgba(0,0,0,0) 0%, rgba(0,0,0,0.9) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0.9))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(0,0,0,0) 0%,rgba(0,0,0,0.9) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00000000', endColorstr='#e6000000',GradientType=0 ); /* IE6-9 */
    z-index: -1;
}
#TL {
    transform:rotate(-2deg);
    -ms-transform:rotate(-2deg); /* IE 9 */
    -webkit-transform:rotate(-2deg); /* Safari and Chrome */
    left: -3px;
}
#TR {
    transform:rotate(2deg);
    -ms-transform:rotate(2deg); /* IE 9 */
    -webkit-transform:rotate(2deg); /* Safari and Chrome */
    right: -3px;
}
#BR {
    transform:rotate(178deg);
    -ms-transform:rotate(178deg); /* IE 9 */
    -webkit-transform:rotate(178deg); /* Safari and Chrome */
    right: -3px;
    bottom: 0px;
}
#BL {
    transform:rotate(182deg);
    -ms-transform:rotate(182deg); /* IE 9 */
    -webkit-transform:rotate(182deg); /* Safari and Chrome */
    left: -3px;
    bottom: 0px;
}
</style>

<div id="container">
    <div id="box"></div>
    <div class="shadow" id="TL"></div>
    <div class="shadow" id="TR"></div>
    <div class="shadow" id="BR"></div>
    <div class="shadow" id="BL"></div>
</div>
于 2013-11-04T00:41:34.367 回答