console
-(function(){
var Grewer = {
init:function(option){
option = option || {};
this.speed = option.speed || 5;
this.getWindowSize();
this.main = document.getElementById('snow');
this.main.style.width = this.winWidth + 'px';
this.main.style.height = this.winHeight + 'px'
document.body.style.margin = 0;
this.main.style.backgroundColor = '#79c6e8';
this.arr = [];
this.run(option);
},
getWindowSize:function(){
if(window.innerWidth){
this.winWidth=window.innerWidth;
}else if((document.body)&&(document.body.clientWidth)){
this.winWidth=document.body.clientWidth;
}
if(window.innerHeight){
this.winHeight=window.innerHeight;
}else if((document.body)&&(document.body.clientHeight)){
this.winHeight=document.body.clientHeight;
}
},
render:function(num){
this.Num = (num * Math.random())|0;
while(this.Num--){
var RS = {
x:(Math.random()*this.winWidth)|0,
y:0,
stepX:(Math.random()*5-2)|0,
stepY:((Math.random()*this.speed)|0)+this.speed
}
var img = document.createElement('div');
img.style.width = '20px';
img.style.height = '20px';
img.innerHTML = '*';
img.style.color = '#fff';
img.style.position = 'absolute';
img.style.left = RS.x + 'px';
img.style.top = (RS.y-20)+'px';
this.main.appendChild(img);
RS.el = img;
this.arr.push(RS);
}
},
update:function(){
var i = this.arr.length;
while(i--){
var list = this.arr[i],
obj = list.el;
list.x += list.stepX;
obj.style.left = list.x + 'px';
list.y += list.stepY;
obj.style.top = list.y + 'px';
if(this.check(list)){
this.arr.splice(i,1);
}
}
},
removeElement:function (_element){
var _parentElement = _element.parentNode;
if(_parentElement){
_parentElement.removeChild(_element);
}
},
check:function(list){
if(list.x-20 <= 0){
this.removeElement(list.el)
return true
}
if(list.x+20 >= this.winWidth){
this.removeElement(list.el)
return true
}
if(list.y+20 > this.winHeight){
this.removeElement(list.el)
return true;
}
return false;
},
run:function(option){
setInterval(function(){
snow.update();
}
,100);
setInterval(function(){
snow.render(option.num||10)
},option.snowFlash||1000)
}
}
window.snow = Object.create(Grewer);
})()
snow.init();
<div id="snow">
</div>