简体   繁体   English

如何使用 Javascript 模糊背景图像?

[英]How do I use Javascript to blur a background image?

My users upload background images.我的用户上传背景图片。 These images are stored in CDN on Amazon S3 (not on the same server).这些图像存储在 Amazon S3 上的 CDN 中(不在同一台服务器上)。

Currently, the way I do the background is this.目前,我做背景的方式是这样的。

<div id="background"></div>

It's position fixed, and I use javascript to set the background-image.它的位置是固定的,我使用 javascript 来设置背景图像。

I would like to BLUR this background on the client side.我想在客户端模糊这个背景。 How can I do this?我怎样才能做到这一点?

Blur.js does not work with an image on Amazon CDN. Blur.js 不适用于 Amazon CDN 上的图像。

You could always try using the Pixastic library (as long as you don't mind using HTML5 features):您可以随时尝试使用 Pixastic 库(只要您不介意使用 HTML5 功能):

http://www.pixastic.com/lib/ http://www.pixastic.com/lib/

You can use css filters to do this, but only in super new browsers.您可以使用 css 过滤器来做到这一点,但只能在超级新浏览器中使用。 That and a combo of SVG filters will get you a few browsers.这和 SVG 过滤器的组合将为您提供一些浏览器。 I guess you could then fall back to canvas, and excanvas to get almost all browsers.我想你可以回退到 canvas 和 excanvas 来获得几乎所有的浏览器。

Really, I'd just use filters, in a year or so most users will have them.真的,我只是使用过滤器,大约一年后,大多数用户都会拥有它们。

Currently, you can use style.filter, see example below:目前,您可以使用 style.filter,请参见下面的示例:

var element = document.getElementById("background");

element.style.filter = "blur(0px)";

Tested on Chrome Version 86.0.4240.198 (Official Build) (x86_64) and it works.在 Chrome 版本 86.0.4240.198 (Official Build) (x86_64) 上测试,它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM