如何使CSS玻璃/模糊效果适用于叠加层?

问题描述 投票:68回答:6

我在半透明叠加div上应用模糊效果时遇到问题。我希望div背后的一切都模糊不清,像这样:

SFW image

这是一个不起作用的jsfiddle:http://jsfiddle.net/u2y2091z/

任何想法如何使这项工作?我希望尽可能保持简单并使其成为跨浏览器。这是我正在使用的CSS:

#overlay {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

    background:black;
    background:rgba(0,0,0,0.8);

    filter:blur(4px);
    -o-filter:blur(4px);
    -ms-filter:blur(4px);
    -moz-filter:blur(4px);
    -webkit-filter:blur(4px);
}
html css css3 blur css-filters
6个回答
61
投票

这是一个使用svg过滤器的示例。

想法是使用与svg相同的height元素和#overlay过滤器。此滤镜应用于feGaussianblur svg元素。为了给它一个拉伸效果,你可以在叠加层的底部使用image

box-shadow

Browser Support for svg filters上演示

Codepen
body {
  background: #222222;
}
#container {
  position: relative;
  width: 450px;
  margin: 0 auto;
}
img {
  height: 300px;
}
#overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 1;
  color: rgba(130, 130, 130, 0.5);
  font-size: 50px;
  text-align: center;
  line-height: 100px;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}

40
投票

谢谢大家的帮助!我能够将来自所有人的信息和来自Googling的信息拼凑起来,我想出了以下适用于Chrome和Firefox的信息:<div id="container"> <img src="http://lorempixel.com/450/300/sports" /> <div id="overlay">WET</div> <svg width="450" height="100" viewBox="0 0 450 100" style="position: absolute; top: 0;"> <defs> <filter id="blur"> <feGaussianBlur in="SourceGraphic" stdDeviation="3" /> </filter> </defs> <image filter="url(#blur)" xlink:href="http://lorempixel.com/450/300/sports" x="0" y="0" height="300px" width="450px" /> </svg> </div>。我仍在努力为IE和Opera做这项工作。

关键是将内容放在应用过滤器的div内:

http://jsfiddle.net/u2y2091z/12/

然后是CSS:

<div id="mask">
    <p>Lorem ipsum ...</p>
    <img src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />
</div>

因此,掩码应用了滤镜。另请注意,对于带有#mask { position: absolute; left: 0; top: 0; right: 0; bottom: 0; background-color: black; opacity: 0.5; filter: blur(10px); -webkit-filter: blur(10px); -moz-filter: blur(10px); -o-filter: blur(10px); -ms-filter: blur(10px); filter: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='svgMask'><feGaussianBlur stdDeviation='10' /></filter></svg>#svgMask"); } 标记的过滤器,使用url() - 这个想法来自<svg>。如果您碰巧缩小了CSS,则可能需要将SVG过滤器标记中的任何空格替换为“%20”。

所以现在,掩码div中的所有内容都模糊了。


9
投票

如果您今天正在寻找可靠的跨浏览器方法,那么您将找不到一个好的方法。您拥有的最佳选择是创建两个图像(这可以在某些环境中自动执行),并将它们排列成一个覆盖另一个图像。我在下面创建了一个简单的例子:

http://codepen.io/AmeliaBR/pen/xGuBr
<figure class="js">
    <img src="http://i.imgur.com/3oenmve.png" />
    <img src="http://i.imgur.com/3oenmve.png?1" class="blur" />
</figure>

虽然有效,但即使这种方法也不一定理想。话虽如此,它确实产生了figure.js { position: relative; width: 250px; height: 250px; } figure.js .blur { top: 0; left: 0; position: absolute; clip: rect( 0, 250px, 125px, 0 ); }


8
投票

这是一个可能的解决方案。

HTML

desired result

CSS

<img id="source" src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />

<div id="crop">
    <img id="overlay" src="http://www.byui.edu/images/agriculture-life-sciences/flower.jpg" />
</div>

我知道CSS可以简化,你可能应该摆脱ID。这里的想法是使用div作为裁剪容器,然后在图像的副本上应用模糊。 #crop { overflow: hidden; position: absolute; left: 100px; top: 100px; width: 450px; height: 150px; } #overlay { -webkit-filter:blur(4px); filter:blur(4px); width: 450px; } #source { height: 300px; width: auto; position: absolute; left: 100px; top: 100px; }

要在Firefox中使用此功能,您必须使用Fiddle


3
投票

SVG hack
#bg, #search-bg {
  background-image: url('https://images.pexels.com/photos/719609/pexels-photo-719609.jpeg?w=940&h=650&auto=compress&cs=tinysrgb');
  background-repeat: no-repeat;
  background-size: 1080px auto;
}

#bg {
  background-position: center top;
  padding: 70px 90px 120px 90px;
}

#search-container {
  position: relative;
}

#search-bg {
  /* Absolutely position it, but stretch it to all four corners, then put it just behind #search's z-index */
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  z-index: 99;

  /* Pull the background 70px higher to the same place as #bg's */
  background-position: center -70px;

  -webkit-filter: blur(10px);
  filter: url('/media/blur.svg#blur');
  filter: blur(10px);
}

#search {
  /* Put this on top of the blurred layer */
  position: relative;
  z-index: 100;
  padding: 20px;
  background: rgb(34,34,34); /* for IE */
  background: rgba(34,34,34,0.75);
}

@media (max-width: 600px ) {
  #bg { padding: 10px; }
  #search-bg { background-position: center -10px; }
}

#search h2, #search h5, #search h5 a { text-align: center; color: #fefefe; font-weight: normal; }
#search h2 { margin-bottom: 50px }
#search h5 { margin-top: 70px }

1
投票

我想出了这个解决方案。

<div id="bg"> <div id="search-container"> <div id="search-bg"></div> <div id="search"> <h2>Awesome</h2> <h5><a href="#">How it works »</a></h5> </div> </div> </div>

这是一种使用绝对定位的子Click to view image of blurry effect的技巧,将其背景图像设置为与父div相同,然后使用div CSS属性以及在父元素上设置的相同background-attachment:fixed属性。

然后在子div上应用background(或任何值)。

filter:blur(10px)
*{
    margin:0;
    padding:0;
    box-sizing: border-box;
}
.background{
    position: relative;
    width:100%;
    height:100vh;
    background-image:url('https://images.unsplash.com/photo-1547937414-009abc449011?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
    background-size:cover;
    background-position: center;
    background-repeat:no-repeat;
}

.blur{
    position: absolute;
    top:0;
    left:0;
    width:50%;
    height:100%;
    background-image:url('https://images.unsplash.com/photo-1547937414-009abc449011?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size:cover;
    filter:blur(10px);
    transition:filter .5s ease;
    backface-visibility: hidden;
}

.background:hover .blur{
    filter:blur(0);
}
.text{
    display: inline-block;
    font-family: sans-serif;
    color:white;
    font-weight: 600;
    text-align: center;
    position: relative;
    left:25%;
    top:50%;
    transform:translate(-50%,-50%);
}

<head> <title>Blurry Effect</title> </head> <body> <div class="background"> <div class="blur"></div> <h1 class="text">This is the <br>blurry side</h1> </div> </body> </html>

© www.soinside.com 2019 - 2024. All rights reserved.