开发者

jQuery animate CSS, set back hover state

开发者 https://www.devze.com 2023-01-21 08:51 出处:网络
I have an update button on which I want to pull attention when updating my content. I am doing this with an animation color fade and adding some arrow characters.

I have an update button on which I want to pull attention when updating my content. I am doing this with an animation color fade and adding some arrow characters.

When the animation is开发者_运维知识库 finished I want my CSS :hover states back.

Is this possible or do I lose the original CSS and do I have to reset these with jQuery's hover()

Fiddle: http://jsfiddle.net/K6fd2/

CSS

a.btn { padding: 5px 20px; color: white; background-color: #4188FB; } 
a.btn:hover { background: #FFCC00;}

HTML

<a href="#" class="btn">Update</a>
<br /><br />
<a href="#" class="change">change content</a>

JS

var oriBtnTxt = $(".btn").html(); // store original text

$(".change").click(function() {
    $(".btn")
    .css("background-color","#FFCC00")
    .html(oriBtnTxt + " &raquo;")
    .animate({backgroundColor: "#4188FB"}, 2000, "swing", function() {
        // set back hover state
        $("a.btn:hover").css("background-color", "#FFCC00");
    });
})


This may not be the most elegant solution, but you could try the following:

$(this).removeAttr('style');

within your $.animate() callback function.

Edit: I suspect that you're losing your :hover styles because $.animate() uses inline styles, which are set after stylesheet rules are evaluated; thus they take precedence over defined styles within the stylesheet.

0

精彩评论

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