在黑背景的退色的文本

时间:2012-05-14 04:41:14

标签: fadein

我有以下褪色代码,允许我淡入白色到黑色的文字,但在我的网站上我有一个背部背景,需要淡化从黑色到白色,任何人都可以帮助..

<html>
<head>
<title>Fading Text Example</title>
<script language="javascript"> 
var hcolor=255;
function Fade(direction) {
obj=document.getElementById("head1");
hcolor += direction;
if (hcolor >= 0 && hcolor < 256) {
hex=hcolor.toString(16);
if (hex.length==1) hex="0" + hex;
obj.style.color="#" + hex + hex + hex;
window.setTimeout("Fade(" + direction + ");",1);
}
}
</script>
</head>
<body onLoad="Fade(1);">
<h1 ID="head1" style="color:#FFFFFF;">
Fading Text Example</h1> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

似乎函数已经包含指定方向的能力,只需传入-1而不是1,并将初始hColor设置为0.节点在下面我也编辑了格式稍微清理变量名称,使其更清晰/更符合。

<html>
    <head>
        <title>Fading Text Example</title>
        <script language="text/javascript"> 
            var hColor=0;
            function fade(direction) {
                obj=document.getElementById("head1");
                hcolor += direction;
                if ((hColor >= 0) && (hColor < 256)) {
                    hex=hColor.toString(16);
                    if (hex.length==1) hex="0" + hex;
                    obj.style.color="#" + hex + hex + hex;
                    window.setTimeout("fade(" + direction + ");",1);
                }
            }
        </script>
    </head>
    <body onload="fade(-1);">
    <h1 id="head1" style="color:#FFFFFF;">Fading Text Example</h1> 
</body>