js實(shí)現(xiàn)煙花特效
本文實(shí)例為大家分享了js實(shí)現(xiàn)煙花特效的具體代碼,供大家參考,具體內(nèi)容如下
1.概述
在網(wǎng)頁(yè)背景中實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊出現(xiàn)模擬煙花爆炸的特效
2.思路
1.獲取鼠標(biāo)點(diǎn)擊位置,底端創(chuàng)建煙花節(jié)點(diǎn)。
2.為煙花添加css屬性,煙花節(jié)點(diǎn)從下至上運(yùn)動(dòng)。
3.運(yùn)動(dòng)至鼠標(biāo)位置時(shí)移除煙花節(jié)點(diǎn),同時(shí)生成多個(gè)煙花碎片。
4.為不同的煙花碎片隨機(jī)生成不同的顏色、運(yùn)動(dòng)速度、運(yùn)動(dòng)方向。
5.煙花碎片超出屏幕顯示部分時(shí)移除。
3.代碼部分
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { padding: 0; margin: 0; } html, body { width: 100%; height: 100%; background-color: black; overflow: hidden; } </style> </head> <body> <script src="move.js"></script> <script> class Firework { constructor(x, y) {//x,y鼠標(biāo)的位置 this.x = x;//將水平位置賦值給this.x屬性。 this.y = y;//將垂直位置賦值給this.y屬性。 this.ch = document.documentElement.clientHeight;//可視區(qū)的高度 } init() { //1.創(chuàng)建煙花節(jié)點(diǎn)。 this.firebox = document.createElement('div'); this.firebox.style.cssText = `width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${this.ch}px;`; document.body.appendChild(this.firebox); this.firemove();//創(chuàng)建完成,直接運(yùn)動(dòng)。 } //2.煙花節(jié)點(diǎn)運(yùn)動(dòng) firemove() { bufferMove(this.firebox, { top: this.y }, () => { document.body.removeChild(this.firebox); //當(dāng)煙花節(jié)點(diǎn)消失的時(shí)候,創(chuàng)建煙花碎片 this.createfires() }); } //3.當(dāng)前鼠標(biāo)點(diǎn)擊的位置,隨機(jī)產(chǎn)生30-60個(gè)盒子。(隨機(jī)顏色) createfires() { for (let i = 1; i <= this.rannum(30, 60); i++) { this.fires = document.createElement('div'); this.fires.style.cssText = `width:5px;height:5px;background:rgb(${this.rannum(0, 255)},${this.rannum(0, 255)},${this.rannum(0, 255)});position:absolute;left:${this.x}px;top:${this.y}px;`; document.body.appendChild(this.fires); this.fireboom(this.fires);//設(shè)計(jì)成一個(gè)一個(gè)運(yùn)動(dòng),等到循環(huán)結(jié)束,出現(xiàn)整體結(jié)果。 } } //4.煙花碎片運(yùn)動(dòng)。 fireboom(obj) { //存儲(chǔ)當(dāng)前obj的初始值。 let initx = this.x; let inity = this.y; //隨機(jī)產(chǎn)生速度(水平和垂直方向都是隨機(jī)的,符號(hào)也是隨機(jī)的)。 let speedx = parseInt((Math.random() > 0.5 ? '-' : '') + this.rannum(1, 15)); let speedy = parseInt((Math.random() > 0.5 ? '-' : '') + this.rannum(1, 15)); obj.timer = setInterval(() => { initx += speedx; inity += speedy++; //模擬重力加速度(垂直方向比水平方向快一些) if (inity >= this.ch) { document.body.removeChild(obj); clearInterval(obj.timer); } obj.style.left = initx + 'px'; obj.style.top = inity + 'px'; }, 1000 / 60); } //隨機(jī)區(qū)間數(shù) rannum(min, max) { return Math.round(Math.random() * (max - min) + min); } } document.onclick = function (ev) { var ev = ev || window.event; //ev.clientX,ev.clientY//獲取的鼠標(biāo)的位置 new Firework(ev.clientX, ev.clientY).init(); } </script> </body> </html>
4.Move.js
function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj)[attr]; } else { return obj.currentStyle[attr]; } } function bufferMove(obj, json, fn) { let speed = 0; clearInterval(obj.timer); obj.timer = setInterval(function () { var flag = true; for (var attr in json) { var currentValue = null; if (attr === 'opacity') { currentValue = Math.round(getStyle(obj, attr) * 100); } else { currentValue = parseInt(getStyle(obj, attr)); } speed = (json[attr] - currentValue) / 10; speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); if (currentValue !== json[attr]) { if (attr === 'opacity') { obj.style.opacity = (currentValue + speed) / 100; obj.style.filter = 'alpha(opacity=' + (currentValue + speed) + ')';//IE } else { obj.style[attr] = currentValue + speed + 'px'; } flag = false; } } if (flag) { clearInterval(obj.timer); fn && typeof fn === 'function' && fn(); } }, 10); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)超炫網(wǎng)頁(yè)煙花動(dòng)畫效果的方法
- JavaScript實(shí)現(xiàn)的簡(jiǎn)單煙花特效代碼
- 原生Js實(shí)現(xiàn)簡(jiǎn)易煙花爆炸效果的方法
- JS煙花背景效果實(shí)現(xiàn)方法
- 用p5.js制作煙花特效的示例代碼
- JS基于面向?qū)ο髮?shí)現(xiàn)的放煙花效果
- javascript實(shí)現(xiàn)網(wǎng)頁(yè)背景煙花效果的方法
- 新年快樂(lè)! javascript實(shí)現(xiàn)超級(jí)炫酷的3D煙花特效
- JS實(shí)現(xiàn)網(wǎng)頁(yè)煙花動(dòng)畫效果
- JavaScript實(shí)現(xiàn)煙花特效(面向?qū)ο?
相關(guān)文章
JavaScript實(shí)現(xiàn)搜索的數(shù)據(jù)顯示
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)搜索的數(shù)據(jù)顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10設(shè)置jsf的選擇框h:selectOneMenu為不可編輯狀態(tài)的方法
本文為大家詳細(xì)介紹下如何設(shè)置jsf的選擇框h:selectOneMenu為不可編輯狀態(tài),具體實(shí)現(xiàn)代碼如下,希望對(duì)大家有所幫助2014-01-01javascript實(shí)現(xiàn)10個(gè)球隨機(jī)運(yùn)動(dòng)、碰撞實(shí)例詳解
這篇文章主要介紹了javascript實(shí)現(xiàn)10個(gè)球隨機(jī)運(yùn)動(dòng)、碰撞的方法,實(shí)例分析了javascript實(shí)現(xiàn)小球碰撞的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07uni-app微信小程序登錄授權(quán)的實(shí)現(xiàn)
這篇文章主要介紹了uni-app微信小程序登錄授權(quán)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05javascript實(shí)現(xiàn)數(shù)字驗(yàn)證碼的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)javascript實(shí)現(xiàn)數(shù)字驗(yàn)證碼的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02JavaScript實(shí)現(xiàn)數(shù)組對(duì)象去重的多種方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)數(shù)組對(duì)象去重的多種方法,使用set對(duì)象或使用`reduce`方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-02-02用JavaScript實(shí)現(xiàn)讓瀏覽器停止載入頁(yè)面的方法
下面小編就為大家?guī)?lái)一篇用JavaScript實(shí)現(xiàn)讓瀏覽器停止載入頁(yè)面的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01JavaScript?ES6中class定義類實(shí)例方法
ES6提供了更接近面向?qū)ο?注意:javascript本質(zhì)上是基于對(duì)象的語(yǔ)言)語(yǔ)言的寫法,引入了Class(類)這個(gè)概念,作為對(duì)象的模板,下面這篇文章主要給大家介紹了關(guān)于JavaScript?ES6中class定義類的相關(guān)資料,需要的朋友可以參考下2022-07-07KnockoutJS數(shù)組比較算法實(shí)例詳解
這篇文章主要介紹了KnockoutJS數(shù)組比較算法實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11