1

I would like to use CSS filters in JavaScript.

Example of CSS filters:

.filter {
    -webkit-filter: blur(20px); /* Blur filter */
    -webkit-filter: invert(1); /* Invert filter */
    -webkit-filter: hue-rotate(100deg); /* Rotate Hue */
    ...
}

For example, how can I make a blurred image using JavaScript?

<img id="image" onmouseover="ImageHover();" src="source.png"/>

while

function ImageHover() {
    document.getElementById('image').??????='???'
}

I hope you guys understand what I want.

3
  • Are you using jQuery? Commented Jul 25, 2015 at 0:46
  • No, I'm not using jQuery. Commented Jul 25, 2015 at 0:46
  • In addition to Ahmed's answer I'd strong advise against inline scripting whenever possible. Commented Jul 25, 2015 at 2:49

3 Answers 3

0

I recommend using this so the function can be used with any element/Img you want later

<img id="image" onmouseover="ImageHover(this);" src="source.png"/>

function ImageHover(el){
    el.classList.add("filter");
};
Sign up to request clarification or add additional context in comments.

4 Comments

you didn't declare the handler of onmouseout ,, so nothing goes back to it's normal state
yes I know, but the function doesnt blur the image, or you see the blur of image?
I have updated the fiddle : jsfiddle.net/kn9h02a5/1 the problem was that you added the class like the way you added an inline style ,, I've fixed this by addung new css rule and added the class to the class list of that element
Thank you :) Just exactly what I wanted.
0

You need to give use to that element's property, as such:

var image = document.getElementById('image');
image.style.webkitFilter = "blur(20px);";

or

image.style["-webkit-filter"] = "blur(20px);";

Comments

-1
var photo = document.getElementById('yourId');

function sepia(){
    photo.style.filter = "sepia(100%)";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.