模糊背景图像,但保持图像在框区域内聚焦

4
有没有人知道一种 CSS/JavaScript 技术,可以实现以下效果:全屏背景图模糊,但其中一个浮动的固定部分不模糊,同时该部分保持居中和相同大小,即使浏览器窗口大小改变。背景图片需要随着浏览器窗口大小调整大小,但聚焦部分需要保持居中并具有相同的框大小,而其裁剪的背景图与模糊的背景一起调整大小。参见示例图。必须兼容各种浏览器。enter image description here

请查看这个链接。我的答案与Gaby aka G. Petrioli类似,所以我没有再次发布。我使用的是CSS Secrets书籍中的方法,秘密18(毛玻璃效果),第146页 - Alex
非常接近了!我仍然需要保持小盒子的大小固定,并且在页面上居中。您的盒子会随着页面的改变而改变大小。请查看Gaby aka G. Petrioli提供的解决方案,以获取一个完全可用的解决方案。谢谢! - jmux
所以你可以使用 px 代替 vwvh - Alex
4个回答

4
尝试使用两个元素(使用相同的背景图片),但是在两个元素上都设置background-attachmentfixed

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

.blur-group {
  position: relative;
  width: 100%;
  height: 100%;
}

.blurred,
.unblurred {
  background: url('//placekitten.com/1000/750') 50% 50% no-repeat;
  background-size: cover;
  background-attachment: fixed;
}

.blurred {
  width: 100%;
  height: 100%;
  filter: blur(7px);
}

.unblurred {
  width: 400px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -150px;
  border: 10px solid white;
}
<div class="blur-group">
  <div class="blurred"></div>
  <div class="unblurred"></div>
</div>


那个看起来非常不错。我可能会使用两张图片,其中一张在Photoshop中模糊以获得更好的效果。谢谢! - jmux
我基于你的版本制作了一个带有两张图片的版本。我已在Chrome、Safari、Firefox、Internet Explorer和Edge上进行了测试。https://jsfiddle.net/jmfuo/3s232cpz/7/ - jmux

1

请将图像与模糊背景响应式地呈现

标记

<div class="widget center">
  <div class="text center">
    <h1 class="">Responsive Blur</h1>
    <p>Resize me</p>
  </div>
  <div class="blur">
    <img src="https://static.pexels.com/photos/88212/pexels-photo-88212.jpeg" class="bg">
  </div>
</div>

CSS (层叠样式表)
img.bg {
  min-height: 100%;
  width: 100%;
  height: auto;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -2;
    -webkit-filter: blur(18px);
    -o-filter: blur(18px);
    filter: blur(18px);
}

.blur {
  height: 250px;
  width: 100%;
  margin: -20px auto;
  z-index: -1;
  position: relative;
  overflow: hidden;
}

.blur:after {
  content: '';
  height: 100%;
  width: 100%;
  position: absolute;
}

.widget {
  border-top: 2px solid white;
  border-bottom: 2px solid white;
  min-height: 200px;
  width: 45%;
  overflow: hidden;
   background-image: url("https://static.pexels.com/photos/88212/pexels-photo-88212.jpeg");
   -webkit-background-size: cover;
    -moz-background-size: cover;
    -ms-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

.center {
  position: absolute;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}


/*  NOT REQUIRED  */

.text {
  height: 200px;
  width: 340px;
}

.text h1 {
  text-align: center;
  text-shadow: 1px 1px rgba(0, 0, 0, .1);
  color: #ffffff;
  margin-top: 70px;
  font-family: 'Lora', serif;
  font-weight: 700;
  font-size: 38px;
}

.text p {
  text-align: center;
  color: #ffffff;
  text-shadow: 1px 1px rgba(0, 0, 0, .1);
  margin-top: 0px;
  font-family: 'Lato', serif;
  font-weight: 400;
  font-size: 22px;
}

请看这里的结果here

谢谢。不幸的是,在您的示例中,子图像被缩小到其容器大小。我需要它始终与模糊背景保持同步。它必须看起来像正方形正在“聚焦”模糊背景的框定区域。 - jmux
谢谢Sebastian。它还没有完全按比例缩放。请参见上面的Gaby aka G. Petrioli,以获取可行的解决方案。不过还是感谢您的建议。 - jmux

1
您可以使用两次图像,在容器上使用绝对定位,将它们居中并一上一下地排列。然后,模糊第一张图片,并使用clip-path来显示第二张图片的一部分。但是也许clip-path的支持今天还不够满足您的需求 :) https://jsfiddle.net/nesquimo/nnmquv1k/2/
.parent{
  position: relative;
}

.child{
  position: absolute;
  width: 100%;
}

.child:first-child{
   -webkit-filter: blur(5px);
  -moz-filter: blur(5px);
  -o-filter: blur(5px);
  -ms-filter: blur(5px);
  filter: blur(5px);
}

.child:last-child{
  -webkit-clip-path: inset(20% 18% 15% 20%);
  clip-path: inset(20% 18% 15% 20%);
}
<div class="parent">
  <img class="child" src="http://i.imgur.com/RRUe0Mo.png">
  <img class="child" src="http://i.imgur.com/RRUe0Mo.png">
</div>

这大概是我所能做到的了。然而,很遗憾 clip-path 在 Internet Explorer 上不起作用,我相信。而且,clip path 只适用于 inset。当调整窗口大小时,我需要使用 JavaScript 重新调整 inset,以保持盒子的大小和位置。你的示例图比我的更漂亮,哈哈 :-) - jmux

0

使用父容器

  • 为父容器添加背景
  • 在父容器内放置一个 div,该 div 将充当遮罩。将模糊滤镜属性添加到遮罩上
  • 在父容器内再添加另一个 div。这个 div 将包含你的内容。为这个 div 添加背景图像,并使用 css transform 居中这个 div
  • 下面是代码片段

    body {
      position: relative;
      width: 100vw;
      height: 100vh;
    }
    
    #parent {
      position: relative;
      height: 100%;
      width: 100%;
    }
    
    #mask {
      width: 100%;
      height: 100%;
      position: absolute;
      background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSvPdDmWSbJtqGgr8qMOKew03yzJVKc9ZCYDbfzRKTnMrnrfwIsNg);
      background-size: cover;
      -webkit-filter: blur(5px);
      -moz-filter: blur(5px);
      -o-filter: blur(5px);
      -ms-filter: blur(5px);
      filter: blur(5px);
    }
    
    #content {
      position: relative;
      width: 50%;
      height: 50%;
      background: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRney6jjMJ8ZrRKwPb9MNDi7dvF1OLFC78ZpT8Th43WaNnJQV263Q);
      background-size: cover;
      top: 50%;
      left: 50%;
      border: solid white;
      -moz-transform: translate(-50%, -50%);
      -webkit-transform: translate(-50%, -50%);
      -o-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%)
    }
    <div id="parent">
      <div id="mask"></div>
      <div id="content"> This is some content</div>
      <div>


    这很接近了,但是背景图必须随着浏览器窗口大小调整,而焦点区域必须保持居中、相同大小,但其聚焦图像必须与模糊的背景图像一起调整大小。它必须看起来像正方形正在“聚焦”模糊背景的框定区域。 - jmux
    答案已更新,请在此处检查笔 http://codepen.io/repzeroworld/pen/BWXKoL 并调整窗口大小。 - repzero
    不好意思,那还不是。在你的代码中,小框中的图像与大框中的图像比例不同。小框应该显示大框的摘录,但不模糊。就像我上面的示例图像一样。你的示例将未模糊的图像缩放到小框的大小。 - jmux
    @jum,你是错误的...我的例子相对于浏览器的视口宽度和高度缩放背景URL...而且你可以简单地用大图像的URL替换小图像的URL,这样它们就是一样的...你难道不能做到吗? - repzero
    我在你的示例codepen.io/repzeroworld/pen/BWXKoL中确实做到了这一点(使用相同的图像URL),在我写评论之前,但缩放仍然不正确。无论如何还是谢谢。请参见上面的Gaby aka G. Petrioli,那个是有效的。 - jmux

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