当前位置:网站首页>逐步分析类的拆分之案例——五彩斑斓的小球碰撞
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
2022-07-29 03:07:00 【勇敢*牛牛】
简单第一步,封装一个class,调用时执行构造函数被创建一个小圆球;

export default class {
Element;
constructor() {
this.Element = document.createElement('div');
Object.assign(this.Element.style, {
width: '50px',
height: '50px',
borderRadius: '50px',
backgroundColor:'red'
})
/* 获取div框,将元素添加进去 */
var div = document.querySelector('.div1')
div.appendChild(this.Element)
}
}
### 将创建成的这个圆球放在div中
appendTo(parent) {
if (typeof parent == "string") {
parent = document.querySelector(parent);
}
parent.appendChild(this.Element)
/* 获取父元素身上所有的属性 */
this.rect = parent.getBoundingClientRect();
}
再让他动起来
move() {
if (this.x > this.rect.width - this.w || this.x < 0) this.speedX = -this.speedX
if (this.y> this.rect.height - this.h ||this.y<0) this.speedY =-this.speedY
this.x += this.speedX;
this.y += this.speedY;
this.Element.style.left = this.x + "px";
this.Element.style.top = this.y + "px";
}
static upData() {
for (var item of Circle.list) {
item.move();
}
}
开始把繁琐的程序放在其他的js文件中;
Utils文件生成这个随机范围数和随机16进制数
export default class {
static randomSpeed(speed, slowSpeed) {
var speed = Math.random() * (2 * speed) - speed;
return (speed>-slowSpeed && speed<slowSpeed) ?slowSpeed:speed
}
static randomColor() {
return Array(6).fill(1).reduce(function(v,t){
return v+(~~(Math.random()*16)).toString(16)},'#')
return Array(6).fill(1).reduce((v)=>v + (~~(Math.random() * 16)).toString(16) , '#')
}
}
边栏推荐
- C traps and defects Chapter 3 semantic "traps" 3.1 pointers and arrays
- MySQL - the difference between count (field), count (primary key), count (1), count (*)
- Why did I choose the test when the development salary was high?
- shell脚本总结
- 算法---粉刷房子(Kotlin)
- Chapter 2 VRP command line
- The basic concept of the origin and connotation of maker Education
- 04 | 后台登录:基于账号密码的登录方式(上)
- C traps and defects Chapter 3 semantic "traps" 3.7 evaluation order
- Analyzing the subjective consciousness of emotional resonance between robots and human beings
猜你喜欢

MySQL installation and configuration super detailed tutorial and simple database and table building method

R language error: compilation failed for package '****‘

MySQL忘记密码怎么办

会议OA项目之我的审批功能

融云 IM & RTC 能力上新盘点

Analysis of Project-based Learning Creativity in steam Education

Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers

Notes on the seventh day

Multi table (Association) query of SQL query data

Shell programming specifications and variables
随机推荐
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
原理知识用得上
Available data sets for scene classification tasks (part)
2022-07-28 顾宇佳 学习笔记
IDEA安装后无法启动
单例模式(饿汉式 懒汉式)
[QNX hypervisor 2.2 user manual]9.11 RAM (under update)
Restfulapi - C - add header username and password authentication
kubernetes-1.24.x 特性
Mongodb index (3)
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
Analysis of Project-based Learning Creativity in steam Education
Feedback function of conference OA
C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题
13_ UE4 advanced_ Montage animation realizes attack while walking
01-SDRAM:初始化模块的代码
Summary of common hooks
C traps and defects Chapter 3 semantic "traps" 3.9 integer overflow
Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers
13_ue4进阶_蒙太奇动画实现一边走一边攻击