磨砂玻璃外观

IT技术 javascript css
2021-03-01 13:09:52

CSS 中是否有背景过滤器的替代方案?为了让这个例子工作,我必须启用chrome://flags/#enable-experimental-web-platform-features

body {
  background: url('http://verdewall.com/wp-content/uploads/2016/08/Background-Images-4H9.jpg') no-repeat center;
}

.glass {
  width: 100%;
  height: 500px;
  background: rgba(0,0,0,0.8);
  
  -webkit-backdrop-filter: contrast(4) blur(30px);
  backdrop-filter: contrast(4) blur(30px);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Blur</title>
</head>
<body>
  <div class="glass">
  </div>
</body>
</html>

这是启用实验性功能后的样子: 在此处输入图片说明

问题是它下面的内容将是动态的。所以并不是所有的都是图像,我发现的替代品只适用于图像。就像这里显示的模糊的半透明一样: 在此处输入图片说明

3个回答

一种解决方法是使用剪辑路径、过滤器和相同的内容两次,然后您将获得与背景过滤器相同的结果

您可以创建一个 jQuery 脚本,当内容很多时,它会为您复制内容。您还可以考虑使用更复杂的脚本来调整所有需要的值和参数。

$('.backdrop').each(function() {
  var e = $(this).html();
  $(this).append('<div class="filter">'+e+'</div>');
})
.container {
  width: 200px;
  height: 200px;
  position: relative;
  padding:1px;
  display:inline-block;
}
.container .glass, .container .filter {
  background: url('https://lorempixel.com/400/200/') center/cover;
  text-align:center;
  color:#fff;
  height:100%;
}

.filter {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  filter: contrast(4) blur(3px);
  z-index: 2;
  clip-path: polygon(5% 15%, 82% 30%, 83% 71%, 17% 73%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container backdrop">
  <div class="glass">
    <h1>A title</h1>
    <p>Some content Some content</p>
  </div>
</div>
<div class="container">
  <div class="glass">
    <h1>A title</h1>
    <p>Some content Some content</p>
  </div>
</div>

我现在就试试。
2021-05-01 13:09:52
@JamieBonnett 它将:) 因为您可以简单地将没有剪辑路径的伪元素应用于内容;)
2021-05-09 13:09:52
@JamieBonnett 好的,我更新了 ;) 并且我也在研究另一个
2021-05-12 13:09:52
谢谢:) 它有效!虽然我不认为我会使用它,因为它会使用更多资源并可能减慢应用程序的速度。我很感谢你的帮助:D
2021-05-12 13:09:52
这对底层的动态内容性质不起作用。我会更新我的帖子以向您展示我的意思。
2021-05-13 13:09:52

很简单:过滤

filter: blur(10px);

不过,你必须稍微改变你的结构。下面是一个例子:

*{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}
main{
  height: 100vh;
  background: url('https://images.unsplash.com/photo-1477346611705-65d1883cee1e?dpr=0.800000011920929&auto=format&fit=crop&w=1199&h=800&q=80&cs=tinysrgb&crop=') fixed no-repeat;
  background-size: cover;
}
#container{
  width: 350px;
  height: 500px;
  background: inherit;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  margin-left: -175px;
  margin-top: -250px;
  border-radius: 8px;
}
#container:before{
  width: 400px;
  height: 550px;
  content: "";
  position: absolute;
  top: -25px;
  left: -25px;
  bottom: 0;
  right: 0;
  background: inherit;
  box-shadow: inset 0 0 0 200px rgba(255,255,255,0.2);
  filter: blur(10px);
}
form img{
  width: 120px;
  height: 120px;
  border-radius: 100%;
}
form{
  text-align: center;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
input{
  background: 0;
  width: 200px;
  outline: 0;
  border: 0;
  border-bottom: 2px solid rgba(255,255,255, 0.3);
  margin: 20px 0;
  padding-bottom: 10px;
  font-size: 18px;
  font-weight: bold;
  color: rgba(255,255,255, 0.8);
}
input[type="submit"]{
  border: 0;
  border-radius: 8px;
  padding-bottom: 0;
  height: 60px;
  background: #df2359;
  color: white;
  cursor: pointer;
  transition: all 600ms ease-in-out;
}
input[type="submit"]:hover{
  background: #C0392B;
}
span a{
  color: rgba(255,255,255, 0.8);
}
<main>
<div id="container">
  <form action="">
    <input type="text" value=""><br>
    <input type="password"><br>
    <input type="submit" value="SIGN IN"><br>
    <span><a href="#">Forgot Password?</a></span>
  </form>
</div>
</main>

@BoltClock 我在这里复制了代码笔。但是,根据您的逻辑,“否”的答案应该是答案并且应该被接受,因为除了背景过滤器之外别无选择。我们不是来解决人们的问题,而是来为他们指明正确的方向。就其本身而言,OP 应该使用“过滤器”。如何使用它是另一个话题,为他编写代码不是一个好习惯。
2021-04-22 13:09:52
@Jonny:因为我忙着在评论中回复。我已经取消了反对票,因为他已经把它编辑成形状了。您可能需要注意评论和编辑的时间戳 - 它们存在是有原因的。
2021-04-24 13:09:52
将某人指向正确的方向是“在不同元素上使用过滤器来模拟背景过滤器”。直截了当地说明“使用过滤器而不是背景过滤器”充其量误导,最坏的情况让您(r 的回答)看起来很傻。您不需要代表任何人编写代码来为他们指明正确的方向——您所需要的只是更多的说明,以便您的答案有意义。
2021-04-28 13:09:52
因为你的答案脱落为“使用过滤器,而不是背景过滤器”,其中,断章取义的,愚蠢的,似乎因为整个的背景下,过滤器是为了过滤器只适用于元素的特定部分,而不是整个过滤器所做的事情。如果您的示例有效,那么您的答案表明的内容与示例中显示的内容完全不同。答案需要不言自明,而不需要读者点击链接才有意义。
2021-04-30 13:09:52
@Jonny:我给 DreamWave 一个改进他的答案的机会,他接受了。我不知道为什么当 DreamWave 本人如此有建设性地接受它时,你为什么会对一个甚至不是你的答案的反对票如此兴奋。
2021-05-03 13:09:52

试试这个:

.glass:before {
  content: "";
  position: fixed;
  left: 0;
  right: 0;
  z-index: 1;
  background: rgba(0,0,0,0.8);

  display: block;
  width:100%;
  height: 100%;

  -webkit-filter: blur(30px);
  -moz-filter: blur(30px);
  -o-filter: blur(30px);
  -ms-filter: blur(30px);
  filter: blur(30px);
}