【问题标题】:Mask in Chrome combining position absolute, overflow hidden, z-index and border-radiusChrome中的掩码结合绝对位置,溢出隐藏,z-index和border-radius
【发布时间】:2019-03-17 10:27:01
【问题描述】:

我正在尝试实现一种效果,我将在其中创建带有背景图像的 div,其中包含内容。此内容应该是具有相同图像但模糊的 flex 定位 div(同时保持其与父级的相对位置)。

在寻找救赎的过程中,我发现 CSS 规则的某些组合可以产生这种确切的效果,但仅限于 Chrome。

.container {
  width: 320px;
  height: 240px;
  position: relative; /* it is required */
  display: flex; /* it is required */
  background: url(https://loremflickr.com/cache/resized/4848_46406748211_5572c760e0_320_240_nofilter.jpg);
}

.mask {
  z-index: 1; /* it is required */
  overflow: hidden; /* it is required */
  width: 150px;
  height: 150px;
  border-radius: 1px; /* it is required */
}

.element {
  background: url(https://loremflickr.com/cache/resized/4848_46406748211_5572c760e0_320_240_nofilter.jpg);
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  filter: blur(5px);
}


<div class="container">
  <div class="mask">
    <div class="element"></div>
  </div>
</div>

https://jsfiddle.net/39um580g/16/

Chrome/Chrome 手机:

火狐:

Safari:

这太荒谬了。有没有办法让这个解决方案跨浏览器?

Moorthy G 的回答:

假设我的块放在右侧。 Chrome 版本的当前行为如我所愿:

添加任何类型的转换都会使.mask 相对,因此会破坏所需的效果:

【问题讨论】:

  • 我会说 chrome 在这里是错误的。该元素相对于上一个元素定位,因此溢出不应应用于它

标签: html css google-chrome image-masking


【解决方案1】:

添加clip-path: polygon(0px 0px, 0px 100%, 100% 100%, 100% 0); 剪辑你的面具。

.mask {
  z-index: 1; /* it is required */
  overflow: hidden; /* it is required */
  width: 150px;
  height: 150px;
  border-radius: 1px; /* it is required */      
  clip-path: polygon(0px 0px, 0px 100%, 100% 100%, 100% 0); /* Fix for Firefox*/
}

【讨论】:

  • 不,它没有。变换使.mask 相对,所以模糊的背景定位不正确。
  • 是的,有道理。我会更新答案。如果我找到任何解决方案。
  • clip-path 有效,但它不是跨浏览器解决方案。
  • 它在 IE/Edge 中不起作用。我想我应该在 Microsoft 浏览器中禁用此效果。感谢您的宝贵时间。
  • @ŁukaszSzcześniak 这是否适用于边界半径?我避免使用剪辑路径,因为我认为它会破坏半径
【解决方案2】:

一种解决方案是制作掩码元素position:absolute,以便溢出将按预期工作,然后您考虑调整background-position,同时调整元素的位置以纠正图像位置。

我使用 CSS 变量使其易于调整:

.container {
  width: 320px;
  height: 240px;
  position: relative;
  display: flex;
  --img:url(https://loremflickr.com/cache/resized/4848_46406748211_5572c760e0_320_240_nofilter.jpg);
  background:var(--img);
}

.mask {
  z-index: 1;
  overflow: hidden;
  width: 150px;
  height: 150px;
  top:var(--t,0);
  left:var(--l,0);
  position:absolute;
  border-radius: 50px;
}

.mask::before {
  content:"";
  display:block;
  background:var(--img) calc(-1*var(--l)) calc(-1*var(--t)); 
  width:100%;
  height:100%;
  filter: blur(5px);
}
<div class="container" style="--t:20px;--l:80px">
  <div class="mask">
  </div>
</div>

<div class="container" style="--t:90px;--l:0px">
  <div class="mask">
  </div>
</div>
<div class="container" style="--t:50px;--l:50px;--img:url(https://picsum.photos/320/240?image=1069)">
  <div class="mask">
  </div>
</div>

【讨论】:

  • 我已经尝试过这个解决方案,但是如果我想使用background-size: cover;会出现问题
  • @ŁukaszSzcześniak ok 会尝试另一种方式,但我看到您已经接受了剪辑路径解决方案。我猜那个适合你?
  • @TermaniAfif 我接受了剪辑路径,但这不是一个完美的解决方案(现在还可以)。如果您发现其他值得尝试的东西,(如果我是正确的)我应该能够更改接受的答案。我认为这个问题值得考虑。
猜你喜欢
  • 2011-06-04
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多