小褪色虫

我有下面的图像淡入/淡出脚本,它确实工作得很好,除了一个错误,这是如果你切换选项卡和回来,偶尔被淡出的图像将挂在背景中,半透明和半不透明.
以下是我正在使用的代码

选择 | 换行 | 行号
  1. var objIn;
  2. var objOut;
  3. var imgCont;
  4. var imgArr = new Array();
  5. var waitTime = 4; //The amount of time the script waits till it should move on to the next image (seconds)
  6. var fadespeed = 50; //The speed of the fading in and out (milliseconds)
  7. var lastindex = 0;
  8. var timer = 0;
  9.  
  10. function start(divElement){
  11.   imgCont = document.getElementById(divElement);
  12.   for(var i=0;i<imgCont.getElementsByTagName("img").length;i++){
  13.     imgArr[i] = imgCont.getElementsByTagName("img")[i];
  14.   }
  15.   if(lastindex>=(imgArr.length-1)){
  16.     timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[0].id+"\")", waitTime*1000);
  17.   }
  18.   else{
  19.     timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[lastindex+1].id+"\")", waitTime*1000);
  20.   }
  21. }
  22.  
  23. function pause(){
  24.   if(timer!=0){
  25.     window.clearTimeout(timer);
  26.     timer = 0;
  27.   }
  28. }
  29.  
  30. function resume(){
  31.   if(lastindex>=(imgArr.length-1)){
  32.     timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[0].id+"\")", waitTime*1000);
  33.   }
  34.   else{
  35.     timer = window.setTimeout("fadeinout(\""+imgArr[lastindex].id+"\", \""+imgArr[lastindex+1].id+"\")", waitTime*1000);
  36.   }
  37. }
  38.  
  39. function fadeinout(current, out){
  40.   objIn = document.getElementById(current);
  41.   objOut = document.getElementById(out);
  42.   fdio();
  43.   if(lastindex>=(imgArr.length-1)){
  44.     lastindex = 0;
  45.   }
  46.   else{
  47.     lastindex = lastindex + 1;
  48.   }
  49.   start(imgCont.id);
  50. }
  51.  
  52. function fdio(){
  53.   if(document.all){ //If statement is true then browser is IE
  54.     if(objIn.filters.alpha.opacity>0){
  55.       objIn.style.filter = 'alpha(opacity='+(objIn.filters.alpha.opacity-10)+')';
  56.       objOut.style.filter = 'alpha(opacity='+(objOut.filters.alpha.opacity+10)+')';
  57.       setTimeout("fdio(\""+objIn.id+"\", \""+objOut.id+"\")", fadespeed);
  58.     }
  59.   }
  60.   else{
  61.     if(objIn.style.opacity>0){
  62.       objIn.style.opacity = ((objIn.style.opacity*10)-1)/10;
  63.       objOut.style.opacity = ((objOut.style.opacity*10)+1)/10;
  64.       setTimeout("fdio(\""+objIn.id+"\", \""+objOut.id+"\")", fadespeed);
  65.     }
  66.   }
  67. }

附注:当切换到不同的程序一段时间后,脚本在后台运行时,也可能会出现该错误

标签: Javascript

添加新评论