0

我有以下淡入淡出代码,它允许我将文本从白色淡入黑色,但在我的网站上,我有一个背景,需要将它从黑色淡入白色,谁能帮忙..

<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>
4

1 回答 1

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>

于 2012-05-14T04:49:13.517 回答