Created
January 2, 2010 09:46
-
-
Save tnhu/267444 to your computer and use it in GitHub Desktop.
snow script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.3site.eu/examples/snow/ | |
// http://www.3site.eu/examples/snow/snow.js | |
// http://www.3site.eu/examples/snow/snow.css | |
document.addEventListener("DOMContentLoaded", function snow() { | |
// (C) Andrea Giammarchi - Mit Style License | |
// Happy Holidays by WebReflection! | |
for (var | |
offsetHeight = document.documentElement.offsetHeight, | |
offsetWidth = document.documentElement.offsetWidth, | |
F = document.createDocumentFragment(), | |
random = Math.random, | |
Int = parseInt, | |
i = Math.max(Math.min((offsetWidth * offsetHeight / 15000) >> 0, 40), 10), | |
radius = 6, | |
p, width, border, style, top, left; | |
i--; | |
){ | |
p = document.createElement("p"); | |
p.style.cssText = "".concat( | |
"position:absolute;", | |
"top:", (random() * offsetHeight) >> 0, "px;", | |
"left:", (random() * offsetWidth) >> 0, "px;", | |
"width:", width = ((random() * radius) >> 0) + 1, "px;", | |
"height:", width, "px;", | |
"z-index:", width, ";", | |
"-moz-border-radius:", border = (width / 2) >> 0, "px;", | |
"-webkit-border-radius:", border, "px;", | |
"border-radius:", border, "px;", | |
"font-size:", (random() * (radius / 2)) >> 0, "px;" | |
); | |
F.appendChild(p).className = "snow"; | |
}; | |
document.body.appendChild(F); | |
window.addEventListener("resize", function () { | |
offsetHeight = document.documentElement.offsetHeight; | |
offsetWidth = document.documentElement.offsetWidth; | |
}, false); | |
radius = radius / 2; | |
setTimeout(function snow(p) { | |
for(i = p.length; i--;) { | |
style = p[i].style; | |
width = Int(style.width, 10); | |
top = Int(style.top, 10) + width; | |
left = Int(style.left, 10) + (border = width + Int(style.fontSize, 10)); | |
if ((style.top = (top + width < offsetHeight ? top : 0) + "px") === "0px") { | |
style.left = ((random() * offsetWidth) >> 0) + "px"; | |
style.fontSize = ((random() * (radius / 2)) >> 0) + "px"; | |
} else { | |
style.left = (left + border < offsetWidth ? left : 0) + "px"; | |
}; | |
}; | |
setTimeout(snow, 30, p); | |
}, 30, document.getElementsByTagName("p")); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment