|
|
请马上登录,朋友们都在花潮里等着你哦:)
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 马黑黑 于 2023-7-8 13:01 编辑
上一讲 用JS类封装粒子特效(二)的 5楼 代码能驱使紫色正方形在父元素内上下左右来回移动。我们先来复习一下 Lizi 类完整代码,然后通过实例化配置实现真正的粒子特效:
class Lizi {
//构造函数 : 属性设计、设置 pa - 粒子宿主元素,size - 粒子大小
constructor(pa,size = 20) {
this.pa = pa;
this.size = size;
this.bg = 'purple';
this.left = 10;
this.top = 10;
this.xstep = 1; //水平移动步幅
this.ystep = 1; //垂直移动步幅
this.ele = document.createElement('li-zi'); //粒子元素 HTML标签名为 li-zi
}
//创建粒子
creating() {
this.ele.style.cssText = `
width: ${this.size}px;
height: ${this.size}px;
left: ${this.left}px;
top: ${this.top}px;
background: ${this.bg};
`;
this.pa.appendChild(this.ele);
this.moving();
}
//移动粒子
moving() {
this.left += this.xstep;
this.top += this.ystep;
if(this.left <= 0 || this.left >= this.pa.offsetWidth - this.size) this.xstep = -this.xstep;
if(this.top <= 0 || this.top >= this.pa.offsetHeight - this.size) this.ystep = -this.ystep;
this.ele.style.left = this.left + 'px';
this.ele.style.top = this.top + 'px';
requestAnimationFrame(this.moving.bind(this));
}
}
重点在:
① 构造函数里设计了 xstep 和 ystep 两个属性用以储存 x 和 y 方向的行进步幅;
② creating 函数调用了 moving 函数,粒子一旦创建立马动起来;
③ moving 函数驱使粒子运动,它通过改变粒子对象的 left 和 top 两个值,并对应去更改粒子元素属性 ele 的 CSS 的 left(style.left)和 top(style.top)属性达成运动目的,其中有边界判断、根据判断改变xy行进步幅的正负值,最后通过 requestAnimationFrame 定时器做永恒动力。
这样,实例化调用这个 Lizi 类可以是非常简单的:
let lz = new Lizi(mydiv,30); //实例化粒子
lz.creating(); //创建一个粒子
本讲,我们要创建更多的粒子,这些粒子是圆形的(在CSS中设置),它们的尺寸、位置、颜色以及移动步幅都不尽相同,需要一一配置。让我们在实例化类对象中完成这一操作:
//设置一组步幅数组 :以便令粒子的运动速度有差异性
let stepAr = [0.5,-0.5,1,-1,0.8,-0.8,1.2,-1.2];
//创建30个粒子
Array.from({length: 30}).forEach((element) => {
//随机获取步幅索引
let xIdx = Math.floor(Math.random() * stepAr.length),
yIdx = Math.floor(Math.random() * stepAr.length);
element = new Lizi(mydiv); //实例化Lizi类
//尺寸区间 :4 - 14
element.size = 4 + Math.ceil(Math.random() * 10);
//左边值
element.left = Math.floor(Math.random() * (mydiv.offsetWidth - element.size));
//上边值
element.top = Math.floor(Math.random() * (mydiv.offsetHeight - element.size));
//x步幅
element.xstep = stepAr[xIdx];
//y步幅
element.ystep = stepAr[yIdx];
//随机背景色 :十六进制颜色
element.bg = '#' + Math.random().toString(16).substr(-6);
//创建粒子
element.creating();
});
粒子的形状我们可以在CSS的 li-zi 选择器中设置,border-radius: 50%; ,当然也可以在JS实例化的 creating() 函数执行后加入一句:element.ele.style.borderRadius = '50'; 。
这里,实例化代码看上去好复杂,实则逻辑清晰,都是对应 Lizi 类的属性进行相关属性配置、调用类提供的方法。在配置类实例化属性时,我们用上了 JS 的内置对象 Math,它提供丰富的数学操作,比如,我们用 Math.random() 生成随机数并用到各种算法中,用 Math.floor 和 Math.ceil 向下、向上获取整数,等等,还有其他的 JS 内置的东东,相关知识请参阅 JS:随机生成十六进制颜色值 - 马黑黑教程专版 - 花潮论坛 - Powered by Discuz! (huachaowang.com) (2楼)。
以下是本讲示例的完整代码:
- <style>
- #mydiv {
- margin: 20px auto;
- width: 600px;
- height: 300px;
- border: 1px solid purple;
- position: relative;
- }
- li-zi {
- position: absolute;
- border-radius: 50%;
- }
- </style>
- <div id="mydiv"></div>
- <script>
- class Lizi {
- constructor(pa,size = 20) {
- this.pa = pa;
- this.size = size;
- this.bg = 'purple';
- this.left = 10;
- this.top = 10;
- this.xstep = 1;
- this.ystep = 1;
- this.ele = document.createElement('li-zi');
- }
- creating() {
- this.ele.style.cssText = `
- width: ${this.size}px;
- height: ${this.size}px;
- left: ${this.left}px;
- top: ${this.top}px;
- background: ${this.bg};
- `;
- this.pa.appendChild(this.ele);
- this.moving();
- }
- moving() {
- this.left += this.xstep;
- this.top += this.ystep;
- if(this.left <= 0 || this.left >= this.pa.offsetWidth - this.size) this.xstep = -this.xstep;
- if(this.top <= 0 || this.top >= this.pa.offsetHeight - this.size) this.ystep = -this.ystep;
- this.ele.style.left = this.left + 'px';
- this.ele.style.top = this.top + 'px';
- requestAnimationFrame(this.moving.bind(this));
- }
- }
- let stepAr = [0.5,-0.5,1,-1,0.8,-0.8,1.2,-1.2]; //步幅数组
- Array.from({length: 30}).forEach((element) => {
- let xIdx = Math.floor(Math.random() * stepAr.length),
- yIdx = Math.floor(Math.random() * stepAr.length);
- element = new Lizi(mydiv);
- element.size = 4 + Math.ceil(Math.random() * 10);
- element.left = Math.floor(Math.random() * (mydiv.offsetWidth - element.size));
- element.top = Math.floor(Math.random() * (mydiv.offsetHeight - element.size));
- element.xstep = stepAr[xIdx];
- element.ystep = stepAr[yIdx];
- element.bg = '#' + Math.random().toString(16).substr(-6);
- element.creating();
- });
- </script>
复制代码 运行效果请看下一楼。
|
评分
-
| 参与人数 5 | 威望 +190 |
金钱 +380 |
经验 +190 |
收起
理由
|
樵歌
| + 50 |
+ 100 |
+ 50 |
赞一个! |
焱鑫磊
| + 30 |
+ 60 |
+ 30 |
赞一个! |
红影
| + 50 |
+ 100 |
+ 50 |
赞一个! |
醉美水芙蓉
| + 30 |
+ 60 |
+ 30 |
很给力! |
南无月
| + 30 |
+ 60 |
+ 30 |
赞一个! |
查看全部评分
|