0

I have an element on my page that has different styles if the user is hovering over it or clicking it. Normally, the image is a snowflake. When the mouse is over it, the snowflake image is replaced with the same snowflake glowing blue, and when it's active, the image is replaced with the same glowing snowflake, but slightly smaller. This creates the illusion that the snowflake glows when hovered, and shrinks when the mouse is clicked.

However, I want to make it so the snowflake changes its look depending on the score. This is what I have for that:

var snowflakeBG = document.getElementById("MainSnowFlake");

changeFlakeImage = function(){
    if (mainScore >= 100){
        snowflakeBG.style.backgroundImage = "url(Images/Snowflake100BG.png)";
    }

This works just fine to replace the normal snowflake with the "snowflake100BG" once the user's score reaches 100. The problem is, I don't know how to also change the elements :hover and :active. Could someone please explain?

  • 1
    You can't change CSS pseudo classes with javascript, but just use classes with different CSS, and add and remove classes instead. – adeneo Nov 23 '13 at 22:00
  • 1
    duplicate: http://stackoverflow.com/questions/4481485/changing-css-pseudo-element-styles-via-javascript – markasoftware Nov 23 '13 at 22:01
  • 1
    Can you add another class, eg, .snowflake100, then override all the styles in the CSS file? – Douglas Nov 23 '13 at 22:02
  • @adeneo Not directly, but you can [manipulate the CSS](http://stackoverflow.com/a/17454470/1169519) itself, a bit hackish solution though. – Teemu Nov 23 '13 at 22:18
  • @Teemu - indeed, but that answer sucks balls, it would be much easier to just insert a style tag into the head that overwrites the current styles, and give that style tag an ID so it can be easily removed, rather than accessing and changing the stylesheets, but then again, it would be even easier to just use classes. And, none of the above targets CSS psuedo classes, as that can't be done. – adeneo Nov 23 '13 at 22:21
  • @adeneo I agree, changing class would be a solution here, I just wanted to point out, that you can access pseudo classes also with JS, in a hackish way though : ). – Teemu Nov 23 '13 at 22:28

0 Answers0