可以使用 mix-blend-mode 在忽略不透明层的情况下模糊背景吗?

3
我有一个带有模糊背景图像的小页面。在其上方,我设置了作为卡片的div,它们在背景图像上具有透明叠加层。我正在尝试使用混合模式设置文本效果,而不考虑叠加层。
我想要复制的文本效果在这个codepen中:https://codepen.io/thebabydino/pen/JNWqLL 具体来说,我想要复制左侧的示例,其中投影颜色与文本一起反转,并且文本是下面图像的反转。
在我的尝试中,我无法使混合模式效果忽略不透明层并仅专注于图像。
不透明层的目的是帮助文本更加突出,但似乎产生了相反的效果,因为它影响混合,使所有内容变暗。
文本之所以只是橙色,是因为由于较暗的不透明层并完全忽略下面的图像而不断反转。
这是我的尝试:

html,
body {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    webkit-font-smoothing: antialiased;
    moz-osx-font-smoothing: grayscale;
    font-family: 'Squada One', sans-serif !important;
}

body::before {
    content: '';
    background-image: url("https://images.unsplash.com/photo-1641119554941-1c286b163e1c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1920&q=80");
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-size: cover;
    overflow: hidden;
    position: fixed;
    z-index: -1;
    filter: blur(2.5px);
    width: 100vw;
    height: 100vh;
    transform: scale(1.1);
}

.parenttile {
    margin: 0 auto;
    text-align: center;
    display: flex;
    align-items: center!important;
}


.link_wrapper2 {
    width: auto;
    padding: 0px;
}

.boxborder {
    border-color: blue;
    border-width: 0.3em;
    border-style: solid;
    border-radius: 15px !important;
}

.front {
    position: relative;
    z-index: 2 !important;
}

.blend {
    text-shadow: 0 2px 2px #fdee00;
    -webkit-filter: invert(100%);
    filter: invert(100%);
    mix-blend-mode: lighten;
}

.test-box:before {
    content: ' ';
    position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
    background-color: rgba(0,0,0,0.20);
    z-index: 1;
}

.test-box {
    background-image: url("https://images.unsplash.com/photo-1641225635705-139a1a5e3938?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=261&q=80");
    background-size: cover;
    background-repeat: no-repeat;
    height: 150px;
    border-radius: 15px !important;
    position: relative;
}

header.blending h2 {
  color: white;
  text-shadow: 0 3px 1px orange;
  mix-blend-mode: difference;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Box Test</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css" integrity="sha512-IgmDkwzs96t4SrChW29No3NXBIBv8baW490zk5aXvhCD8vuZM3yUSkbyTBcXohkySecyzIrUwiF/qV0cuPcL3Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>

<body>
    <div class="columns is-centered desktop  front">
        <div class="column is-9">
            <div class="tile is-ancestor parenttile ml-3 mr-3">
                <div class="tile is-4 is-parent">
                    <div class="tile is-child box test-box boxborder has-text-white has-text-centered" style="overflow:hidden;">
                        <a href="#">
                            <div class="link_wrapper2 is-align-items-center is-flex blend front">
                                <header class="blending">Contrasting Text Example</header>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

我该如何使用混合模式效果来反转图像底部,同时忽略文本和图像之间的叠加层?如果这种方法不可行,有什么替代方法可以实现相同的结果?

这与您之前提出的问题不同吗? - A Haworth
@AHaworth 同样的问题,只是修订得更清晰,没有其他问题了。 - Jake Rankin
您在HTML代码中没有使用.blurDiv CSS规则。哪个元素需要具有.blurDiv类? - the Hutt
@onkarruikar 你可以忽略它,我拿了我的实际页面并尽可能地缩小了它,以便为问题提供示例,只是出于疏忽没有删除它。 - Jake Rankin
1个回答

1
半透明的覆盖层将会对整个卡片添加阴影。我们可以在覆盖层中使用相同的图像并将其淡化。

html,
body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  webkit-font-smoothing: antialiased;
  moz-osx-font-smoothing: grayscale;
  font-family: 'Squada One', sans-serif !important;
}

body::before {
  content: '';
  background: black background-attachment: fixed;
  background-repeat: no-repeat;
  background-size: cover;
  overflow: hidden;
  position: fixed;
  z-index: -1;
  filter: blur(2.5px);
  width: 100vw;
  height: 100vh;
  transform: scale(1.1);
}

.parenttile {
  margin: 0 auto;
  text-align: center;
  display: flex;
  align-items: center !important;
}

.link_wrapper2 {
  width: auto;
  padding: 0px;
}

.boxborder {
  border-color: blue;
  border-width: 0.3em;
  border-style: solid;
  border-radius: 15px !important;
}

.front {
  position: relative;
  z-index: 2 !important;
}

.blend {
  text-shadow: 0 2px 2px #fdee00;
  -webkit-filter: invert(100%);
  filter: invert(100%);
  mix-blend-mode: lighten;
}

.img1 {
  background-image: url("https://images.unsplash.com/photo-1641225635705-139a1a5e3938?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=261&q=80");
}

.img2 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_02.jpg");
}

.img3 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_01.jpg");
}

.test-box {
  background-size: 101% 101%;
  background-repeat: no-repeat;
  height: 150px;
  border-radius: 15px !important;
  position: relative;
}

.test-box::before {
  content: ' ';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 15px !important;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-color: #faa4;
  /*blending #faa4 with background image to make it look fade out*/
  background-blend-mode: lighten;
}


/*add image to the pseudo element and then fade it*/

.img1.test-box:before {
  background-image: url("https://images.unsplash.com/photo-1641225635705-139a1a5e3938?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=261&q=80");
}

.img2.test-box:before {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_02.jpg");
}

.img3.test-box:before {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_01.jpg");
}

header.blending {
  color: white;
  text-shadow: 0 3px 1px orange;
  mix-blend-mode: difference;
  font-size: 5rem;
  font-weight: bolder;
  filter: saturate(4);
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css" integrity="sha512-IgmDkwzs96t4SrChW29No3NXBIBv8baW490zk5aXvhCD8vuZM3yUSkbyTBcXohkySecyzIrUwiF/qV0cuPcL3Q==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<div class="columns is-centered desktop  front">
  <div class="column is-4">
    <div class="tile is-ancestor parenttile ml-3 mr-3">
      <div class="tile is-12 is-parent">
        <div class="tile is-child box test-box img1 boxborder has-text-white has-text-centered" style="overflow:hidden;">
          <a href="#">
            <header class="blending">Contra..</header>
          </a>
        </div>
      </div>
    </div>
  </div>
  <div class="column is-4">
    <div class="tile is-ancestor parenttile ml-3 mr-3">
      <div class="tile is-12 is-parent">
        <div class="tile is-child box test-box img2 boxborder has-text-white has-text-centered" style="overflow:hidden;">
          <a href="#">
            <header class="blending">Contra..</header>
          </a>
        </div>
      </div>
    </div>
  </div>
  <div class="column is-4">
    <div class="tile is-ancestor parenttile ml-3 mr-3">
      <div class="tile is-12 is-parent">
        <div class="tile is-child box test-box img3 boxborder has-text-white has-text-centered" style="overflow:hidden;">
          <a href="#">
            <header class="blending">Contra..</header>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>


另一种方法是使用background-clip:text:

.img {
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

.img1 {
  background-image: url("https://images.unsplash.com/photo-1641225635705-139a1a5e3938?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=261&q=80");
}

.img2 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_02.jpg");
}

.img3 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_01.jpg");
}

.img4 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_cat_bg_03.jpg");
}

.img5 {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2017/17_04_art_bw_bg.jpg");
}

.wrapper {
  border: 1px solid;
  height: 200px;
  width: 400px;
  padding: 0;
  border-radius: 15px !important;
  position: relative;
  background-color: #555;
  /*blend background image with #444 */
  background-blend-mode: screen;
}

.text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
  width: 100%;
  margin: 0;
  font-size: 80px;
  font-weight: bolder;
  font-family: cursive;
  -webkit-text-fill-color: transparent;
  color: transparent;
  background-clip: text;
  -webkit-background-clip: text;

  filter: invert();
}
<div class="wrapper img img1">
  <div class="text img img1"><span>CONTRAS</span></div>
</div>
<div class="wrapper img img4">
  <div class="text img img4"><span>CONTRAS</span></div>
</div>
<div class="wrapper img img5">
  <div class="text img img5"><span>CONTRAS</span></div>
</div>
<div class="wrapper img img3">
  <div class="text img img3"><span>CONTRAS</span></div>
</div>
<div class="wrapper img img2">
  <div class="text img img2"><span>CONTRAS</span></div>
</div>


谢谢你的回答 - 这是一个非常有趣的尝试。我更喜欢使用混合模式而不是剪辑文本,因为我认为它可以得到更清晰的结果。我想知道是否有一种方法可以将混合/过滤器限制在主要是黑白颜色的范围内,就像我在问题中链接的CodePen一样。很多文本都是绿色的,给人一种负面的感觉,并且与图像的对比度不太好 - 可能是因为叠加层的原因?如果有一种方法可以将其限制为白色(和白色部分上的黑色),我认为它会看起来更好。 - Jake Rankin
或者只是白色文本,需要时交替倒影?也许可以使用混合模式来复制我链接的codepen中的剪辑文本示例? - Jake Rankin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接