10

我有一个不透明度为 0 背景的 div #test,我想对其进行动画处理,直到不透明度达到 0.7。但是 .animate 似乎不适用于 css rgba。

我的CSS是:

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

我的html:

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

和我的 jQuery:

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

这里有一个 jsFiddle:http: //jsfiddle.net/malamine_kebe/7twXW/10/

非常感谢您的帮助!

4

2 回答 2

15

首先你需要正确设置属性

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

那么你需要包含 jquery-ui 来为颜色设置动画。

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

您还可以使用 css 过渡来为背景颜色设置动画

#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/

于 2013-04-27T17:27:39.117 回答
1

使用动画功能时不要使用背景颜色,而是使用背景颜色。所以这是工作代码:

$('#test').animate({ backgroundColor: "rgba(0,0,0,0.7)" });
于 2014-08-29T14:58:19.573 回答