简体   繁体   English

动态应用CSS过滤器

[英]Dynamically apply CSS filter

How to dynamically apply CSS filters? 如何动态应用CSS过滤器? I have tried the following for Chrome. 我为Chrome尝试了以下内容。

image.style = "-webkit-filter: brightness(50%);";

You should set value to the webkitFilter property, not to the style object. 您应该将值设置为webkitFilter属性,而不是style对象。 This syntax will work : 此语法将起作用

image.style.webkitFilter = "brightness(50%)";

If you don't know JavaScript property name, you can reference it by CSS property (like karaxuna suggested, will work too ): 如果您不知道JavaScript属性名称,可以通过CSS属性引用它(如karaxuna建议, 也可以 ):

image.style["-webkit-filter"] = "brightness(50%)";
image.style["-webkit-filter"] = "brightness(50%)";

Add that filter to a class: 将该过滤器添加到类:

.bright {
    -webkit-filter: brightness(50%);
}

And toggle that class: 并切换该类:

image.classList.toggle('bright');

1) Check if browser supports css-filters (if needed) 1)检查浏览器是否支持css-filters(如果需要)
2) Detect if "filter" property needs vendor in current browser and save it 2)检测“过滤器”属性是否需要当前浏览器中的供应商并保存
3) Set filter value in format: 3)以格式设置过滤值:

el.style["-vendor-filter"] = value;

or 要么

el.style.vendorFilter = value;

Check small demo for it: http://codepen.io/malyw/pen/EDnmt 检查小型演示: http//codepen.io/malyw/pen/EDnmt
Also you can find large demo showing css filters in action: http://malyw.github.io/css-filters/ 您还可以找到显示css过滤器的大型演示: http//malyw.github.io/css-filters/

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

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