【问题标题】:Is it possible to use -webkit-filter: blur(); on background-image?是否可以使用 -webkit-filter: blur();在背景图像上?
【发布时间】:2012-10-21 12:18:52
【问题描述】:

是否可以在背景图像上使用-webkit-filter: blur();

我已经尝试过这段代码,它只是模糊了除背景图像之外的所有内容:

body {
  background-image: url('http://www.publicdomainpictures.net/pictures/10000/velka/pebbles-and-sea-11284647414Rbeh.jpg');
  background-attachment: fixed;
  -webkit-filter: blur(5px);
}

我已经搜索了一段时间,但找不到任何描述如何执行此操作的资源。

【问题讨论】:

    标签: css


    【解决方案1】:

    不要将其应用于正文,将其应用于如下全尺寸容器,将容器大小和位置设置为绝对,然后将其余内容设置为相对并设置 z-indexes。

    ​<body>
    <div class="bgImageContainer">
    </div>
    <div class="content"> Some text and stuff here</div>
    </body>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    
    .bgImageContainer{
        background-image:url('http://www.whitegadget.com/attachments/pc-wallpapers/16950d1224057972-landscape-wallpaper-blue-river-scenery-wallpapers.jpg'); 
        width:500px;
        height:400px;
        -webkit-filter: blur(5px);
        z-index:0;
        position:absolute;
    }
    .content{
        z-index:10;
        position:relative;
    }
    

    编辑 - 更新了小提琴,旧图像不再工作。

    http://jsfiddle.net/Yr2zD/1130/

    【讨论】:

    • 这可行但不漂亮,我希望有一种更简单的方法。感谢分享!
    • 有没有人试过用全屏背景图片来做这个。Chrome 的性能严重下降。
    • 这种方法会对 Android 浏览器造成巨大的性能影响,因为背景上每个对象的绘制操作需要更长的时间
    • 拒绝一个 2 岁的答案,因为它在当前的浏览器上不能很好地运行......
    • 不在哪里工作?在 Chrome 中运行良好。小提琴中的图像不起作用,但代码仍然有效。用新图像更新了小提琴。 @Dev_Man
    【解决方案2】:

    未来用户:IE7、IE8、IE9、IE10、IE11 或当前版本的 EDGE 不支持此功能。

    请勿在真实网站上使用,除非您有后备方案。

    如果您正在寻找不依赖额外标记的解决方案,您可以将背景图像和过滤器应用到正文上的伪元素。

    body {
        position: absolute;
        top: 0; bottom: 0; left: 0; right: 0;
        height: 100%;
    }
    body:before {
        content: "";
        position: absolute;
        background: url(http://lorempixel.com/420/255);
        background-size: cover;
        z-index: -1; /* Keep the background behind the content */
        height: 20%; width: 20%; /* Using Glen Maddern's trick /via @mente */
    
        /* don't forget to use the prefixes you need */
        transform: scale(5);
        transform-origin: top left;
        filter: blur(2px);
    }
    

    查看this JsFiddle

    【讨论】:

    • 如果您不关心 IE7 或更早版本的支持,则可以使用。
    • @RickCalder 认真的吗?是关于Webkit过滤器的,连IE11都不支持。
    • 我不认为较小的宽度/高度然后进行转换是必要的。它在使用全屏 GIF 时很有用,因为必须在每一帧都重新绘制画布,但将单帧图像用作背景,这不会造成任何重大的 CPU 问题。如果使用单帧图像获得任何收益,它们应该是最小的 - 尽管我还没有测试过。
    • Edge 14 现在支持过滤器。
    【解决方案3】:

    如果整个页面的背景,那么这对我来说是最好的解决方案。

    body {
        //background-color: black;  use it if you don't want the background border
    }
    body:before {
        content: '';
        position: fixed;
        width: 100vw;
        height: 100vh;
        background-image: url('http://www.publicdomainpictures.net/pictures/10000/velka/pebbles-and-sea-11284647414Rbeh.jpg'); // your image
        background-position: center center;
        background-repeat: no-repeat;
        background-position: 100% 100%;
        background-size: cover;
        filter: blur(2px);
        z-index: -9;
    }
    

    【讨论】:

    • 这是一个很好的答案!谢谢。虽然为什么会有边界? -- 编辑我得到了答案,这都是因为 blur()。可以使用 transform: scale(1.1);删除边框stackoverflow.com/questions/28870932/…
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多