Web动画,如何为背景rgba颜色设置动画

时间:2019-09-02 03:56:33

标签: javascript css web-animations

我想为模式中的rgba设置背景色的动画,以便它淡入。我不能使用不透明度作为模式中的div,我不想成为透明的。我不能使用Jquery,但我正在使用React。

我没有收到下面的错误提示

const modal = document.getElementById('modal');
modal.animate(
[
  {backgroundColor: 'rgba(255, 0, 0, 0)'}, 
  {backgroundColor: 'rgba(255, 0, 0, 0.8)'}
], 
  {duration: 200, easing: 'ease-in-out', fill: 'forwards'}
);

1 个答案:

答案 0 :(得分:0)

使用setInterval函数更改javascript中的颜色。

var change=0;
var colorEvent= setInterval("changeColor();", 50);
function changeColor(){
  if(change<100){           
      document.getElementById("modal").style.backgroundColor="rgb(255,0,"+change+")";
  }
  else 
    clearInterval(colorEvent);
  change+=1;
  console.log(change);
}
#modal{
    height:50px;
    background-color:rgb(255,0,0);
}
<div id="modal"></div>

或更改不透明度

var change=0.0;
if(change<0.8){
  setInterval(function(){
      document.getElementById("modal").style.backgroundColor="rgb(255,0,0,"+change+")";
      change+=0.01;
  }, 10);
}
#modal{
    height:50px;
    background-color:rgb(255,0,0,0);
}
<div id="modal"></div>