javascript實現下雪效果【執行個體代碼】,javascript執行個體

來源:互聯網
上載者:User

javascript實現下雪效果【執行個體代碼】,javascript執行個體

原理 :

1、js動態建立DIV,指定CLASS類設定不同的背景圖樣式顯示不同的雪花效果。

2、js擷取建立的DIV並改變其top屬性值,當下落的高度大於螢幕高後刪除該移動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();}// 建立DIVfunction creatDiv(){  // 建立DIV並追加到父元素  var snowDiv=document.createElement("div");  document.getElementById("js_sonw").appendChild(snowDiv);  // 讓建立DIV的class為隨機,顯示不同的雪花  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屬性值(隨機的)並賦值給建立的DIV  var whatLeft=getLeft()+'px';  snowDiv.style.left=whatLeft;  return snowDiv;}// 擷取隨機left屬性值function getLeft(){  // 擷取該DIV的最大left屬性值即父元素的寬度  var eleParent=document.getElementById("js_sonw");  // 擷取父元素的所有style樣式  var style=window.getComputedStyle(eleParent);  // CSS中的left是負數這裡得減去下  var maxWidth=parseInt(style.width)+70;  // 讓建立的DIV的left為隨機值  var randomLeft=Math.floor(Math.random()*maxWidth);  return randomLeft;}// 讓其向下移動function moveDown(){  // 擷取移動對象  var moveElem=creatDiv();  // 擷取移動對象的所有style屬性值  var eleStyle=window.getComputedStyle(moveElem);  // 擷取它的top屬性值  var eleTop=parseInt(eleStyle.top);  // 設定定時器動態改變移動對象的top屬性值  var t=setInterval(function(){    eleTop++;    // 把新的top值付給移動對象    moveElem.style.top=eleTop+"px";    // 當下落到螢幕的高度後停止定時器並把該移動對象從父元素刪除    if(eleTop>=window.innerHeight){          clearInterval(t);      document.getElementById("js_sonw").removeChild(moveElem);    }      },10);//下落速度沒10毫秒下落1px}function init(){  // 動態擷取並設定body的高度  document.body.style.height=window.innerHeight+"px";  // 每500毫秒建立一個移動對象並執行移動函數  var t=setInterval(function(){    moveDown();  },100);}

以上這篇javascript實現下雪效果【執行個體代碼】就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.