dnn
dnn

Reputation: 57

CSS/HTML: Remove image filter on click

I use some images as links on my site. When I hover the mouse over one of the images, they go from gray to color - and they go back to gray when the mouse is no longer hovering.

I want the image to stay colorized when activated. Say I click the image, it should stay colorized until another link is clicked/activated.

This is my code:

HTML:

<p><a href="privileged.php" target="testframe">
<img class="grayscale" height="67" src="powerlogo.png" width="303">
</a></p>
<iframe name="testframe"></iframe>

CSS:

img.grayscale {
  filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale"); /* Firefox 3.5+ */
  filter: gray; /* IE6-9 */
  -webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
}

a:hover img, a:focus img {
    filter: none;
    -webkit-filter: grayscale(0%);
}

The above works as intended in Firefox, but not in Chrome and IE.

What am I doing wrong?

Upvotes: 3

Views: 13964

Answers (1)

yckart
yckart

Reputation: 33408

You can add/toggle a class with javascript to your element and style this with css:

$("a").click(function(){
  $("a").removeClass("active");
  $(this).addClass("active");
});

a:hover img,
a:focus img,
a.active img {
  filter: none;
}

http://fiddle.jshell.net/3uYDc/

Upvotes: 6

Related Questions