(function () {
// 添加事件监听器
function addEvent(a, b, c) {
if (a.addEventListener) a.addEventListener(b, c, false);
else a.attachEvent && a.attachEvent("on" + b, c)
}
// 向window.onload添加执行函数
function addToOnLoad(a) {
if (typeof window.onload != "function") window.onload = a;
else {
var b = window.onload;
window.onload = function () {
b();
a()
}
}
}
// scroll top ,left
function getScrollDis() {
var scroll = {};
for (var type in {
Top: "",
Left: ""
}) {
var b = type == "Top" ? "Y" : "X";
if (typeof window["page" + b + "Offset"] != "undefined") scroll[type.toLowerCase()] = window["page" + b + "Offset"];
else {
// <html>
b = document.documentElement.clientHeight ? document.documentElement : document.body;
scroll[type.toLowerCase()] = b["scroll" + type]
}
}
return scroll;
} // 获取浏览器窗口高度(不包括工具栏/滚动条)
function getWinHeight() { var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
return height;
} //构造函数 ,模拟雪花类
function SnowDot(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";
this.maxLeft = document.body.offsetWidth - this.size;
this.maxTop = document.body.offsetHeight - this.size;
this.left = Math.random() * this.maxLeft;
this.top = getScrollDis().top + 1;
this.angle = 1.4 + 0.2 * Math.random();
// PI/2 =1.57
this.minAngle = 1.4;
this.maxAngle = 1.6;
// 角度增量
this.angleDelta = 0.01 * Math.random();
this.speed = 2 + Math.random()
}
// 原型 对象实例所共享
SnowDot.prototype = {
createEl: function (a, b) {
this.el = document.createElement("img");
this.el.setAttribute("src", b + "snow" + Math.floor(Math.random() * 4) + ".gif");
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;
this.angle += this.angleDelta;
// 利用正弦波 余弦波(注意波形图在 pi/2 附近的取值)
this.left += this.speed * Math.cos(this.angle * Math.PI);
this.top -= this.speed * Math.sin(this.angle * Math.PI);
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
}
} var j = false;
addToOnLoad(function () {
j = true
}); //开启/关闭 标志
var f = true; // a : 雪花gif图片所在路径
// b : 雪花最大数目
window.createSnow = function (a, b) {
if (j) {
var c = [],
m = setInterval(function () {
// &&前的为false则后边的语句不再执行
f && b > c.length && Math.random() < b * 0.0025 && c.push(new SnowDot(a));
!f && !c.length && clearInterval(m);
for (var e = getScrollDis().top, n = getWinHeight(), d = c.length - 1; d >= 0; d--) {
if (c[d]) {
// 雪花超出指定高度
if (c[d].top > 700 || c[d].top + c[d].size + 1 > e + n) {
c[d].remove();
c[d] = null;
c.splice(d, 1) //移除数组索引d位置开始1个元素
//alert(c[d].top)
} else {
c[d].move();
c[d].draw()
}
}
}
},
40);
// 窗口滚动时 雪花移动相应的距离
addEvent(window, "scroll",
function () {
for (var e = c.length - 1; e >= 0; e--) c[e].draw()
})
} else addToOnLoad(function () {
createSnow(a, b)
})
};
window.removeSnow = function () {
f = false
}; })();
源代码:snow.zip 页面内容节选自 阿狸-梦之城堡