: 在调整大小后旋转移动的元素

:after element with rotate moving on resize

我有一个 :before 和一个 :after 元素(用于阴影目的),应用了 transform: rotate。 :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>