level 8
圆形随机出现随机运动,这个成功了 但就是随机颜色不成功,求大佬赐教,拜托了
function randColor(){//封装随机颜色的方法
var r = randRange(0,255);
var g = randRange(0,255);
var b = randRange(0,255);
var a = randRange(0,1);//透明度
return "rgba("+r+","+g+","+b+","+a+")";
}
2018年07月31日 09点07分
1
level 8
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<style type="text/css">
div{
position:absolute;
border-radius:50%;
}
</style>
</head>
<body>
<div></div>
<script type="text/javascript" src ="ballon.js"></script>
<script type="text/javascript">
//创建一个气球对象
var b = new ballon();
//绘制气球
b.drawBallon(document.body);
b.run();
</script>
</body>
</html>
2018年07月31日 09点07分
3
level 8
function ballon(){//定义一个气球对象
this.div = document.createElement("div");
this.r = randRange(20, 80); //半径//Math.random()*60+20=20~80
//初始位置
this.left = randRange(0,1000);
this.top = randRange(0,600);
this.bg = randColor();//背景颜色
document.write(this.bg);
//运行速度
this.speex = randRange(-3,3);
this.speey = randRange(-3,3);
}
//绘制气球
ballon.prototype.drawBallon = function(parent){
var sty = this.div.style;
sty.width = this.r * 2 + "px";
sty.height = this.r * 2 + "px";
sty.left = this.left + "px";
sty.top = this.top + "px";
sty.background = this.bg;
parent.appendChild(this.div);
}
ballon.prototype.run = function(){
var ts= this;
setInterval(function(){
//获取移动的距离
var left = ts.div.offsetLeft + ts.speex;
var top = ts.div.offsetLeft + ts.speey;
//开始移动
ts.div.style.left = left + "px";
ts.div.style.top = top + "px";
},20);
}
function randRange(min,max){//封装一个指定范围的随机函数
return Math.random()*(max-min)+min;
}
function randColor(){//封装随机颜色的方法
var r = randRange(0,255);
var g = randRange(0,255);
var b = randRange(0,255);
var a = randRange(0,1);//透明度
return "rgba("+r+","+g+","+b+","+a+")";
}
2018年07月31日 09点07分
4
level 6
function randColor(){//封装随机颜色的方法
var r = randRange(0,255);
r=Math.floor(r);
var g = randRange(0,255);
g=Math.floor(g);
var b = randRange(0,255);
b=Math.floor(b);
var a = randRange(0,1);//透明度
a=a.toFixed(1)
return "rgba("+r+","+g+","+b+","+a+")";
}
自己复制粘贴,自己领悟下
2018年07月31日 14点07分
9
哇,成功了,谢谢大佬
2018年08月01日 06点08分