Looking to create a smooth background color transition using only CSS3? Check out this simple animation example below:
// Selecting the square element
var square = document.querySelector('.square');
// Initializing the yellow color percentage
var percentYellow = 0;
// Function to change the background color gradually
function changeBackground(){
percentYellow = percentYellow + 10;
// Applying linear gradient background
square.style.background = 'linear-gradient(90deg, yellow '+percentYellow+'%, blue 0)';
if(percentYellow <= 100){
setTimeout(changeBackground, 200);
}
}
changeBackground();
.square{
width: 300px;
height: 300px;
background: linear-gradient(90deg, yellow 0%, blue 0);
border: 1px solid;
}
<div class="square"></div>