0

I want to create a hover effect for an element but want the pre-hover CSS for the element to be the same as before, when hovering out again. So, I don't want to inadvertantly override prior styles when hovering out of the element again. In other words - an easy way to memoize the css state of an element and to restore that state later.

Ideas?

Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63

3 Answers3

2

Trigger your hover CSS by applying a specific class to the elements.

Then when your hover is complete remove the hover class.

This lets CSS handle the style overriding and restoration for you.

Sly_cardinal
  • 12,270
  • 5
  • 49
  • 50
  • Thanks! Answer rewarded to sha404 because he was a minute faster. But thanks to both of you for quick and very useful answers! :-) – Bent Rasmussen May 06 '12 at 09:29
2

just create a new css class. and on hovering, use addClass(_thatclass_) and when hovering out use removeClass(_thatclass_)

sha256
  • 3,029
  • 1
  • 27
  • 32
2

Can you do it in pure CSS, using :hover? If you can you should, and it restores the old CSS when not hovering automatically.

See this example, all divs have a border that gets bigger when you hover, and shrinks back to normal when you stop hovering. I'll put the CSS here, so this answer is self-contained:

div {
   border: 2px solid red;
}

div:hover {
  border-width: 10px;
}

(Note that you can use hover anywhere within your selectors, not just at the end e.g. #myID:hover .childClass)

huon
  • 94,605
  • 21
  • 231
  • 225
  • No, because I want to create a hover effect on another element when hovering over a given element. But good suggestion given lack of context. – Bent Rasmussen May 06 '12 at 09:27