js實現(xiàn)煙花特效
本文實例為大家分享了js實現(xiàn)煙花特效的具體代碼,供大家參考,具體內(nèi)容如下
1.概述
在網(wǎng)頁背景中實現(xiàn)鼠標(biāo)點擊出現(xiàn)模擬煙花爆炸的特效
2.思路
1.獲取鼠標(biāo)點擊位置,底端創(chuàng)建煙花節(jié)點。2.為煙花添加css屬性,煙花節(jié)點從下至上運動。3.運動至鼠標(biāo)位置時移除煙花節(jié)點,同時生成多個煙花碎片。4.為不同的煙花碎片隨機生成不同的顏色、運動速度、運動方向。5.煙花碎片超出屏幕顯示部分時移除。
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='http://m.cgvv.com.cn/bcjs/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é)點。 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)建完成,直接運動。 } //2.煙花節(jié)點運動 firemove() { bufferMove(this.firebox, { top: this.y }, () => { document.body.removeChild(this.firebox); //當(dāng)煙花節(jié)點消失的時候,創(chuàng)建煙花碎片 this.createfires() }); } //3.當(dāng)前鼠標(biāo)點擊的位置,隨機產(chǎn)生30-60個盒子。(隨機顏色) 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è)計成一個一個運動,等到循環(huán)結(jié)束,出現(xiàn)整體結(jié)果。 } } //4.煙花碎片運動。 fireboom(obj) { //存儲當(dāng)前obj的初始值。 let initx = this.x; let inity = this.y; //隨機產(chǎn)生速度(水平和垂直方向都是隨機的,符號也是隨機的)。 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); } //隨機區(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);}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程2. 詳解瀏覽器的緩存機制3. Xml簡介_動力節(jié)點Java學(xué)院整理4. python多線程和多進(jìn)程關(guān)系詳解5. 一款功能強大的markdown編輯器tui.editor使用示例詳解6. Python xlrd/xlwt 創(chuàng)建excel文件及常用操作7. python 寫函數(shù)在一定條件下需要調(diào)用自身時的寫法說明8. 存儲于xml中需要的HTML轉(zhuǎn)義代碼9. Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)10. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享
