老师检查下代码,我自己怎么写不出来啊
<style>
body {
background-color: black;
}
.ball {
position: absolute;
border-radius: 50%;
}
</style>
</head>
<body>
<script>
function ball(x, y) {
this.x = x;
this.y = y;
this.r = '20'
this.opacity=1;
this.color = colorarr[parseInt(Math.random()*colorarr.length)];
do{
this.dx=parseInt(Math.random()*20)-10;
this.dy=parseInt(Math.random()*20)-10;
}while(this.dx==0&&this.dy==0)
this.init();
ballarr.push(this);
}
ball.prototype.init = function () {
this.dom = document.createElement('div')
this.dom.className = 'ball'
this.dom.style.width = this.r * 2 + 'px'
this.dom.style.height = this.r * 2 + 'px'
this.dom.style.left = this.x - this.r + 'px';
this.dom.style.top = this.y - this.r + 'px'
this.dom.style.backgroundColor = this.color;
document.body.appendChild(this.dom);
}
ball.prototype.update = function () {
this.x+=this.dx;
this.y-=this.dy
this.r+=0.2;
this.opacity-=0.01;
this.dom.style.width = this.r * 2 + 'px'
this.dom.style.height = this.r * 2 + 'px'
this.dom.style.left = this.x - this.r + 'px';
this.dom.style.top = this.y - this.r + 'px'
this.dom.style.opacity=this.opacity;
if(this.opacity<0){
for(var i=0;i<ballarr.length;i++){
if(ballarr[i]==this){
ballarr.splice(i,1);
}
}
document.body.removeChild(this.dom);
}
}
var ballarr = [];
var colorarr=['#66cccc','#ccff66','#ff99cc','#ff6666','#cc3399','#ff6600']
new ball(200,600);
setInterval(function () {
for (var i = 0; i < ballarr.length; i++) {
ballarr[i].update()
}
}, 20);
document.onmousemove=function(e){
var x=e.clientX;
var y=e.clientY;
new ball(x ,y);
}
搜索
复制

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星