简体   繁体   English

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

[英]Web Animation, how to animate the background rgba color

I want to animate the background color in rgba for a modal so it fades in. I cant use opacity as the div inside the modal I dont want to be transparent. 我想为模式中的rgba设置背景颜色的动画,以便它淡入。我不能将不透明度用作模式中的div,我不想成为透明的。 I cant use Jquery but I am using React. 我不能使用Jquery,但我正在使用React。

I'm not getting an error by the below does not work 我没有收到以下错误,但不起作用

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'}
);

Use setInterval function to change color in javascript. 使用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> 

or changing opacity 或更改不透明度

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM