我正在尝试将旋转应用于div。
但是,当我这样做时,它破坏了上面的“纸影效果”。为什么?
该如何保持这种效果?
.test {
position: relative;
margin: 20px auto;
width: 300px;
height: 100px;
border: 1px solid #ccc;
background: #fff;
}
.test:before {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
left: 12px;
width: 45%;
height: 20px;
background: #777;
-webkit-box-shadow: 0 15px 19px #aaa;
-moz-box-shadow: 0 15px 19px #aaa;
box-shadow: 0 15px 19px #aaa;
-webkit-transform: rotate(-3deg);
-moz-transform: rotate(-3deg);
-o-transform: rotate(-3deg);
-ms-transform: rotate(-3deg);
transform: rotate(-3deg);
}
.test:after {
z-index: -1;
position: absolute;
content: "";
bottom: 15px;
right: 12px;
width: 45%;
height: 20px;
background: #777;
-webkit-box-shadow: 0 15px 19px #aaa;
-moz-box-shadow: 0 15px 19px #aaa;
box-shadow: 0 15px 19px #aaa;
-webkit-transform: rotate(3deg);
-moz-transform: rotate(3deg);
-o-transform: rotate(3deg);
-ms-transform: rotate(3deg);
transform: rotate(3deg);
}
<div class="test">Without transform</div>
<div class="test" style="transform:rotate(2deg)">With transform:rotate(2deg)</div>
答案 0 :(得分:1)
这是因为transform
create a stacking context将阴影放置在容器内部而不是外部。例如,如果您向容器中添加z-index
值,则会遇到相同的问题:
要解决此问题,您可以考虑采用另一种方法,如下所示:
.test {
position: relative;
margin: 20px auto;
width: 300px;
height: 100px;
border: 1px solid #ccc;
z-index: 0;
}
/*this will create the shadow*/
.test:before {
z-index: -2;
position: absolute;
content: "";
bottom: 0;
left: 15px;
right: 15px;
height: 10px;
background:
linear-gradient(to top right, transparent 49.5%, #aaa 50%) top right/50% 100%,
linear-gradient(to top left, transparent 49.5%, #aaa 50%) top left/50% 100%;
background-repeat: no-repeat;
filter: drop-shadow(0 7px 5px #aaa);
}
/*this will be your background*/
.test:after {
z-index: -1;
position: absolute;
content: "";
top: 0;
bottom: 0;
right: 0;
left: 0;
background: #fff;
}
<div class="test">Without transform</div>
<div class="test" style="transform:rotate(2deg)">With transform:rotate(2deg)</div>