CSS:解决方案到背景滤镜?

rya*_*sh7 53 html css

backdrop-filter 是最近的css功能,现在还没有在现代浏览器中使用(至少截至2016年7月1日).

  • Chrome 51支持backdrop-filter通过Experimental Web Platform标志.
  • Safari 9.1支持--webkit-前缀
  • Firefox 47没有支持

处于这种无法使用的状态,我想知道是否存在任何其他方式来产生相同的结果.

JS的模糊,灰度等工作区也很受欢迎

backdrop-filter可以通过https://bugs.chromium.org/p/chromium/issues/detail?id=497522跟踪开发情况

Kro*_*ono 43

我不知道你是否还在追问这个问题,但对于未来的其他用户:

这个效果可以通过CSS Pseudo-Classes实现如下,不需要JavaScript!如下所示:

body,
main::before {
  background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/80625/tree.jpg) 0 / cover fixed;
}

main {
  margin: 100px auto;
  position: relative;
  padding: 10px 5px;
  background: hsla(0, 0%, 100%, .3);
  font-size: 20px;
  font-family: 'Lora', serif;
  line-height: 1.5;
  border-radius: 10px;
  width: 60%;
  box-shadow: 5px 3px 30px black;
  overflow: hidden;
}

main::before {
  content: '';
  margin: -35px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  filter: blur(20px);
  z-index: -1;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <blockquote>"The more often we see the things around us &#45; even the beautiful and wonderful things &#45; the more they become invisible to us. That is why we often take for granted the beauty of this world: the flowers, the trees, the birds, the clouds &#45;
    even those we love. Because we see things so often, we see them less and less."
    <footer>&mdash;
      <cite>
        Joseph B. Wirthlin
      </cite>
    </footer>
  </blockquote>
</main>
Run Code Online (Sandbox Code Playgroud)

在Codepen上可以看到实例:https://codepen.io/jonitrythall/pen/GJQBOp

快速注意:

根据您已经设置的网站结构,您的Z-Index可能与示例中描述的不同.

  • 好的解决方法.但有一个后备,因为背景上的其他元素不会模糊. (12认同)
  • 唯一的问题是它当然不是动态的.它与静态图像联系在一起.但针对特定情况的良好解决方法:) (4认同)
  • 据我了解, :before 伪类获取与我们只使用 `filter: blur(2px);` 的 body 相同的背景图像,所以这个技巧不适用于背景中的其他元素。 (3认同)

Duv*_*rai 43

我用它来获得流行的磨砂玻璃效果.直到某人成功发明了一个好的polyfill,backdrop-filter因为我使用了一个稍微透明的背景作为后备:

/* slightly transparent fallback */
.backdrop-blur {
  background-color: rgba(255, 255, 255, .9);
}

/* if backdrop support: very transparent and blurred */
@supports ((-webkit-backdrop-filter: blur(2em)) or (backdrop-filter: blur(2em))) {
  .backdrop-blur {
    background-color: rgba(255, 255, 255, .5);
    -webkit-backdrop-filter: blur(2em);
    backdrop-filter: blur(2em);
  }
}
Run Code Online (Sandbox Code Playgroud)

该过滤器适用于当前支持的浏览器.(启用了实验性Web平台功能的Safari和Chrome)backdrop-filter如果规范在此之前没有发生变化,代码也应该适用于未支持前缀的未来浏览器.

没有和支持背景滤镜的示例:

透明的后备 模糊

  • 我喜欢这种方法。我今天在这里学到的一个小细节 https://blog.logrocket.com/new-in-chrome-76/ 是,在这种情况下,“@supports”可以与“none”一起使用。好处是实际值不会不必要地重复。 (4认同)
  • 当然,[@support 并不适用于所有浏览器](https://caniuse.com/css-featurequeries),但它适用于[所有支持background-filter 的浏览器](https://caniuse.com/css-backdrop) -filter) 因此上述后备应该按预期工作。 (2认同)

Mas*_*eed 18

从 Chrome M76 开始,背景过滤器现已发货,无前缀且无需标记。

https://web.dev/backdrop-filter/

注意:(由于 Mozilla 尚未发布此答案,因此一直被否决):此功能在 Safari、Chrome 和 Edge 中可用,但在 Firefox 中尚不可用。Mozilla计划很快发货,但还没有。因此,此答案不包含“变通方法”,而仅包含有关哪些浏览器需要变通方法的更多信息。这似乎仍然是有用的信息。

  • 目前在 Firefox 74 上仍然不支持“backdrop-filter”。并不是每个人都使用 Chrome 或基于 chromium 的浏览器,而且,我对此投了反对票,因为这不是问题的答案。 (48认同)
  • 感谢您的反对票。由于问题是“背景过滤器的解决方法”,我认为人们可能有兴趣知道 Chrome 不需要解决方法。Firefox 正在开发此功能,今天可以使用layout.css.backdrop-filter.enabled 标志进行试用。 (6认同)
  • @Bruno Finger Firefox 支持背景过滤器,但您需要更改 about:config 上的两个选项 [查看](https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter) (3认同)

Ahm*_*med 5

使用SVG过滤器。他们像魅力一样工作。签出此示例CodePen

backdrop-filterChrome Canary现在默认支持。Firefox开发的优先级列表也在2019年。因此,它将很快在所有浏览器中得到支持,但是现在您可以使用SVG过滤器。

更新资料

SVG过滤器不能完全正常工作backdrop-filter。这只是一种解决方法,如果下面有移动的元素,则无法使用。无论如何,我认为我们backdrop-filter很快就能在生产中使用。现在,Chrome Beta中已默认启用该功能!这也意味着边缘。