Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically apply CSS filter

How to dynamically apply CSS filters? I have tried the following for Chrome.

image.style = "-webkit-filter: brightness(50%);";
like image 678
Jacob Avatar asked Feb 13 '13 08:02

Jacob


3 Answers

You should set value to the webkitFilter property, not to the style object. 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):

image.style["-webkit-filter"] = "brightness(50%)";
like image 110
Pavlo Avatar answered Nov 08 '22 17:11

Pavlo


image.style["-webkit-filter"] = "brightness(50%)";
like image 29
karaxuna Avatar answered Nov 08 '22 17:11

karaxuna


Add that filter to a class:

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

And toggle that class:

image.classList.toggle('bright');
like image 45
Blender Avatar answered Nov 08 '22 18:11

Blender