javascript實(shí)現(xiàn)下雪效果【實(shí)例代碼】
原理 :
1、js動(dòng)態(tài)創(chuàng)建DIV,指定CLASS類設(shè)置不同的背景圖樣式顯示不同的雪花效果。
2、js獲取創(chuàng)建的DIV并改變其top屬性值,當(dāng)下落的高度大于屏幕高后刪除該移動(dòng)div
3、好像不夠完善勿噴
HTML代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>雪花飛舞</title> <link rel="stylesheet" href="css/index.css"> <script src="js/move.js"></script> </head> <body> <div class="snow_parent" id="js_sonw"> </div> </body> </html>
CSS代碼
*{ margin:0; padding:0; list-style: none; border: none; } body{ width: 100%; height:600px; background:#000; } .snow_parent{ position: relative; width: 100%; height:100%; overflow: hidden; margin: 0 auto; } .snow_parent div.parent{ background-image: url(../img/snow.png); float: left; -webkit-transform: scale(.1); -moz-transform: scale(.1); -o-transform: scale(.1); -ms-transform: scale(.1); transform: scale(.1); position: absolute; } .snow_one{ width: 180px; height: 180px; background-position:0 0; background-repeat: no-repeat; left:-70px; top: -95px; } .snow_two{ width: 140px; height: 140px; background-position:-220px -18px; left:-30px; top: -75px; } .snow_three{ width:150px; height: 150px; background-position:-400px -15px; left:-20px; top: -80px; } .snow_four{ width: 160px; height: 160px; background-position:-10px -206px; } .snow_four{ left:-10px; top: -85px; }
JS代碼:
/* creatBy jiucheng 2016-4-24 */ window.onload=function(){ init(); } // 創(chuàng)建DIV function creatDiv(){ // 創(chuàng)建DIV并追加到父元素 var snowDiv=document.createElement("div"); document.getElementById("js_sonw").appendChild(snowDiv); // 讓創(chuàng)建DIV的class為隨機(jī),顯示不同的雪花 var whatName=["snow_one parent","snow_two parent","snow_three parent","snow_four parent"]; var index=Math.floor(Math.random()*whatName.length); snowDiv.className=whatName[index]; // 獲取該DIV的left屬性值(隨機(jī)的)并賦值給創(chuàng)建的DIV var whatLeft=getLeft()+'px'; snowDiv.style.left=whatLeft; return snowDiv; } // 獲取隨機(jī)left屬性值 function getLeft(){ // 獲取該DIV的最大left屬性值即父元素的寬度 var eleParent=document.getElementById("js_sonw"); // 獲取父元素的所有style樣式 var style=window.getComputedStyle(eleParent); // CSS中的left是負(fù)數(shù)這里得減去下 var maxWidth=parseInt(style.width)+70; // 讓創(chuàng)建的DIV的left為隨機(jī)值 var randomLeft=Math.floor(Math.random()*maxWidth); return randomLeft; } // 讓其向下移動(dòng) function moveDown(){ // 獲取移動(dòng)對(duì)象 var moveElem=creatDiv(); // 獲取移動(dòng)對(duì)象的所有style屬性值 var eleStyle=window.getComputedStyle(moveElem); // 獲取它的top屬性值 var eleTop=parseInt(eleStyle.top); // 設(shè)置定時(shí)器動(dòng)態(tài)改變移動(dòng)對(duì)象的top屬性值 var t=setInterval(function(){ eleTop++; // 把新的top值付給移動(dòng)對(duì)象 moveElem.style.top=eleTop+"px"; // 當(dāng)下落到屏幕的高度后停止定時(shí)器并把該移動(dòng)對(duì)象從父元素刪除 if(eleTop>=window.innerHeight){ clearInterval(t); document.getElementById("js_sonw").removeChild(moveElem); } },10);//下落速度沒(méi)10毫秒下落1px } function init(){ // 動(dòng)態(tài)獲取并設(shè)置body的高度 document.body.style.height=window.innerHeight+"px"; // 每500毫秒創(chuàng)建一個(gè)移動(dòng)對(duì)象并執(zhí)行移動(dòng)函數(shù) var t=setInterval(function(){ moveDown(); },100); }
以上這篇javascript實(shí)現(xiàn)下雪效果【實(shí)例代碼】就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 關(guān)于賦值、淺拷貝、深拷貝的個(gè)人理解
關(guān)于賦值、淺拷貝、深拷貝,以前也思考良久,很多時(shí)候都以為記住了,但是,我太難了。今天我特地寫(xiě)下筆記,希望可以完全掌握這個(gè)東西,也希望可以幫助到任何想對(duì)學(xué)習(xí)這個(gè)東西的同學(xué)2019-11-11javascript動(dòng)畫(huà)之模擬拖拽效果篇
其實(shí)javascript本身是具有原生拖放功能的,但是由于兼容性問(wèn)題,以及功能實(shí)現(xiàn)的方式,用的不是很廣泛。javascript動(dòng)畫(huà)廣泛使用的還是模擬拖拽。本文將詳細(xì)介紹javascript的模擬拖拽,有需要的可以參考借鑒。2016-09-09Webpack-cli安裝成功后查看webpack -v報(bào)錯(cuò)案例詳解
這篇文章主要介紹了Webpack-cli安裝成功后查看webpack -v報(bào)錯(cuò)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Bootstrap Navbar Component實(shí)現(xiàn)響應(yīng)式導(dǎo)航
這篇文章主要介紹了Bootstrap Navbar Component實(shí)現(xiàn)響應(yīng)式導(dǎo)航的相關(guān)資料,講解了Bootstrap Navbar應(yīng)用及源碼解析,需要的朋友可以參考下2016-10-10JS實(shí)現(xiàn)為表格動(dòng)態(tài)添加標(biāo)題的方法
這篇文章主要介紹了JS實(shí)現(xiàn)為表格動(dòng)態(tài)添加標(biāo)題的方法,涉及javascript中createCaption方法添加標(biāo)題的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03