whatever:hover, fast and easy
July 19th, 2005
- Category:
- CSS,
- Web Development
A way to easily implement whatever:hover in your IE/Win specific style sheet (you know, you should use Conditional Comments) is using Dynamic Properties.
Have a look at the following snippet:
.whatever {
background-color: expression('#808080',
this.onmouseover = function() { this.runtimeStyle.backgroundColor = '#eaeaea'; },
this.onmouseout = function() { this.runtimeStyle.backgroundColor = '#808080'; });
}
It looks a bit odd, but it works! Here is a little demo page.
I like this solution a lot, because using htc-files always ended up in a hassle for me. And I like beating IE with his own weapons.
Update: It is of course better to avoid having to define the same styles in two different style sheets. One should use a class and just change the class attribute on mousover/mouseout. Here is an complete example (taken from the demo page) – first the CSS for all browsers:
.whatever {
background: #808080;
}
.whatever:hover, .whateverhover {
background: #eaeaea;
}
IE's extra CSS:
.whatever {
background-color: expression(this.runtimeStyle.backgroundColor,
this.onmouseover = function() { this.className += ' whateverhover'; },
this.onmouseout = function() { this.className = this.className.replace('whateverhover', ''); });
}
Update: I got a tip at the css-discuss mailing list, that one can even leave out the first part of the expression – this.runtimeStyle.backgroundColor:
.whatever {
background-color: expression(
this.onmouseover = function() { this.className += ' whateverhover'; },
this.onmouseout = function() { this.className = this.className.replace('whateverhover', ''); });
}
Thanks Brett!
Update: I got another hint from the css-d mailing list: The described technique fails in IE 5/Win, probably because of the braces { } inside the declaration.
Fortunately there is another way to create an anonymous function in JavaScript:
var myFunction = new Function([param1[, param2[,...paramN]]], "statement1[; ...statementN;"]);
That way, whatever:hover – fast and easy – works in that old browser as well:
.whatever {
background-color: expression(
this.onmouseover = new Function("this.className += ' whateverhover';"),
this.onmouseout = new Function("this.className = this.className.replace('whateverhover', '');")
);
}
One last thing to mention: The technique does not work in IE/Mac. That browser simply does not support Dynamic Properties.
20 Comments
Comments feed
Comment by Anonymous, November 11th, 2005 at 1:44 pm
Author Comment
Comment by Klaus, February 17th, 2006 at 6:22 pm
Author Comment
Comment by Klaus, February 18th, 2006 at 11:53 pm
Comment by peter, May 28th, 2006 at 12:46 pm
Author Comment
Comment by Klaus, June 5th, 2006 at 5:40 pm
Comment by los, June 16th, 2006 at 11:20 pm
Comment by los, June 28th, 2006 at 9:20 am
Author Comment
Comment by Klaus, June 28th, 2006 at 11:44 pm
Comment by Cameling, September 13th, 2006 at 1:55 am
Author Comment
Comment by Klaus, September 13th, 2006 at 8:03 am
Comment by Cameling, September 14th, 2006 at 8:22 pm
Author Comment
Comment by Klaus, September 15th, 2006 at 8:31 pm
Comment by lkehoe, November 13th, 2006 at 5:55 pm
Comment by lkehoe, November 13th, 2006 at 6:00 pm
Author Comment
Comment by Klaus, November 13th, 2006 at 9:44 pm
Comment by lkehoe, November 14th, 2006 at 9:21 pm
Author Comment
Comment by Klaus, November 15th, 2006 at 8:43 am
Comment by uni, March 19th, 2008 at 10:06 am
Author Comment
Comment by Klaus, March 19th, 2008 at 10:47 am
Comment by Pathfinder, March 29th, 2008 at 7:21 am