1

参考这个问题: -

JavaScript - 更改 CSS 颜色 5 秒

答案的工作演示:-

http://jsfiddle.net/maniator/dG2ks/

我需要知道如何为它添加缓动效果,以便慢慢地颜色变得 100% 不透明并同样获得 100% 透明。

4

2 回答 2

1

代码

function makeRGBStr(obj) {
    if (obj.a == null) return "rgb(" + obj.r + "," + obj.g + "," + obj.b + ")";
    else return "rgba(" + obj.r + "," + obj.g + "," + obj.b + "," + obj.a + ")";
}


window["highlight"] = function(obj, color) {
    var highlightColor = color || {
        "r": 255,
        "g": 0,
        "b": 0
    };


    var orig = obj.style.backgroundColor;
    var curAlpha = 1;
    obj.style.backgroundColor = makeRGBStr(highlightColor);
    setTimeout(function() {
        curAlpha -= 0.1;
        var newColor = highlightColor;
        newColor.a = curAlpha;
        obj.style.backgroundColor = makeRGBStr(newColor);

        if (curAlpha <= 0) {
            obj.style.backgroundColor = orig;
        }
        else {

            setTimeout(arguments.callee, 100);
        }
    });
}

jsFiddle:http: //jsfiddle.net/dG2ks/32/

一些例子

于 2012-03-31T10:42:20.403 回答
0

您可以添加 jquery 库,结合 jquery ui - 如果您还没有使用它 - 并使用 switchClass 方法。

这里的所有信息:http: //jqueryui.com/demos/switchClass/

只需 5 行即可完成您想要的操作:

将 jquery en jquery ui 放在页面的 head 部分(2 行代码)。这些是远程托管的文件,您可以“按原样”复制/粘贴代码。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>

然后,在正文的末尾,放置一个包含这三行代码的脚本:

    $(".yourbutton".click(function() {
            switchClass('.currentclass','.redclass',500) 
            // transition from .currentclass to .redclass in 500 milliseconds
    });
于 2012-03-31T12:41:29.050 回答