原生JS實(shí)現(xiàn)煙花效果
原生JS實(shí)現(xiàn)煙花效果,點(diǎn)擊頁(yè)面生成煙花,向四周擴(kuò)散,然后再墜落下去。(這里的煙花我是用的特殊字符愛(ài)心形狀)
基礎(chǔ)css代碼
/* 設(shè)置基礎(chǔ)的css樣式 */ body{background: #000;overflow: hidden;} .fire{position: absolute;width: 4px;height: 30px;}
js代碼:
1、給頁(yè)面添加點(diǎn)擊事件,生成主體煙花
//給頁(yè)面設(shè)置點(diǎn)擊事件 document.onclick = function(eve){ var e = eve || window.event; //設(shè)置一個(gè)空數(shù)組,用來(lái)后面存放小煙花 var arr = []; //獲取鼠標(biāo)點(diǎn)擊的位置 var x = e.clientX; var y = e.clientY; //設(shè)置步長(zhǎng) var speed = 20; //生成大煙花,設(shè)置他的css樣式,出發(fā)點(diǎn)在可視區(qū)頁(yè)面的下方 var fire = document.createElement(’div’); fire.className = ’fire’; fire.style.background = randomColor(); fire.style.left = x + ’px’; fire.style.top = document.documentElement.clientHeight+’px’; //將大煙花追加到頁(yè)面上 document.body.appendChild(fire);
2、設(shè)置定時(shí)器,讓煙花向上運(yùn)動(dòng),刪除
//生成定時(shí)器 var t = setInterval(function(){ //判斷如果大煙花的TOP值小于小于目標(biāo)值,清除定時(shí)器,并且將大煙花清除 if(fire.offsetTop <= y){ clearInterval(t); document.body.removeChild(fire); //執(zhí)行show(生成小煙花) show(); } //讓大煙花垂直向上運(yùn)動(dòng) fire.style.top = fire.offsetTop - speed +’px’; },30);
3、然后在點(diǎn)擊的位置生成小煙花,設(shè)置樣式
function show(){ //利用循環(huán),生成50個(gè)小煙花,給小煙花添加css屬性 for(var i=0;i<50;i++){ var sFire = document.createElement(’div’); // sFire.className = ’small-fire’; sFire.style.left = x +’px’; sFire.style.top = y +’px’; // sFire.style.background = randomColor(); sFire.style.color = randomColor(); sFire.innerHTML = ’❤’; sFire.style.position = ’absolute’; //生成隨機(jī)數(shù) var a=Math.random()*360; sFire.sx = Math.sin(a*180/Math.PI)*20*Math.random(); sFire.sy = Math.cos(a*180/Math.PI)*20*Math.random(); //將小煙花追加到頁(yè)面上 document.body.appendChild(sFire); //將生成的煙花信息都添加到數(shù)組中 arr.push(sFire); } }
4、設(shè)置定時(shí)器,讓小煙花完成自由落體運(yùn)動(dòng)
setInterval(function move(){ //利用循環(huán)一直改變小煙花的位置 for(var i=0;i<arr.length;i++){ //設(shè)置將每次循環(huán)的第i個(gè)小煙花的運(yùn)動(dòng)范圍 arr[i].style.left = arr[i].offsetLeft + arr[i].sx + ’px’; arr[i].style.top = arr[i].offsetTop + arr[i].sy + ’px’; //讓煙花垂直方向的位置每次都增加,實(shí)現(xiàn)下落效果 arr[i].sy += 1; //判斷煙花是否運(yùn)動(dòng)出屏幕可視區(qū),如果是,就將它刪除 if(arr[i].offsetLeft<0 || arr[i].offsetLeft > document.documentElement.clientWidth || arr[i].offsetTop > document.documentElement.clientHeight){ document.body.removeChild(arr[i]); // arr.splice(i,1); } } },30) }
5、最后別忘了我們的隨機(jī)數(shù)和隨機(jī)顏色的封裝
// 范圍隨機(jī)數(shù) function random(max,min){ return Math.round(Math.random()*(max-min)+min); } // 隨機(jī)顏色 function randomColor(){ return 'rgb('+random(0,255)+','+random(0,255)+','+random(0,255)+')'; }
最后我們的煙花效果就實(shí)現(xiàn)了
今天的分享就到這里,希望大家能夠喜歡。
更多JavaScript精彩特效分享給大家:
Javascript菜單特效大全
javascript仿QQ特效匯總
JavaScript時(shí)鐘特效匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類(lèi)別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例2. python中PyQuery庫(kù)用法分享3. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說(shuō)明4. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送5. python將下載到本地m3u8視頻合成MP4的代碼詳解6. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效7. python 爬取嗶哩嗶哩up主信息和投稿視頻8. Python編寫(xiě)nmap掃描工具9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
