使用Javascript编写代码实现对象颜色随机变换。 示例代码:
//获取对象 const object = document.querySelector('.object');
//定义随机颜色函数
function randomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return rgb(${r}, ${g}, ${b})
;
}
//定时器,每秒变换一次颜色 setInterval(function() { object.style.backgroundColor = randomColor(); }, 1000);