Сделай так чтобы в этом коде js цвета медленно перемещались вправо и менялись цвета код Define an array of colorsvar colors = #FF0000 #00FF00 #0000FF #FFFF00 #00FFFF #FF00FF #C0C0C0 #808080
// Define an array of colors var colors = [ "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#00FFFF", "#FF00FF", "#C0C0C0", "#808080", "#800000", "#808000", "#008000", "#800080", "#008080", "#000080", "#FFFFFF", "#000000" ];
// Function to get a random color from the array function getRandomColor() { var randomIndex = Math.floor(Math.random() * colors.length); return colors[randomIndex]; }
// Function to change the background color of the document function changeBackgroundColor() { var color1 = getRandomColor(); var color2 = getRandomColor(); var gradient = "linear-gradient(to right, " + color1 + ", " + color2 + ")"; document.body.style.background = gradient; }
// Call the changeBackgroundColor function every 5 seconds var interval = setInterval(function() { changeBackgroundColor(); }, 1000);
// Function to animate the color gradient function animateGradient() { var currentColor = 0; var gradientInterval = setInterval(function() { currentColor++; if(currentColor == colors.length) { currentColor = 0; } var color1 = colors[currentColor]; var color2 = colors[(currentColor + 1) % colors.length]; var gradient = "linear-gradient(to right, " + color1 + ", " + color2 + ")"; document.body.style.background = gradient; }, 100); }
// Call the animateGradient function after 5 seconds setTimeout(function() { clearInterval(interval); animateGradient(); }, 5000);
In this code, I added a new function called "animateGradient" which uses setInterval to slowly animate the background color gradient. The function starts after 5 seconds using setTimeout and clears the original interval used to change the background color every second. The animateGradient function cycles through each color in the array and creates a new gradient every 100 milliseconds
原文地址: https://www.cveoy.top/t/topic/cb2l 著作权归作者所有。请勿转载和采集!