我有一个:before和:after元素(出于阴影目的),并带有一个transform:旋转应用于它。调整视口大小时,:before保持在原位,但是:after稍微上下移动。如果旋转被移除,则它在调整大小时保持不变。如何使其与轮播保持一致?
我在:before和:after上设置了z-index:1,以查看问题
这是阴影#3
http://codepen.io/mildrenben/details/jEYpyP/
HTML:
<h2>Shadow #3</h2>
<div class="row shadow-3">
<div class="shadow-box">
<img>
</div>
<div class="shadow-box">
<img>
</div>
<div class="shadow-box">
<img>
</div>
<div class="shadow-box">
<img>
</div>
</div>
CSS:
html, body {
width: 100%;
height: 100%;
}
.row {
height: 160px;
width: 80%;
margin: 0 auto 60px;
display: block;
img:nth-of-type(1) {
content: url(http://marshall.org/wp-content/themes/marshall/img/featured-space-policy.jpg);
}
img:nth-of-type(2) {
content: url(http://i.dailymail.co.uk/i/pix/2014/09/04/1409790551890_wps_28_Astronaut_Reid_Wiseman_po.jpg);
}
img:nth-of-type(3) {
content: url(http://cdn2.collective-evolution.com/assets/uploads/2014/08/life_in_space-728x400.jpg);
}
img:nth-of-type(4) {
content: url(http://s.ngm.com/2007/10/space-travel/img/space_feature.jpg);
}
}
img {
width: 23%;
margin: 0 1% 0 0;
height: 100%;
transition: all 0.2s ease-in-out;
cursor: pointer;
}
.shadow-box {
width: 23%;
margin: 0 1% 0 0;
height: 100%;
transition: all 0.2s ease-in-out;
cursor: pointer;
display: inline-block;
img {
width: 100%;
height: 100%;
&:hover {
transform: translateY(-4px);
}
}
}
.shadow-box:before, .shadow-box:after {
content: '';
z-index: 1;
position: absolute;
width: 8%;
height: 110px;
background: red;
transform: skew(-10deg) rotate(-6deg) translate(calc(3% + 10px), 31px);
transition: all 0.2s ease-in-out;
}
.shadow-box:after {
transform: skew(10deg) rotate(6deg) translate(126%, -146px);
}
.shadow-box:hover:before, .shadow-box:hover:after {
box-shadow: 0 20px 20px lighten($black, 40%);
}
最佳答案
我不认为使用翻译是解决问题的方法,我将容器设置为position:relative;然后使用绝对定位来定位阴影物体:http://jsfiddle.net/bpma9ko4/
这里相关部分:
.shadow-box {
width: 45%;
margin: 0 1% 0 0;
height: 100%;
transition: all 0.2s ease-in-out;
cursor: pointer;
display: inline-block;
position: relative;
}
.shadow-box:before, .shadow-box:after {
content: '';
z-index: 1;
position: absolute;
width: 40%;
height: 110px;
background: red;
transform: skew(-10deg) rotate(-6deg);
transition: all 0.2s ease-in-out;
top: 20px;
left: 20px;
}
.shadow-box:after {
transform: skew(10deg) rotate(6deg);
right: 20px;
left: auto;
}
在您的示例中,当我移除翻译时,右侧的红色框位于图像下方(在新行上),然后您尝试将其与翻译一起向上拖动,但对我来说,如果您将其放置在一个更直接的方式首先使用top / left / right属性将其放在图片上。
注意:我的示例已简化,没有阴影和其他东西,它被简化为与问题相关的部分。
整个代码:
html, body {
width: 100%;
height: 100%;
}
.row {
height: 160px;
width: 80%;
margin: 0 auto 60px;
display: block;
img:nth-of-type(1) {
content: url(http://marshall.org/wp-content/themes/marshall/img/featured-space-policy.jpg);
}
img:nth-of-type(2) {
content: url(http://i.dailymail.co.uk/i/pix/2014/09/04/1409790551890_wps_28_Astronaut_Reid_Wiseman_po.jpg);
}
img:nth-of-type(3) {
content: url(http://cdn2.collective-evolution.com/assets/uploads/2014/08/life_in_space-728x400.jpg);
}
img:nth-of-type(4) {
content: url(http://s.ngm.com/2007/10/space-travel/img/space_feature.jpg);
}
}
img {
width: 100%;
margin: 0 1% 0 0;
height: 100%;
transition: all 0.2s ease-in-out;
cursor: pointer;
}
.shadow-box {
width: 45%;
margin: 0 1% 0 0;
height: 100%;
transition: all 0.2s ease-in-out;
cursor: pointer;
display: inline-block;
position: relative;
}
.shadow-box:before, .shadow-box:after {
content: '';
z-index: 1;
position: absolute;
width: 40%;
height: 110px;
background: red;
transform: skew(-10deg) rotate(-6deg);
transition: all 0.2s ease-in-out;
top: 20px;
left: 20px;
}
.shadow-box:after {
transform: skew(10deg) rotate(6deg);
right: 20px;
left: auto;
}
.shadow-box:hover:before, .shadow-box:hover:after {
box-shadow: 0 20px 20px lighten($black, 40%);
}
<h2>Shadow #3</h2>
<div class="row shadow-3">
<div class="shadow-box">
<img src="http://placehold.it/466x180">
</div>
<div class="shadow-box">
<img src="http://placehold.it/466x180">
</div>
<div class="shadow-box">
<img src="http://placehold.it/466x180">
</div>
<div class="shadow-box">
<img src="http://placehold.it/466x180">
</div>
</div>
关于html - :after带有旋转的元素在调整大小时移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28475659/