Skip to content

Instantly share code, notes, and snippets.

@lbj96347
Created March 6, 2012 14:21
Show Gist options
  • Save lbj96347/1986529 to your computer and use it in GitHub Desktop.
Save lbj96347/1986529 to your computer and use it in GitHub Desktop.
Javascript Fade Effect
//how to use it?you just need to use this words : 'fadeEffect.init(theID,0)' or 'fadeEffect.init(theID,1);'
//*theID is which element you want to add the effect in.
//*the 0/1 is the Parameter for the effect.0 stands for Transparent;1 stands for Visible
//besides you should set opacity in your html tags
//怎样用它?你只要写这个语句就可以了: 'fadeEffect.init(theID,0)' 或者 'fadeEffect.init(theID,1);'
//0 或者 1 这个这个效果的参数。0代表透明,1代表可见。
//此外,建议你再你的html标签里面设定好初始值
fadeEffect=function(){
return{
init:function(id, flag, target){
this.elem = document.getElementById(id);
clearInterval(this.elem.si);
this.target = target ? target : flag ? 100 : 0;
this.flag = flag || -1;
this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
this.si = setInterval(function(){fadeEffect.tween()}, 20);
},
tween:function(){
if(this.alpha == this.target){
clearInterval(this.si);
}else{
var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);
this.elem.style.opacity = value / 100;
this.elem.style.filter = 'alpha(opacity=' + value + ')';
this.alpha = value
}
}
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment