在网上找到的JavaScript的淡入淡出代码都升级基于jQuery的,抑或不合适的,所以自己写了一份。包括我自己的网站首页的广告轮播也用了这份代码。
var anim = {switchTimePicc : 0,element : null,sss : function (x, y, callback){if (x == 1){if (y<=anim.switchTimePicc){anim.element.style.opacity = y/anim.switchTimePicc;setTimeout("anim.sss(" + x + ", " + ++y + ", " + callback.toString() + ")", 1);} else {callback();}} else {if (y>=0){anim.element.style.opacity = y/anim.switchTimePicc;setTimeout("anim.sss(" + x + ", " + --y + ", " + callback.toString() + ")", 1);} else {callback();}}},fadeIn : function (time, callback){anim.switchTimePicc = time;if (callback == undefined)callback = function () {};anim.sss(1, 0, callback);},fadeOut : function (time, callback){anim.switchTimePicc = time;if (callback == undefined)callback = function () {};anim.sss(2, time, callback);}}