gpt4 book ai didi

html - :after element with rotate moving on resize

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:15 24 4
gpt4 key购买 nike

我有一个 :before 和一个 :after 元素(用于阴影目的),并应用了 transform: rotate。 :before 在调整视口(viewport)大小时保留在原位,但是 :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;
}

在您的示例中,当我删除翻译时,右侧的红色框位于图像下方(在新行上),然后您尝试使用翻译将其向上拖动,但对我来说这是一种更直接的方式,如果您只需使用顶部/左侧/右侧属性将其放在图像上即可开始。

注意:我的例子是简化的,它没有阴影和其他东西,它被削减到与问题相关的部分。

完整代码:

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 element with rotate moving on resize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28475659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com