淡入方法
HTMLElement.prototype.fadeIn = function(speed=0.05,callback){
//判断速度
if(speed === "slow"){speed = 0.02};
if(speed === "fast"){speed = 0.08};
if(typeof speed === "function"){
callback = speed;
speed = 0.05;
}
//淡入逻辑,让opacity逐渐变为1
this.style.opacity = 0;
this.style.display = "block";
let fadeOut = setInterval(()=>{
let o = parseFloat(this.style.opacity);
if(o >= 1){
this.style.opacity = 1 ;
clearInterval(fadeOut)
callback && typeof callback === "function" ? callback() : ""
return;
}
this.style.opacity = o + speed;
},16)
}
淡出方法
HTMLElement.prototype.fadeOut = function(speed=0.05,callback){
//判断速度
if(speed === "slow"){speed = 0.02};
if(speed === "fast"){speed = 0.08};
if(typeof speed === "function"){
callback = speed;
speed = 0.05;
}
//淡出逻辑,让opacity逐渐变为0
this.style.opacity = 1;
let fadeOut = setInterval(()=>{
let o = parseFloat(this.style.opacity);
if(o <= 0){
this.style.opacity = 0;
this.style.display = "none";
clearInterval(fadeOut)
callback && typeof callback === "function" ? callback() : ""
return;
}
this.style.opacity = o - speed;
},16)
}