function fade(obj,tar) {
obj.timer && clearInterval(obj.timer);
obj.timer=setInterval(function () {
var cur=parseInt(css(obj,'opacity')*100);
var speed=(tar-cur)/7;
speed=(speed>0)?Math.ceil(speed):Math.floor(speed);
if(cur!=tar){
obj.style.opacity=(cur+speed)/100;
obj.style.filter='alpha(opacity='+(cur+speed)+')'
}else{
clearInterval(obj.timer);
obj.timer=null;
}
},40);
}
function css(obj, attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
var box=document.getElementById('box');
fade(box,40)