2011-11-26 29 views
-1
function fade(obj, time) { 
    if(this) { //prevents the function from being called too many times at once 
     addCallData(this); 
    } 
    if(!obj || !time) { 
     alert("Object/time parameters are required.\n"); 
     return false; 
    } else { 
     if(!func_data[this]["fade"]) { /*if fade is not defined for [this], then define it*/ 
      func_data[this]["fade"] = ((obj.style.opacity)?1:(obj.style.filter)); 
     } else { 
      if(func_data[this]["fade"] <= 0) { /*if object opacity has declined completely, then hide/remove the element to indicate that the object has faded*/ 
       obj.style.display="none"; 
       return false; 
      } 
      func_data[this]["fade"]=((func_data[this]["fade"])--); /*gradually reduce fade*/ 
      ((obj.style.filter)?((obj.style.filter)=func_data[this]["fade"]): 
      (obj.style.filter("alpha(opacity="+(func_data[this]["fade"])+""))); /*ultimately, set opacity to x-0.1 or x-1*/ 
      setTimeout(function(){fade(obj, time);}, time); /*loop until false occurs*/ 
     } 
    }  
} 

我不完全确定为什么这不起作用。我可以将func_data[this]["fade"]设置为0.50,并且HTML元素将失去其不透明度的50%,但是如果将该属性的值设置为不透明度,则该函数将在此处停止并且不会到达超时阶段。Javascript设置不透明功能

+0

@DrStrangeLove这是行不通的。 – user784446

+0

i'if(this)'完全没用。 “这个”总是被强迫进入总是真诚的对象。 – pimvdb

+0

@pimvdb你能否提供澄清? – user784446

回答

0

检查:

 
function setOpacity(value) { 
    testObj.style.opacity = value/10; 
    testObj.style.filter = 'alpha(opacity=' + value*10 + ')'; 
} 

从参考:http://www.quirksmode.org/js/opacity.html

+0

这也行不通(y在全球范围内): 函数fade(obj,time){ if(y <= 0){return false;} y =((y)-0.1); obj.style.opacity = y; setTimeout(function(obj,time){fade(obj,time);},50); } – user784446