您的位置:首页 > Web前端 > JavaScript

Javascript利用封装实现,多个部分的随机变色

2020-04-01 12:19 686 查看

随机变色

//随机变色
btn.onclick = function (){
setRandomColor(body,"red");
//实现body的随机变色
setRandomColor(btn,"green");
//实现button的随机变色
setRandomColor(box,"bule");
//实现box的随机变色
}
function setRandomColor(ele,color){
var r = color === "red" ? 255:parseInt(Math.random()*256);
// 生成 r , 0~255的随机数 ,若color传的参数为red;则把r 值设为 255
var g = parseInt(Math.random()*256);
// 生成 g , 0~255的随机数
var b = parseInt(Math.random()*256);
// 生成 b , 0~255的随机数
var random_color = "rgb(" + r + "," + g +"," + b + ")";
//rgb( r , g , b)的具体颜色
ele.style.cssText = "background:" + random_color;
//实现css样式的变换
}
  • 点赞 1
  • 收藏
  • 分享
  • 文章举报
Jmytea 发布了4 篇原创文章 · 获赞 6 · 访问量 201 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: