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

大兔子大兔子 提交于 2019-11-29 09:51:31

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/

Ahmad

When using animate function don't use background-color but backgroundColor instead. So here is the working code :

$('#test').animate({ backgroundColor: "rgba(0,0,0,0.7)" });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!