Skip to content

Instantly share code, notes, and snippets.

@yyr446
Created December 1, 2010 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyr446/723150 to your computer and use it in GitHub Desktop.
Save yyr446/723150 to your computer and use it in GitHub Desktop.
FallObj.js
function objtimer(input,callbackfn,thisArg){
var current = input.start;
var timerId = setInterval(function(){
var step = input.step;
var end = input.end;
var ending;
callbackfn.call(thisArg,{type:'init'});
current += step;
ending = (step > 0 && current >= end) || (step < 0 && current <= end);
if(ending) current = end;
if (false === callbackfn.call(thisArg,{type:'timer',relatedValue:current})
|| ending){
callbackfn.call(thisArg,{type:'complete'});
clearInterval(timerId);
input = callbackfn = thisArg = current = timerId = null;
}
},input.interval);
}
function objtimer_S (flgment,interval,step,start,end,onInit,styleSetter,onComplete,obj){
objtimer({interval:interval,step:step,start:start,end:end},
function(e){
switch(e.type){
case "init":
this.onInit(this.document,this.flgment,this.obj);
return true;
case "timer":
this.styleSetter(this.flgment,e.relatedValue,this.obj);
return true;
case "complete":
this.onComplete(this.flgment,this.onInit,this.styleSetter,this.onComplete,this.obj);
return false;
}
},{
obj:obj,
document:document,flgment:flgment,
styleSetter: styleSetter || new Function,
onComplete: onComplete || new Function,
onInit: onInit || new Function,
});
}
function falldownobj(target){
this.target = target.cloneNode(true);
this.flg = true;
}
falldownobj.prototype.start =function(){
var C = document /*@cc_on @if (1) [document.compatMode == 'CSS1Compat' ?
'documentElement' : 'body'] /*@else@*/ .defaultView /*@end@*/;
var range = {"w":/*@if (1) C.clientWidth @else@*/ C.innerWidth /*@end@*/,
"h":/*@if (1) C.clientHeight @else@*/ C.innerHeight /*@end@*/};
var x = Math.floor(Math.random()*range.w);
var y = Math.floor(Math.random()*range.h);
var step = Math.ceil(Math.random()*20);
this.target.style.left = x + "px";
objtimer_S(this.target,100,step,y,range.h,this.setup,this.move,this.loop,this);
};
falldownobj.prototype.setup =function(doc,elm,obj){
doc.body.appendChild(elm);
}
falldownobj.prototype.move =function(elm,value,obj){
if(obj.flg)
  //elm.style.top = value + "px"; //スクロール無視
  //elm.style.top = value + window.scrollY + "px"; //IE,Operaだめ
elm.style.top = value + document.documentElement.scrollTop + "px"; //Safariだめ
else{
  elm.style.display = "none";
}
};
falldownobj.prototype.loop =function(elm,onInit,styleSetter,onComplete,obj){
var C = document /*@cc_on @if (1) [document.compatMode == 'CSS1Compat' ?
'documentElement' : 'body'] /*@else@*/ .defaultView /*@end@*/;
var range = {"w":/*@if (1) C.clientWidth @else@*/ C.innerWidth /*@end@*/,
"h":/*@if (1) C.clientHeight @else@*/ C.innerHeight /*@end@*/};
var x = Math.floor(Math.random()*range.w);
var y = Math.floor(Math.random()*range.h);
elm.style.left = x + "px";
var step = Math.ceil(Math.random()*20);
if(obj.flg)
objtimer_S(elm,100,step,y,range.h,onInit,styleSetter,onComplete,obj);
else{
document.body.removeChild(elm);
obj.target = null;
}
};
falldownobj.prototype.end = function(){
this.flg = false;
}
function init(target){
if (1 > arguments.length) return null;
target.style.position = 'absolute';
var obj = new falldownobj(target);
obj.start();
return obj;
}
function setfalldownobj(target,nummber){
var obj=[];
for(var i=0;i<nummber;i++){obj[i] = init(target);}
return obj;
}
function stopfalldownobj(obj){
for(var i=0;i<obj.length;i++) obj[i].end();
}
function changefalldownobj(obj,target,number){
stopfalldownobj(obj);
obj = setfalldownobj(target,number);
return obj;
}
@yyr446
Copy link
Author

yyr446 commented Dec 2, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment