【问题标题】:Blurr background image, but keep image focussed in box area模糊背景图像,但保持图像集中在方框区域
【发布时间】:2018-03-08 00:42:19
【问题描述】:

有没有人知道一种 css/javascript 技术,它可以执行以下操作: 全屏背景图像模糊,但浮动固定,该图像的一部分未模糊,但该部分保持居中并且在浏览器窗口调整大小时保持相同大小。 背景图像需要随着浏览器窗口调整大小,但焦点部分需要保持居中并具有相同的框大小,而其剪裁的背景图像会与模糊背景一起调整大小。见示例图像。 必须是跨浏览器兼容的。

【问题讨论】:

  • 检查this。我的回答类似于 Gaby aka G. Petrioli,所以我没有再发帖。我使用这个方法来自CSS Secrets book,秘密18(磨砂玻璃效果),page 146
  • 非常接近!我仍然需要保持小框的大小固定并在页面上居中。你的调整页面大小。请参阅 Gaby aka G. Petrioli 的解决方案,了解功能齐全的解决方案。谢谢!
  • 所以你可以用px代替vwvh

标签: javascript css image


【解决方案1】:

尝试使用两个元素(在两者上使用相同的背景图像),但在两者上都将 background-attachment 设置为 fixed

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 中模糊以获得更好的效果。谢谢!
  • 我根据你的图片制作了一个包含两张图片的版本。我已经在 Chrome、Safari、Firefox、Internet Explorer 和 Edge 上对其进行了测试。 jsfiddle.net/jmfuo/3s232cpz/7
【解决方案2】:

您可以使用图像两次,将它们居中,使用绝对位置在容器上一个在另一个之上。然后,模糊第一个并使用剪辑路径显示第二个的一部分。但也许今天的剪辑路径支持不足以满足您的需要:) 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 上不起作用。此外,剪辑路径仅适用于插图。在调整窗口大小时,我必须使用 javascript 重新调整插图以保持框的大小和位置。不过,您的示例图像比我的要好:-)
【解决方案3】:

使图像响应模糊背景

标记

<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

【讨论】:

  • 谢谢。不幸的是,在您的示例中,子图像被缩小到它的容器。我总是需要它与模糊的背景同步。它必须看起来像正方形正在“聚焦”模糊背景的框架区域。
  • 谢谢塞巴斯蒂安。它还没有完全正确地缩放。有关可行的解决方案,请参见上面的 Gaby aka G. Petrioli。不过,感谢您的建议。
【解决方案4】:

使用父容器

  • 为父容器添加背景
  • 在父容器中放置一个 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>
  • 【讨论】:

    • 非常接近,但是背景图像必须随着浏览器窗口调整大小,并且焦点区域必须保持居中,大小相同,但其焦点图像必须与模糊背景图像一起调整大小。它必须看起来像正方形正在“聚焦”模糊背景的框架区域。
    • 在此处回答更新的检查笔codepen.io/repzeroworld/pen/BWXKoL 并调整窗口大小
    • 不,很遗憾仍然不是。在您的代码中,小框中的图像与大框中的图像比例不同。小框应该显示大框的摘录,但不能模糊。就像我上面的示例图片一样。您的示例将未模糊的图像缩放到小盒子的大小。
    • @jum 你不正确...我的示例相对于浏览器的视口宽度和高度缩放背景 url...您也可以简单地将小图像的 url 替换为大图,所以它们是一样的......你很难替换它吗?
    • 我在您的示例 codepen.io/repzeroworld/pen/BWXKoL 中正是这样做的(使用相同的图像 url),因为我写了我的评论,但缩放仍然不正确。不管怎么说,还是要谢谢你。请参阅上面的 Gaby aka G. Petrioli,那个正在工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2021-06-08
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多