/* Inspired by Autumn leaves- by Kurt Grigg (kurt.grigg@virgin.net)
* Modified by Dynamic Drive for NS6 functionality
* visit
http://www.dynamicdrive.com for the original script
* Modified by jscheuer1 in
http://www.dynamicdrive.com/forums
* 01/11 for optional direction, allow effect to
* work fully on wide pages, to not create 'dancing scrollbars' on small
* pages & use jQuery methods. Requires standards mode DOCTYPE
* tested in all current version major browsers including IE 8 & IE 9 Beta. */
/* Updated further 10/13 to allow for stop and start and multiple usage per page, adds preload confirmation before start */
function rise_fall(opts){
this.init(opts);
}
(function($){
rise_fall.prototype = {
defaults: { //these items may be set in the on page call
dir: 1, // Set the direction (1 for down, -1 for up)
speed: 20, // 12 to whatever (60 is pretty slow) higher numbers are slower
Amount: 18, // Smoothness depends on image file size, the smaller the size the more you can use!
sway: 6, // Set amount of left/right swaying of objects (default = 10), higher numbers produce more sway
//Pre-load your image(s) below! 6 is an optimal number for variety. Use just one for uniformity.
grphcs: [
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif";
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif";
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif";
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif";
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif";
"http://images2.layoutsparks.com/1/167200/love-fall-maple-leaf.gif"
]
},
//////////////// Stop Editing //////////////
loadem: function(){
var i = this.grphcs.length, c = i, rs = this;
while(--i > -1){
$(new Image()).bind('load error', function(){
if(!--c){rs.makeem();}
}).attr('src', this.grphcs[i]);
}
},
$master: $('<div style: "position:fixed,top:0,left:0;">'),
WinHeight: $(window).height(),
WinWidth: $(window).width(),
makeem: function(){
var i = this.Amount, els = [], rs = this;
this.grphcs.sort(function(){return 0.5 - Math.random();});
while (--i > -1){
this.els.push($('<img alt="" src="' + this.grphcs[i % this.grphcs.length] + '" style="position:fixed;top:0;left:0;">').appendTo(this.$master));
this.Ypos.push(Math.floor(Math.random()*this.WinHeight));
this.Xpos.push(Math.floor(Math.random()*this.WinWidth));
this.Speed.push((Math.random()*5+3)*this.dir);
this.Cstep.push(0);
this.Step.push(Math.random()*0.1+0.05);
}
$('body').append(this.$master);
this.moveem();
this.inst = setInterval(function(){rs.moveem();}, this.speed);
},
moveem: function(){
this.WinHeight = $(window).height();
this.WinWidth = $(window).width();
var i = this.Amount, sy, sx, w, h, cloned;
while (--i > -1){
sy = this.Speed[i]*Math.sin(90*Math.PI/180);
sx = this.Speed[i]*Math.cos(this.Cstep[i]);
w = this.els[i].width();
h = this.els[i].height();
this.Ypos[i] += sy;
this.Xpos[i] += sx * this.sway * 0.1;
if (this.Ypos[i] > this.WinHeight && this.dir === 1 || this.Ypos[i] < -h && this.dir === -1){
this.Xpos[i] = Math.round(Math.random() * this.WinWidth);
this.Speed[i] = (Math.random() * 5 + 3) * this.dir;
}
this.Ypos[i] = (this.Ypos[i] > this.WinHeight && this.dir === 1)? -h : (this.Ypos[i] < -h && this.dir === -1)? this.WinHeight + h : this.Ypos[i];
sx = Math.min(this.WinWidth, this.Xpos[i]);
sy = this.Ypos[i];
if(this.dir === 1 && sy > this.WinHeight - h && this.accumulate && !this.els[i].data('cloned')){
this.clones.push(this.els[i].data({cloned: true}).clone().css({top: 'auto', bottom: 0, zIndex: -1}).appendTo(this.$master));
(function(el){
setTimeout(function(){el.data({cloned: false});}, 2000);
})(this.els[i].css({visibility: 'hidden'}));
while(this.clones.length > this.accumulate){
(this.clones.shift()).fadeOut('slow', function(){$(this).remove();});
}
} else if(sy === -h) {
this.els[i].css({visibility: 'visible'})
}
this.els[i].css({left: sx, top: sy});
this.Cstep[i] += this.Step[i];
}
},
stop: function(hide){
clearInterval(this.inst);
if(hide){
$.each(this.els, function(i, el){el.hide();});
$.each(this.clones, function(i, el){el.hide();});
}
},
restart: function(fresh){
clearInterval(this.inst);
if(fresh){
$.each(this.els, function(i, el){el.remove();});
$.each(this.clones, function(i, el){el.remove();});
this.init(this.cObj);
} else {
$.each(this.els, function(i, el){el.show();});
$.each(this.clones, function(i, el){el.show();});
var rs = this;
this.inst = setInterval(function(){rs.moveem();}, this.speed);
}
},
init: function(cObj){
cObj = cObj && cObj.constructor === Object? cObj : {};
this.cObj = cObj;
$.extend(this, this.defaults, cObj);
this.Ypos = []; this.Xpos = []; this.Speed = []; this.Step = []; this.Cstep = []; this.els = []; this.clones = [];
this.loadem();
}
};
})(jQuery);
var leaves = new rise_fall({speed: 60, sway: 20, Amount: 10, accumulate: 255});