效果如下
一、JS文件内代码
1.Utils 类
export default class Utils{
static ce(type,style,parent){
var elem=document.createElement(type);
if(style){
for(var prop in style){
elem.style[prop]=style[prop];
}
}
if(typeof parent==="string") parent=document.querySelector(parent);
if(parent) parent.appendChild(elem);
return elem;
}}
2.MoveBall类
import Utils from "./Utils.js";
export default class MoveBall {
color;
elem;
w;
speedX;
speedY;
tw;
constructor() {
this.elem = this.creatElem();
}
creatElem() {
if (this.elem) return this.elem;
this.color = Utils.randomColor()<