Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - How can I animate the opacity of the background of a div?

I have a div #test with a 0 opacity background, I want to animate it until reach the opacity of 0.7. But .animate doesn't seem to work with the css rgba.

My css is:

#test {
    background-color: rgba(0, 0, 0, 0);
}

my html:

<div id="test">
    <p>Some text</p>
    <img src="http://davidrhysthomas.co.uk/img/dexter.png" />
</div>

and my jQuery:

$('#test').animate({ background-color: rgba(0, 0, 0, 0.7) },1000);

Here a jsFiddle: http://jsfiddle.net/malamine_kebe/7twXW/10/

thanks a lot for helping!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all you need to set the property correctly

$('#test').animate({ 'background-color': 'rgba(0, 0, 0, 0.7)' },1000);

then you need to include jquery-ui to animate colours.

http://jsfiddle.net/7twXW/11/

You can also use css transitions to animate background colours

#test {
    background-color: rgba(0, 0, 0, 0);
    -webkit-transition:background-color 1s;
    -moz-transition:background-color 1s;
    transition:background-color 1s;
}

http://jsfiddle.net/7twXW/13/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...