前幾天再編輯自己的網頁時想到上次百度做炸雞啤酒那個雪花飄落,於是有興趣就在網路上找了一找,找到了沒有注釋的源碼,於是自己就學習了一下,並且為代碼添加上注釋和大家分享,源碼地址也已經貼出。注釋不知道是否正確,有問題大家可以留言。
js部分
(function(){ function k(a, b, c) { if (a.addEventListener) a.addEventListener(b, c, false); else a.attachEvent && a.attachEvent("on" + b, c) }//添加監聽事件函數 function g(a) { if (typeof window.onload != "function") window.onload = a; else { var b = window.onload; window.onload = function() { b(); a() } } }//頁面載入時就可以進行繪製 function h() { var a = {}; for (type in { Top: "", Left: "" }) { var b = type == "Top" ? "Y": "X"; if (typeof window["page" + b + "Offset"] != "undefined") a[type.toLowerCase()] = window["page" + b + "Offset"]; else { b = document.documentElement.clientHeight ? document.documentElement: document.body; a[type.toLowerCase()] = b["scroll" + type] } } return a }//top為向下已經拖動的高度,left為向左已經拖動的寬度 function l() { var a = document.body, b; if (window.innerHeight) b = window.innerHeight; else if (a.parentElement.clientHeight) b = a.parentElement.clientHeight; else if (a && a.clientHeight) b = a.clientHeight; return b }//擷取當前螢幕網頁寬度 function i(a) { this.parent = document.body; this.createEl(this.parent, a); this.size = Math.random() * 5 + 5; this.el.style.width = Math.round(this.size) + "px"; this.el.style.height = Math.round(this.size) + "px";//img節點大小 this.maxLeft = document.body.offsetWidth - this.size;//左邊界 this.maxTop = document.body.offsetHeight - this.size;//下邊界 this.left = Math.random() * this.maxLeft;//初始X軸 this.top = h().top + 1;//初始Y軸 this.angle = 1.4 + 0.2 * Math.random();//初始角度 this.minAngle = 1.4;//擺動角度範圍1.4pi~1.6pi this.maxAngle = 1.6; this.angleDelta = 0.01 * Math.random();//角度變動 this.speed = 2 + Math.random()//移動速度,2~3不等 } var j = false; g(function() { j = true }); var f = true; window.createSnow = function(a, b) { if (j) { var c = [], m = setInterval(function() { f && b > c.length && Math.random() < b * 0.0025 && c.push(new i(a)); //限定雪花數組大小! f && !c.length && clearInterval(m);//條件符合,清除雪花 for (var e = h().top, n = l(), d = c.length - 1; d >= 0; d--) if (c[d]) if (c[d].top < e || c[d].top + c[d].size + 1 > e + n) { c[d].remove(); c[d] = null; c.splice(d, 1)//雪花超出底線,刪除 } else { c[d].move();//雪花移動 c[d].draw()//雪花繪製 } }, 40);//40ms一跳 k(window, "scroll", function() { for (var e = c.length - 1; e >= 0; e--) c[e].draw() })//監聽捲軸時間,滾動重繪 } else g(function() { createSnow(a, b) }) }; window.removeSnow = function() { f = false };//移除雪花繪製 i.prototype = { createEl: function(a, b) { this.el = document.createElement("img"); //添加img節點 this.el.setAttribute("src", b + "snow" + Math.floor(Math.random() * 4) + ".gif"); //設定雪花圖片路徑,種類為snow0~3隨機 this.el.style.position = "absolute";//絕對的布局顯示 this.el.style.display = "block";//將雪花定義為區塊層級元素 this.el.style.zIndex = "99999";//在最前方顯示 this.parent.appendChild(this.el)//添加節點 }, move: function() { if (this.angle < this.minAngle || this.angle > this.maxAngle) this.angleDelta = -this.angleDelta;//保證角度在1.4到1.6之間 this.angle += this.angleDelta;//角度變換 this.left += this.speed * Math.cos(this.angle * Math.PI);//x軸位移計算 this.top -= this.speed * Math.sin(this.angle * Math.PI);//Y軸位移計算 if (this.left < 0) this.left = this.maxLeft;//邊界判定,雪花過邊界重另一邊界穿出 else if (this.left > this.maxLeft) this.left = 0 }, draw: function() { this.el.style.top = Math.round(this.top) + "px";//定義縱軸位置 this.el.style.left = Math.round(this.left) + "px"//定義橫軸位置 }, remove: function() { this.parent.removeChild(this.el);//移除雪花元素 this.parent = this.el = null } }})();
在網頁中添加:
<script src="jsized.snow.min.js" type="text/javascript"></script> <script> /** * This function takes 2 arguments * First is the path to the directory with snowflake images * Second is the maximum number of snowflakes, please do not * set this number above 60 as it will impact the performance */ createSnow('', 60);</script>
效果網站:http://hhteam.duapp.com/
雪花圖片已經貼出,右擊另存新檔即可
源碼在回複中貼出