// 彩条
function ColorBar(obj,timer){
	this.target = obj;
	this.i = 0;
	this.wind = 0;
	this.timer = timer || 100;
	this.other = 0;
	this.init();
}
ColorBar.prototype = {
	init:function(){
		this.createElem();
		this.snowRun();
	},
	getRndAndFillZero :function (){
	    var str=Math.ceil(Math.random()*16777215).toString(16);
	    while(str.length<6){
	        str='0'+str;
	    }
	    return str;
	},
	createElem : function(){
		var self = this,
			box = self.target,
			wind = self.wind;
		var s01 = Math.random()*10,
		s02 = Math.random()*10,
		s03 = Math.random()*10,
		s04 = Math.random()*10;

        var $oDiv = $('<div class="snow" id="snow'+this.i+'"></div>');
        if(wind != 0){
            var snow_left = Math.random() > Math.abs(wind*0.025) ? Math.random() : (wind > 0 ? 0 : 1) ;
        }else{
            var snow_left = Math.random();
        }
        snow_left = snow_left * parseInt(box.width());
        var snow_top = -10,
        borderColor = 'transparent transparent #' + self.getRndAndFillZero();
        if (Math.random() < 0.5) {
        	borderColor = '#' + self.getRndAndFillZero()+' transparent transparent';
        };
        if (Math.random() > 0.5 && Math.random() < 0.8) {
        	borderColor = '#' + self.getRndAndFillZero()+' transparent transparent';
        };
        if (Math.random() > 0.8) {
        	borderColor = 'transparent #' + self.getRndAndFillZero()+' transparent transparent';
        };
        $oDiv.css({
        	position:"absolute",
            top: snow_top + "px",
            left:snow_left + "px",
            zIndex:'-1',
            borderTopWidth:s01 + "px",
            borderBottomWidth:s02 + "px",
            borderLeftWidth:s03 + "px",
            borderRightWidth:s04 + "px",
            borderColor : borderColor
        });

        box.append($oDiv);
        self.snowGoDown('snow'+this.i,8*Math.random());
        this.i++;
        var randomNum = 50 * Math.random()* Math.random();
        setTimeout(function(){
            self.createElem();
        },randomNum);  
	},
	snowGoDown:function(snow_id,speed){
		if(speed < 3){
            speed = 3;
        }
        var self = this,
        box = self.target,
        timer = self.timer;
        var $snow_div = $("#"+snow_id);
        var snowTop = parseInt($snow_div.offset().top);
        var snowHeight = parseInt(box.height());
        var snowNewTop = snowTop + speed;
        $snow_div.css({
            top:snowNewTop+"px"
        });
        var n = snow_id;
        if(snowTop < snowHeight){
            snow_id = setTimeout(function(){
                self.snowGoDown(n,speed);
            },timer);
        }else{
            clearTimeout(snow_id);
            $snow_div.remove();
            speed=null;
            this.other++;
        };
	},
	snowRun:function(){
		var self = this,
    	box = this.target;
    	var wind_time;
        var snows = box.find(".snow");
        for(var i = 0;i<snows.length;i++){
            var snowLeft = parseInt(snows.offset().left) + this.wind +'px'
            snows.eq(i).css('left',snowLeft);
        };
        if(Math.abs(this.wind) > 0.1){
            wind_time = setTimeout(function(){
                self.snowRun();
            },this.timer)
        }else{
            clearTimeout(wind_time);
            this.wind = 0;
        };
	}
}

  

 posted on 2013-01-22 15:00  刘卿  阅读(410)  评论(0编辑  收藏  举报