小褪色虫
我有下面的图像淡入/淡出脚本,它确实工作得很好,除了一个错误,这是如果你切换选项卡和回来,偶尔被淡出的图像将挂在背景中,半透明和半不透明.
以下是我正在使用的代码
选择 | 换行 | 行号
- var objIn;
- var objOut;
- var imgCont;
- var imgArr = new Array();
- var waitTime = 4; //The amount of time the script waits till it should move on to the next image (seconds)
- var fadespeed = 50; //The speed of the fading in and out (milliseconds)
- var lastindex = 0;
- var timer = 0;
- function start(divElement){
- imgCont = document.getElementById(divElement);
- for(var i=0;i<imgCont.getElementsByTagName("img").length;i++){
- imgArr[i] = imgCont.getElementsByTagName("img")[i];
- }
- if(lastindex>=(imgArr.length-1)){
- timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[0].id+"\")", waitTime*1000);
- }
- else{
- timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[lastindex+1].id+"\")", waitTime*1000);
- }
- }
- function pause(){
- if(timer!=0){
- window.clearTimeout(timer);
- timer = 0;
- }
- }
- function resume(){
- if(lastindex>=(imgArr.length-1)){
- timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[0].id+"\")", waitTime*1000);
- }
- else{
- timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[lastindex+1].id+"\")", waitTime*1000);
- }
- }
- function fadeinout(current, out){
- objIn = document.getElementById(current);
- objOut = document.getElementById(out);
- fdio();
- if(lastindex>=(imgArr.length-1)){
- lastindex = 0;
- }
- else{
- lastindex = lastindex + 1;
- }
- start(imgCont.id);
- }
- function fdio(){
- if(document.all){ //If statement is true then browser is IE
- if(objIn.filters.alpha.opacity>0){
- objIn.style.filter = 'alpha(opacity='+(objIn.filters.alpha.opacity-10)+')';
- objOut.style.filter = 'alpha(opacity='+(objOut.filters.alpha.opacity+10)+')';
- setTimeout("fdio(\""+objIn.id+"\", \""+objOut.id+"\")", fadespeed);
- }
- }
- else{
- if(objIn.style.opacity>0){
- objIn.style.opacity = ((objIn.style.opacity*10)-1)/10;
- objOut.style.opacity = ((objOut.style.opacity*10)+1)/10;
- setTimeout("fdio(\""+objIn.id+"\", \""+objOut.id+"\")", fadespeed);
- }
- }
- }
附注:当切换到不同的程序一段时间后,脚本在后台运行时,也可能会出现该错误