css样式:
#box {
width: 1200px;
height: 900px;
margin: auto;
position: relative;
overflow: hidden;
background-color: black;
}
#c {
border-radius: 50%;
}
<body>
<div id="box">
<!-- <div id="c" style="position: absolute;top:20px;left:20px;width: 20px;height: 20px;background-color: aliceblue;"></div> -->自己先写出来就容易明白
</div>
<script>
function Bull(size, left, top) {
this.size = size;
this.left = left;
this.top = top;
}
Bull.prototype.Bubbles = function() {
let _size = this.RandomNum(10, 60);
let _left = this.RandomNum(1, 1150);
let _top = this.RandomNum(1, 890);
let _bgc = this.RandomColor()
var _html = '<div id="c" style="position: absolute;top:' + _top + 'px;left:' + _left + 'px;width: ' + _size + 'px;height: ' + _size + 'px;background-color: ' + _bgc + ';"></div>';
return _html;
}
//随机颜色
Bull.prototype.RandomColor = function() {
var BgcColor = [" #f83600 ", "#FFF94C ", "#0072ff ", "#780206 ", "#7B920A ", "#dc2430 ", "#A83279 ", "#00bf8f ", "#FF512F ", "#485563 ", "#061700 ", "#02AAB0 "];
return BgcColor[this.RandomNum(0, BgcColor.length - 1)];
}
//随机数值(这里就产生了一个问题?闭包:外面的作用域可以访问到RandomNum内部的局部变量,避免出现全局变量的污染和出现函数名冲突问题)
Bull.prototype.RandomNum = function(max, min) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var str = '';
for (var i = 0; i <= 150; i++) {
str += new Bull().Bubbles();
}
// var ball=new Bull();//把这个输出的球进行累加
// var str = ball.Bubbles();//输出一个str(彩球)
这两句是对str += new Bull().Bubbles();的解释
document.querySelector('#box').innerHTML = str;
</script>
</body>