[英]How can I animate the opacity of the background of a div?
我有一個帶有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/
非常感謝你的幫助!
首先,您需要正確設置屬性
$('#test').animate({ 'background-color': 'rgba(0, 0, 0, 0.7)' },1000);
那么你需要包含jquery-ui來動畫顏色。
您還可以使用css過渡來為背景顏色設置動畫
#test {
background-color: rgba(0, 0, 0, 0);
-webkit-transition:background-color 1s;
-moz-transition:background-color 1s;
transition:background-color 1s;
}
使用animate函數時,請不要使用background-color而應使用backgroundColor。 所以這是工作代碼:
$('#test').animate({ backgroundColor: "rgba(0,0,0,0.7)" });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.