<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
class Ball{
//构造函数
constructor(_r,_color){//传入随机数(小球宽高值)随机颜色
this.r=_r;//随机数字
this.color=_color;//随机颜色
this.speed=2;//速度
this.speedx=20*(Math.random()*2-1);//水平方向的速度
this.elem=this.create();//生成小球
}
create(){//生成小球
if(this.elem)return this.elem;//如果小球已存在,返回被创建的小球
let div=document.createElement("div");//创建div
Object.assign(div.style,{//给div添加样式
width:this.r+"px",
height:this.r+"px",
backgroundColor:this.color,
position:"absolute
JS-案例-跳动小球
最新推荐文章于 2022-12-12 13:47:50 发布