Javascript在函数中设置不透明度

Javascript setting opacity in function

本文关键字:设置 不透明度 函数 Javascript      更新时间:2023-09-26
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%的不透明度,但如果我将该属性的值设置为不透明度,函数将停止在那里,不会达到超时阶段。

检查这个:

<>之前setOpacity(value) {testObj.style.opacity = value/10;testObj.style.filter =' alpha(不透明度=' + value*10 + ')';}之前

来源:http://www.quirksmode.org/js/opacity.html