使用CSS模糊部分背景图像

12

我使用这个解决方案来使背景图模糊,效果很好!但我想在背景上放一些 <div>,并让它们变模糊。因此,只有在 <div> 下方的背景应该是清晰和模糊的。如果我将模糊滤镜移动到那个 <div> 上,它会变得模糊,但背景图仍然保持清晰。

HTML 很简单:

<body>
   <div class = "background"></div>
   <div class = "content"></div>
</body>

CSS 是:

.content{
    width: 70%;
    height: 70%;
    border:2px solid;
    border-radius:20px;
    position: fixed;
    top: 15%;
    left: 15%;
    z-index:10;
    background-color: rgba(168, 235, 255, 0.2);
    filter: blur(2px); 
    -webkit-filter: blur(2px);
    -moz-filter: blur(2px);
    -o-filter: blur(2px); 
    -ms-filter: blur(2px);
}
.background{
    width:100%;
    height:100%;
    background-image:url('http://www.travel.com.hk/region/time_95.jpg');
    z-index:0;
    position:absolute;
}

这里有一个例子 http://jsfiddle.net/photolan/8gVGR/

所以我需要使用CSS仅模糊内容div下的背景。

2个回答

12

如果需要动态的话,可能会遇到一些麻烦,但你可以从下面这个开始:

HTML

<div class="background"></div>
<div class="mask">
    <div class="bluredBackground"></div>
</div>
<div class="content"></div>

CSS

.content {
    width: 70%;
    height: 70%;
    border:2px solid;
    border-radius:20px;
    position: fixed;
    top: 15%;
    left: 15%;
    z-index:10;
    background-color: rgba(168, 235, 255, 0.2);
}
.background {
    width:100%;
    height:100%;
    background-image:url('http://www.travel.com.hk/region/time_95.jpg');
    z-index:2;
    position:fixed;
}
.bluredBackground {
    width:100%;
    height:100%;
    display:block;
    background-image:url('http://www.travel.com.hk/region/time_95.jpg');
    z-index:1;
    position:absolute;
    top:-20%;
    left:-20%;
    padding-left:20%;
    padding-top:20%;
    -webkit-filter: blur(2px);
}
.mask {
    width: 70%;
    height: 70%;
    border:2px solid;
    border-radius:20px;
    position: fixed;
    top: 15%;
    left: 15%;
    z-index:10;
    overflow:hidden;
}

FIDDLE


哇!非常好用!对于这么难的任务来说,这是一个简单的解决方案! - Ruslan Abyshov

-1

.background div 中使用 -webkit-filter: blur(2px),而不是在 .content div 中使用。

这是您更新后的代码:

.content{
width: 70%;
height: 70%;
border:2px solid;
border-radius:20px;
position: fixed;
top: 15%;
left: 15%;
z-index:10;
background-color: rgba(168, 235, 255, 0.2);
filter: blur(2px); 
-moz-filter: blur(2px);
-o-filter: blur(2px); 
-ms-filter: blur(2px);
}

.background{
width:100%;
height:100%;
background-image:url('http://www.travel.com.hk/region/time_95.jpg');
z-index:0;
position:absolute;
 -webkit-filter: blur(2px);
}

你想要让背景图片模糊还是内容div模糊? - Joke_Sense10
我想在 .content div 下使背景图模糊。 - Ruslan Abyshov
不行。我只想在内容div下面使背景模糊。因此,我想使用内容div作为玻璃效果的遮罩层覆盖在背景上。 - Ruslan Abyshov

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