微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CSS:如何在CSS缩放转换期间管理溢出?

如何解决CSS:如何在CSS缩放转换期间管理溢出?

我遇到了规模转换溢出的问题。

这是codepen:https://codepen.io/rukawaz/pen/XWdByLP

当我将鼠标悬停在左div时,一切正常,过渡工作顺利且没有溢出。

.header {
  height: 90vh;
  margin: 0;
  background-color: #fff;
  position: relative;
  overflow: hidden;
  clip-path: polygon(0 0,100% 0%,100% 85%,0 100%);
}

.header__section {
  height: 100%;
  width: 50%;
  top: 0;
  border: 1px solid trasparent;
  position: absolute;
  overflow: hidden;
  background-size: cover;
  background-position: center center;
  transition: all .5s ease-in-out;
}

.header__section::before {
  content: "";
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  background-color: #034ea2;
  opacity: 0.8;
  transition: opacity .5s ease-in-out;
}

.header__section:hover {
  transform: scale(1.4);
}

.header__section:hover::before {
  opacity: 0;
}

.header__left {
  left: 0;
  background-image: url(https://cdn.pixabay.com/photo/2018/07/26/07/45/valais-3562988_960_720.jpg);
}

.header__right {
  left: 50%;
  background-image: url(https://cdn.pixabay.com/photo/2020/06/26/17/16/daisies-5343423_960_720.jpg);
}
<header class="header">
  <div class="header__section header__left">
    a
  </div>
  <div class="header__section header__right">
    b
  </div>
</header>

但是当我将鼠标悬停在正确的div上时,背景图像就会出现溢出问题。

解决方法

调整z索引以解决此问题。减小是否将悬停的元素确定在另一个元素之下。

.header {
  height: 90vh;
  margin: 0;
  background-color: #fff;
  position: relative;
  overflow: hidden;
  clip-path: polygon(0 0,100% 0%,100% 85%,0 100%);
}

.header__section {
  height: 100%;
  width: 50%;
  top: 0;
  border: 1px solid trasparent;
  position: absolute;
  overflow: hidden;
  background-size: cover;
  background-position: center center;
  z-index: 1; /* added */
  transition: all .5s ease-in-out,z-index 0s 0.5s;  /* added */
}

.header__section::before {
  content: "";
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  background-color: #034ea2;
  opacity: 0.8;
  transition: opacity .5s ease-in-out;
}

.header__section:hover {
  transform: scale(1.4);
  z-index: -1; /* added */
  transition: all .5s ease-in-out,z-index 0s;  /* added */
}

.header__section:hover::before {
  opacity: 0;
}

.header__left {
  left: 0;
  background-image: url(https://cdn.pixabay.com/photo/2018/07/26/07/45/valais-3562988_960_720.jpg);
}

.header__right {
  left: 50%;
  background-image: url(https://cdn.pixabay.com/photo/2020/06/26/17/16/daisies-5343423_960_720.jpg);
}
<header class="header">
  <div class="header__section header__left">
    a
  </div>
  <div class="header__section header__right">
    b
  </div>

</header>

或将比例尺应用于伪元素:

.header {
  height: 90vh;
  margin: 0;
  background-color: #fff;
  position: relative;
  overflow: hidden;
  clip-path: polygon(0 0,0 100%);
}

.header__section {
  height: 100%;
  width: 50%;
  top: 0;
  border: 1px solid trasparent;
  position: absolute;
  overflow: hidden;
}

.header__section::before,.header__section::after {
  content: "";
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  background-size: cover;
  background-position: center center;
  transition: all .5s ease-in-out;
}

.header__section::after {
  background-color: #034ea2;
  opacity: 0.8;
}

.header__section:hover::before {
  transform: scale(1.4);
}

.header__section:hover::after {
  opacity: 0;
}

.header__left {
  left: 0;
}

.header__left:before {
  background-image: url(https://cdn.pixabay.com/photo/2018/07/26/07/45/valais-3562988_960_720.jpg);
}

.header__right {
  left: 50%;
}

.header__right:before {
  background-image: url(https://cdn.pixabay.com/photo/2020/06/26/17/16/daisies-5343423_960_720.jpg);
}
<header class="header">
  <div class="header__section header__left">
    a
  </div>
  <div class="header__section header__right">
    b
  </div>

</header>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。