开发者

How to make :active state work in IE?

I have a button in my html form and need to change it's background image when it is clicked using css. it works perfect in FF but it seems that IE doesnt support :active state.

Here is my code:

HTML:

<button class='button'>Click Me</button>

CSS:

.button {
    wid开发者_如何学JAVAth: 118px;
    height: 33px;
    background: url(/images/admin/btn.png) no-repeat center top;
    border: none;
    outline: none;
}
.button:active {
    background-position: center bottom;
}


This is a known bug in earlier versions of IE (I think they solved it in IE8). I usually solve this (as well as the corresponding "hover" problem) with javascript. I attach two event handlers to the element -- "mousedown" to set an additional class (something like "button-active") and "mouseup" to remove the class. In jQuery it would be something like this:

$('.button').mousedown(function() { $(this).addClass('button-active'); });
$('.button').mouseup(function() { $(this).removeClass('button-active'); });

Then, just add that class to the css rule, like so:

.button:active, .button-active {
    background-position: center bottom;
}

A little ugly, yes, but what do you expect -- it's Internet Explorer. It can't be pretty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜